-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 Into<NonNull<T>> impls for Box<T>/Arc<T>/Rc<T> #56998
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
Thanks for the PR! This seems reasonable to me, but I'm double checking the rest of libstd for other idioms. It looks like this isn't implemented for The I think that leaves us with two routes to go here:
I'd ideally like to hear from @SimonSapin first to see if they remember why we didn't do |
So the options left are implementing |
#46952 (comment) mentions that we generally avoid methods that take Perhaps more importantly, #46952 (comment) says I undid that because of a
IIRC coherence does not allow this, since |
Yes, for box its a potentially breaking change because someone could have implemented I personally prefer using standard conversion methods here, especially since we already do that for references, to avoid users having to look up methods with names like |
I don't think coherence blocks this, so @dwijnand want to switch to |
It does when generics are involved: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=cff9ce9a3b1da269f92da22175236596 |
Yeah... Over on Zulip I've been pointed to https://github.com/rust-lang/rfcs/blob/master/text/2451-re-rebalancing-coherence.md, which points to #55437 and thus #56145. So if we want to use From and make Box work the same way, we could block this on that PR landing. |
I guess this is the remaining concern with an |
Oh oops, my mistake! @dwijnand want to switch to I'll hold of on formulating whether it's a good idea until we've tested it on crater :) |
I'll also note that there's an active lang RFC to change coherence in a way that I think would allow |
This comment has been minimized.
This comment has been minimized.
As far as I understand, As to the initial |
Is it perhaps related to my usage of |
Anyways, thank you @SimonSapin for unblocking me, I've pushed a commit that seems to work for my test cases. |
@bors: try |
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
@bors: retry |
@craterbot run start=master#ec194646fef1a467073ad74b8b68f6f202cfce97 end=try#185e0799ac53f1168549e11c4fc64ebf18b4a2c1 mode=check-only |
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
This comment has been minimized.
This comment has been minimized.
Travis CI is green but is showing yellow/pending. Re-syncing... |
Oh. Oops, didn't know rfcbot would do that :-/ |
@rfcbot fcp merge |
Team member @SimonSapin has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
@@ -201,6 +203,7 @@ impl<T: ?Sized> Box<T> { | |||
/// ``` | |||
#[unstable(feature = "box_into_raw_non_null", issue = "47336")] | |||
#[inline] | |||
#[rustc_deprecated(reason = "Use `.into()`", since = "1.32.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.
I would prefer not to deprecated this. Inherent associated functions are more discoverable and avoid needing to guide type inference, as you see in the into_raw
implementation above.
Box::into_raw_non_null(b).as_ptr()
let p: NonNull<T> = b.into();
p.as_ptr()
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.
@SimonSapin suggested this. I'm happy either way.
Do we want one implementation to delegate to the other? And if yes, which 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.
@dtolnay Should every single impl of conversion traits have a corresponding inherent method? If not, what makes this one special?
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.
Perhaps the very fact that the From/Into traits are type conversions makes them special with regards to ergonomics and type inference. My understanding was that was one of the reasons for having both From and Into, i.e Foo::from
avoids result type inference, while bar.into()
method-syntax is nicer to use. Being forced by the coherence rules I think it might be worth keeping the associated function.
@@ -479,6 +482,15 @@ impl From<Box<str>> for Box<[u8]> { | |||
} | |||
} | |||
|
|||
#[allow(incoherent_fundamental_impls)] |
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.
Please add a comment explaining the implications of this.
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.
I don't know what they are. I'm happy to push comments if anyone has any suggestions!
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.
incoherent_fundamental_impls
is a C-future-compatibility
warning; those are essentially hard errors that have been temporarily lowered into warnings to give people time to migrate. Tho in this case it doesn't seem clearly specified in the issue what the warning actually does. However, there's a PR up that is actually making it into a hard error #49799. If this impl is admitted into stable Rust (i.e. through #[allow(incoherent_fundamental_impls)]
) then the warning cannot, AFAIK, be made into a hard error. That seems... bad.
cc @nikomatsakis @rust-lang/wg-traits
@@ -1179,6 +1179,14 @@ impl<T> From<Vec<T>> for Rc<[T]> { | |||
} | |||
} | |||
|
|||
#[unstable(feature = "rc_into_nonnull", reason = "newly added", issue = "0")] | |||
impl<T: ?Sized> Into<NonNull<T>> for Rc<T> { |
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.
I would like to add an equivalent inherent function like we have for Box: Rc::into_raw_non_null and Arc::into_raw_non_null.
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.
Sure. What is the "raw" for? Also, between the Into impl and the inherent function, should one delegate to the other?
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.
I'll wait for #56998 (comment) to be resolved first.
Like Simon explained for Box into NonNull.
Well that PR landed, so maybe this can be implemented using |
No, it can't. impl<T: ?Sized> From<Arc<T>> for NonNull<T> Can't compile in liballoc because |
@Centril was this triaged at the meeting, or did you change your mind on nominating it? |
@dwijnand It was. The gist of it is that you cannot do |
I see! Thanks for the info. I'll reformulate this proposal as |
Follow-up in #57934. |
/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> for Box, in #46952