Skip to content

Tracking Issue for [*const T|*mut T]::with_metadata_of #75091

Open

Description

@oliver-giersch

Feature gate: #![feature(set_ptr_value)]

Public API

impl<T: ?Sized> *mut T {
    pub fn with_metadata_of<U: ?Sized>(self, val: *const U) -> *mut U;
}

impl<T: ?Sized> *const T {
    pub fn with_metadata_of<U: ?Sized>(self, val: *const U) -> *const U;
}

Steps / History

Unresolved Questions

Activity

  1. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Aug 3, 2020
  2. added
    B-unstableBlocker: Implemented in the nightly compiler and unstable.
    T-libs-api[DEPRECATED; DO NOT USE]
    on Aug 3, 2020
  3. added
    I-libs-radarLibs issues that are tracked on the team's radar.
    A-raw-pointersArea: raw pointers, MaybeUninit, NonNull
    on Aug 6, 2020
  4. added a commit that references this issue on Aug 11, 2020
  5. SimonSapin commented on Apr 16, 2021

    @SimonSapin
    Contributor

    As discussed on Zulip this could be replaced with ptr::from_raw_parts(val, ptr::metadata(self)), tracked in #81513

  6. ChayimFriedman2 commented on Mar 29, 2022

    @ChayimFriedman2
    Contributor

    Even if we want to keep these methods I think it's better to write them in terms of the metadata API as it's easy to reason about and less error prone than the current implementation.

  7. changed the title [-]Tracking Issue for [*const T|*mut T]::set_ptr_value[/-] [+]Tracking Issue for [*const T|*mut T]::with_metadata_of[/+] on Oct 21, 2022
  8. added a commit that references this issue on Oct 22, 2022
  9. added a commit that references this issue on Oct 22, 2022
  10. 11 remaining items

  11. GnomedDev commented on Sep 24, 2024

    @GnomedDev
    Contributor

    For the "Is this even needed/useful given that there now also is #81513?", I feel like this is super useful even with or without the metadata APIs.

    • Without (as stable is now) it allows for working with metadata in a limited fashion without assuming layout (which people are, and will do if not given the correct tools).
    • With, this is still useful as a higher level helper function, and doesn't block any of the metadata API stuff as a with_metadata function could be added to attach a Pointee::Metadata to an existing pointer in future.

    I'd love to see this stabilized, if that is a good resolution to the last question needed answering.

  12. Noratrieb commented on Sep 27, 2024

    @Noratrieb
    Member

    @rust-lang/libs-api see above

  13. scottmcm commented on Oct 2, 2024

    @scottmcm
    Member

    Hmm, I think the primary alternative to this would be something like rust-lang/libs-team#246 -- instead of copying the metadata from a full other pointer, just remove the blocker to letting you pass around the metadata directly.

    It's not obvious to me that people really ever want this version where you need to pass a full pointer, just to ignore its address. Or is there some situation I'm not thinking of where that's really better than doing a let (p, m) = pointer.parts(); then p.whatever().with_metadata(m)?

  14. GnomedDev commented on Oct 3, 2024

    @GnomedDev
    Contributor

    As I said above @scottmcm:

    • Without pointer metadata: This is a simpler API that can be stable now to allow people to do what they are already doing but soundly (without assuming fat pointer layout).
    • With pointer metadata: This is a helper function for the situation where someone does have a full pointer and can avoid splitting it into parts just to add the metadata on.

    My motivating example is https://github.com/andylokandy/smallbox which has been assuming fat pointer layout for years and I recently added a nightly feature which uses this API and strict provenance. Since strict provenance is in the process of being stabilized, this being stabilized as well would be amazing for smallbox.

  15. 197g commented on Oct 3, 2024

    @197g
  16. RalfJung commented on Oct 4, 2024

    @RalfJung
    Member

    @HeroicKatora IMO it makes little sense to have a function with_metadata_of<U: ?Sized>(self, other: Metadata<U>) -> *mut U -- that should be called with_metadata! The of part refers to "with the metadata of another pointer"; if we are directly providing the metadata, we should just say "with this metadata", and there's no "of" involved any more.

    This tracking issue is specifically about copying the metadata from one pointer to another. Your proposal would be part of #81513.

  17. 197g commented on Oct 4, 2024

    @197g
    Contributor

    I understood @scottmcm as a new question, separate from #81513 itself, to explore whether rust-lang/libs-team#246 changes something about the unresolved redundancy question in the issue. It is not accepted, so not yet a part of #81513 to my understanding (and not linked there either). Maybe I'm misreading the process here.

    Neither the API of this (#75091) nor the sketch based on rust-lang/libs-team#246 would require stabilization of the Metadata trait itself. I think this affects the merit of the simplicity argument. The representation without address is superior for some library use cases. (It would definitely occur in practice, infact I'm sure my libraries would always prefer the address-less value). I concur, those methods should be named with_metadata, not with_metadata_of, so there's no necessary overlap in the interface and it would of course be possible to have both sets of methods.

    Are you suggesting the redundancy even with the ACP is slight enough that it would still not be problematic, and there's enough merit to consuming a pointer directly? Definitely a reasonable position to take. I just intended for it to be clarified that the ACP presents a (middle ground) alternative not yet explored in comments in this thread. If libs is in favor of potentially having both that's fine 👍

  18. RalfJung commented on Oct 4, 2024

    @RalfJung
    Member

    All I am saying is, we should not have functions called with_metadata_of that take a pointer and a metadata. That name should only be used for functions that take two pointers.

    This issue here is specifically about the two-pointer version of the APIs, which avoids stabilizing any new type. with_metadata in the form you suggested IMO makes a lot of sense (it nicely matches with_addr as well), but such an API should be proposed in #81513 since it clearly requires talking about "a metadata value matching this pointer type". Whether that is expressed as Metadata<T> or <T as Pointee>::Metadata is a completely orthogonal question.

    IOW, rust-lang/libs-team#246 is a modification of #81513, but has absolutely no bearing on this issue.

  19. added a commit that references this issue on Oct 15, 2024
  20. added a commit that references this issue on Oct 15, 2024
  21. added a commit that references this issue on Oct 15, 2024
  22. added
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    and removed
    T-libs-api[DEPRECATED; DO NOT USE]
    on Aug 12, 2026
  23. removed
    I-libs-radarLibs issues that are tracked on the team's radar.
    on Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-raw-pointersArea: raw pointers, MaybeUninit, NonNullB-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions