Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expose_provenance and from_exposed_provenance on NonNull #518

Closed
RalfJung opened this issue Jan 3, 2025 · 4 comments
Closed

Add expose_provenance and from_exposed_provenance on NonNull #518

RalfJung opened this issue Jan 3, 2025 · 4 comments
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@RalfJung
Copy link
Member

RalfJung commented Jan 3, 2025

Proposal

Problem statement

expose_provenance and from_exposed_provenance exist on raw pointers but not on NonNull.

Motivating examples or use cases

This seems like a simple oversight; to my knowledge we generally aim for API consistency between NonNull and raw pointers.

Solution sketch

impl<T> NonNull<T> {
    pub fn expose_provenance(self) -> NonZero<usize>;
    pub fn from_exposed_provenance(addr: NonZero<usize>) -> Self;
}

Alternatives

We could do nothing.

Links and related work

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
@RalfJung RalfJung added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Jan 3, 2025
@kennytm
Copy link
Member

kennytm commented Jan 3, 2025

while std::ptr::with_exposed_provenance::<T> requires T: Sized, <*const T>::expose_provenance has no such requirement. So for consistency NonNull<T>::expose_provenance should also allow unsized T.

impl<T: ?Sized> NonNull<T> {
    pub fn expose_provenance(self) -> NonZero<usize>;
}
impl<T> NonNull<T> { // or better T: ?Sized + Thin
    pub fn from_exposed_provenance(addr: NonZero<usize>) -> Self;
}

@traviscross
Copy link

This was discussed in the libs-api call today. The conclusion was, "ship it, inclusive of the minor fix mentioned by @kennytm." That is, this ACP is accepted, modulo that fix.

@dtolnay dtolnay closed this as completed Jan 7, 2025
@Amanieu Amanieu added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Jan 7, 2025
@RalfJung
Copy link
Member Author

RalfJung commented Jan 8, 2025

Turns out there's another function missing on NonNull: without_provenance. Could someone reopen the issue or should I file a new ACP?

@RalfJung
Copy link
Member Author

RalfJung commented Jan 8, 2025

Tracking issue: rust-lang/rust#135243
Implementation PR: rust-lang/rust#135242

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 9, 2025
…ratt

add missing provenance APIs on NonNull

This adds some provenance APIs that exist on raw pointers but have been forgotten on `NonNull`:
```rust
impl<T> NonNull<T> {
    pub const fn without_provenance(addr: NonZero<usize>) -> Self;
    pub fn from_exposed_provenance(addr: NonZero<usize>) -> Self;
}
impl<T: ?Sized> NonNull<T> {
    pub fn expose_provenance(self) -> NonZero<usize>;
}
```
rust-lang/libs-team#518 is the ACP for the two exposed provenance ones; I forgot to include `without_provenance` there but I hope that, too, is uncontroversial (and anyway this PR only adds things unstably). Cc `@rust-lang/libs-api`

Tracking issue: rust-lang#135243
jhpratt added a commit to jhpratt/rust that referenced this issue Jan 9, 2025
…ratt

add missing provenance APIs on NonNull

This adds some provenance APIs that exist on raw pointers but have been forgotten on `NonNull`:
```rust
impl<T> NonNull<T> {
    pub const fn without_provenance(addr: NonZero<usize>) -> Self;
    pub fn from_exposed_provenance(addr: NonZero<usize>) -> Self;
}
impl<T: ?Sized> NonNull<T> {
    pub fn expose_provenance(self) -> NonZero<usize>;
}
```
rust-lang/libs-team#518 is the ACP for the two exposed provenance ones; I forgot to include `without_provenance` there but I hope that, too, is uncontroversial (and anyway this PR only adds things unstably). Cc ``@rust-lang/libs-api``

Tracking issue: rust-lang#135243
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 9, 2025
Rollup merge of rust-lang#135242 - RalfJung:nonnull-provenance, r=jhpratt

add missing provenance APIs on NonNull

This adds some provenance APIs that exist on raw pointers but have been forgotten on `NonNull`:
```rust
impl<T> NonNull<T> {
    pub const fn without_provenance(addr: NonZero<usize>) -> Self;
    pub fn from_exposed_provenance(addr: NonZero<usize>) -> Self;
}
impl<T: ?Sized> NonNull<T> {
    pub fn expose_provenance(self) -> NonZero<usize>;
}
```
rust-lang/libs-team#518 is the ACP for the two exposed provenance ones; I forgot to include `without_provenance` there but I hope that, too, is uncontroversial (and anyway this PR only adds things unstably). Cc `@rust-lang/libs-api`

Tracking issue: rust-lang#135243
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

5 participants