-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Implement provenance preserving methods on NonNull #95556
Conversation
**Description** Add the `addr`, `with_addr, `map_addr` methods to the `NonNull` type, and map the address type to `NonZeroUsize`. **Motiviation** The `NonNull` type is useful for implementing pointer types which have the 0-niche. It is currently possible to implement these provenance preserving functions by calling `NonNull::as_ptr` and `new_unchecked`. The addition of these methods simply make it more ergonomic to use. **Testing** Added a unit test of a nonnull tagged pointer type. This is based on some real code I have elsewhere, that currently routes the pointer through a `NonZeroUsize` and back out to produce a usable pointer.
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @dtolnay (or someone else) soon. Please see the contribution instructions for more information. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
@bors r+ |
@bors ping |
😪 I'm awake I'm awake |
@bors r+ |
📌 Commit 2a82763 has been approved by |
Implement provenance preserving methods on NonNull ### Description Add the `addr`, `with_addr`, `map_addr` methods to the `NonNull` type, and map the address type to `NonZeroUsize`. ### Motivation The `NonNull` type is useful for implementing pointer types which have the 0-niche. It is currently possible to implement these provenance preserving functions by calling `NonNull::as_ptr` and `new_unchecked`. The adding these methods makes it more ergonomic. ### Testing Added a unit test of a non-null tagged pointer type. This is based on some real code I have elsewhere, that currently routes the pointer through a `NonZeroUsize` and back out to produce a usable pointer. I wanted to produce an ideal version of the same tagged pointer struct that preserved pointer provenance. ### Related Extension of APIs proposed in rust-lang#95228 . I can also split this out into a separate tracking issue if that is better (though I may need some pointers on how to do that).
Implement provenance preserving methods on NonNull ### Description Add the `addr`, `with_addr`, `map_addr` methods to the `NonNull` type, and map the address type to `NonZeroUsize`. ### Motivation The `NonNull` type is useful for implementing pointer types which have the 0-niche. It is currently possible to implement these provenance preserving functions by calling `NonNull::as_ptr` and `new_unchecked`. The adding these methods makes it more ergonomic. ### Testing Added a unit test of a non-null tagged pointer type. This is based on some real code I have elsewhere, that currently routes the pointer through a `NonZeroUsize` and back out to produce a usable pointer. I wanted to produce an ideal version of the same tagged pointer struct that preserved pointer provenance. ### Related Extension of APIs proposed in rust-lang#95228 . I can also split this out into a separate tracking issue if that is better (though I may need some pointers on how to do that).
Rollup of 8 pull requests Successful merges: - rust-lang#95354 (Handle rustc_const_stable attribute in library feature collector) - rust-lang#95373 (invalid_value lint: detect invalid initialization of arrays) - rust-lang#95430 (Disable #[thread_local] support on i686-pc-windows-msvc) - rust-lang#95544 (Add error message suggestion for missing noreturn in naked function) - rust-lang#95556 (Implement provenance preserving methods on NonNull) - rust-lang#95557 (Fix `thread_local!` macro to be compatible with `no_implicit_prelude`) - rust-lang#95559 (small type system refactoring) - rust-lang#95560 (convert more `DefId`s to `LocalDefId`) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
|
||
/// Creates a new pointer by mapping `self`'s address to a new one. | ||
/// | ||
/// This is a convenience for [`with_addr`][Self::with_addr], see that method for details. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be good to have a similar reference in the addr
and with_addr
docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean references to the ptr::addr
and ptr::with_addr
from the NonNull
versions? I can make a followup CR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's what I mean. That would be great. :)
Description
Add the
addr
,with_addr
,map_addr
methods to theNonNull
type, and map the address type toNonZeroUsize
.Motivation
The
NonNull
type is useful for implementing pointer types which have the 0-niche. It is currently possible to implement these provenance preserving functions by callingNonNull::as_ptr
andnew_unchecked
. The adding these methods makes it more ergonomic.Testing
Added a unit test of a non-null tagged pointer type. This is based on some real code I have elsewhere, that currently routes the pointer through a
NonZeroUsize
and back out to produce a usable pointer. I wanted to produce an ideal version of the same tagged pointer struct that preserved pointer provenance.Related
Extension of APIs proposed in #95228 . I can also split this out into a separate tracking issue if that is better (though I may need some pointers on how to do that).