-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Rename std::ptr::Shared to NonNull and stabilize it #46952
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @KodrAus (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
That proposal so far has got a couple thumbs up and no negative comment or reaction. Maybe @rust-lang/libs can discuss and do a FCP in this thread? |
Bikeshed: |
@leodasvacas What’s your preferred shed color? By your use of lower case I assume you mean in the |
Sorry for being unclear, I just prefer |
Ok. So the methods would be |
🚲🏡: since it's NonNull (not Nonnull), non_null makes sense over nonnull. |
I’ve pushed a commit to rename |
But That said, riding in the 🚲 lane,
use ::std::ptr::NotNull;
use ::core::nonzero::Nonzero;
fn foo() {
let raw = NotNull::new(&0).unwrap();
let _ = Box::from_not_null_raw(raw);
} or leaning the other way, we could have |
Besides naming, this is also a good time to reconsider whether I added So I think there's value in keeping this conversion in some form. It could be an impl of the
|
src/libcore/ptr.rs
Outdated
@@ -2331,8 +2331,9 @@ impl<T: ?Sized> PartialOrd for *mut T { | |||
/// Unlike `*mut T`, `Unique<T>` is covariant over `T`. This should always be correct | |||
/// for any type which upholds Unique's aliasing requirements. | |||
#[allow(missing_debug_implementations)] |
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.
Could you impl Debug for NonNull (superseding #46792)?
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
fmt::Pointer::fmt(&self.as_ptr(), f) | ||
} | ||
} | ||
|
||
#[unstable(feature = "shared", issue = "27730")] | ||
impl<T: ?Sized> From<Unique<T>> for Shared<T> { | ||
#[stable(feature = "nonnull", since = "1.24.0")] |
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.
Shouldn't this be #[unstable(feature = "unique")]?
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.
Maybe. Even if this impl
is usable, it can’t be used if you can’t get a Unique<T>
value in the first place on stable Rust. And anyway, IIRC stability attributes don’t work on impl
blocks at the moment. (They’re insta-stable if the relevant traits and types are stable.)
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.
Right, I was just afraid it might have other effects (e.g. like showing up in rustdoc). If not, I'm fine either way.
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.
Unstable impls are visible in rustdoc too: https://doc.rust-lang.org/std/ptr/struct.Shared.html#impl-From%3CUnique%3CT%3E%3E
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.
Alright, then.
|
However we already have |
Pushed another commit (that we can remove from the PR if it turns out problematic) that replaces |
I don't think it's a good idea to reintroduce an |
Sigh. This PR changes the expected result of an unrelated test (rustdoc seach results) that was added after the PR was created. Should be fixed now: diff --git a/src/test/rustdoc-js/from_u.js b/src/test/rustdoc-js/from_u.js
index 920620a9ae..0296788f7a 100644
--- a/src/test/rustdoc-js/from_u.js
+++ b/src/test/rustdoc-js/from_u.js
@@ -15,7 +15,6 @@ const EXPECTED = {
{ 'path': 'std::char', 'name': 'from_u32' },
{ 'path': 'std::str', 'name': 'from_utf8' },
{ 'path': 'std::string::String', 'name': 'from_utf8' },
- { 'path': 'std::boxed::Box', 'name': 'from_unique' },
{ 'path': 'std::i32', 'name': 'from_unsigned' },
{ 'path': 'std::i128', 'name': 'from_unsigned' },
], |
@bors r=alexcrichton |
📌 Commit 602a445 has been approved by |
Rename std::ptr::Shared to NonNull and stabilize it This implements the changes proposed at #27730 (comment): > * Rename `Shared<T>` to `NonNull<T>` and stabilize it. (Being in the `ptr` module is enough to say that it’s a pointer. I’m not very attached to this specific name though.) > * Rename `Box<T>` methods ~~`from_unique`~~/`into_unique` to ~~`from_nonnull`~~/`into_nonnull` (or whatever names are deemed appropriate), replace `Unique<T>` with `NonNull<T>` in their signatures, and stabilize them. > * Replace `Unique<T>` with `NonNull<T>` in the signatures of methods of the `Alloc` trait. > * Mark `Unique` “permanently-unstable” by replacing remaining occurrences of `#[unstable(feature = "unique", issue = "27730")]` with: > > ```rust > #[unstable(feature = "ptr_internals", issue = "0", reason = "\ > use NonNull instead and consider PhantomData<T> (if you also use #[may_dangle]), \ > Send, and/or Sync")] > ``` > > (Maybe the `reason` string is only useful on the struct definition.) Ideally it would be made private to some crate instead, but it needs to be used in both liballoc and libstd. > * (Leave `NonZero` and `Zeroable` unstable for now, and subject to future bikeshedding.)
☀️ Test successful - status-appveyor, status-travis |
\o/ you did it! |
Stop me if I should bring this up in a new issue, but how do folks feel about adding a conversion from |
@joshlf It should probably be a new issue (or a PR ;)) but I’ve been thinking the same when considering whether every |
Would've submitted a PR, but I see you've beaten me to it :)
…On Jan 20, 2018 11:34 PM, "Simon Sapin" ***@***.***> wrote:
@joshlf <https://github.com/joshlf> It should probably be a new issue (or
a PR ;)) but I’ve been thinking the same when considering whether every *mut
u8 in return types in the Alloc trait should be NonNull<u8> instead. So
for though I think the need is less pressing as long as there are few APIs
that return NonNull<_>: if you create it from a raw pointer anyway, you
can cast with as beforehand.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#46952 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AA_2L_mZdcvSPvPHjYXaz1V6BSo1rguWks5tMuiagaJpZM4RLVAQ>
.
|
Oops, I forgot to comment here about it. Yeah, since I was making other changes… :) |
… change more in the future due to rust-lang/rust#46952
Add some APIs to ptr::NonNull and fix `since` attributes This is a follow-up to its stabilization in rust-lang#46952. Tracking issue: rust-lang#27730. * These trait impls are insta-stable: `Hash`, `PartialEq`, `Eq`, `PartialOrd` and `Ord`. * The new `cast<U>() -> NonNull<U>` method is `#[unstable]`. It was proposed in rust-lang#46952 (comment).
Add Into<NonNull<T>> impls for Arc<T>/Rc<T> /cc @withoutboats who mentioned to me this might be worth adding to the standard library, in withoutboats/smart#4 /cc @kennytm who last year didn't love the idea of having an Into<NonNull<T>> for Box<T>, in #46952
This implements the changes proposed at #27730 (comment):