-
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
Add Into<NonNull<T>> impls for Box<T>/Arc<T>/Rc<T> #56998
Changes from all commits
7a786d4
dd74e56
98b46ba
6faee57
93de0b6
8c1237b
fdb0236
8456019
b124169
243c841
0761aa2
d52a14e
d7d526b
0d0f2fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,7 +170,8 @@ impl<T: ?Sized> Box<T> { | |
#[stable(feature = "box_raw", since = "1.4.0")] | ||
#[inline] | ||
pub fn into_raw(b: Box<T>) -> *mut T { | ||
Box::into_raw_non_null(b).as_ptr() | ||
let p: NonNull<T> = b.into(); | ||
p.as_ptr() | ||
} | ||
|
||
/// Consumes the `Box`, returning the wrapped pointer as `NonNull<T>`. | ||
|
@@ -193,6 +194,7 @@ impl<T: ?Sized> Box<T> { | |
/// | ||
/// ``` | ||
/// #![feature(box_into_raw_non_null)] | ||
/// #![allow(deprecated)] | ||
/// | ||
/// fn main() { | ||
/// let x = Box::new(5); | ||
|
@@ -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")] | ||
pub fn into_raw_non_null(b: Box<T>) -> NonNull<T> { | ||
Box::into_unique(b).into() | ||
} | ||
|
@@ -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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
cc @nikomatsakis @rust-lang/wg-traits |
||
#[stable(feature = "box_into_non_null", since = "1.34.0")] | ||
impl<T: ?Sized> Into<NonNull<T>> for Box<T> { | ||
#[inline] | ||
fn into(self) -> NonNull<T> { | ||
Box::into_unique(self).into() | ||
} | ||
} | ||
|
||
impl Box<dyn Any> { | ||
#[inline] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,11 +316,11 @@ impl<T> Rc<T> { | |
// pointers, which ensures that the weak destructor never frees | ||
// the allocation while the strong destructor is running, even | ||
// if the weak pointer is stored inside the strong one. | ||
ptr: Box::into_raw_non_null(box RcBox { | ||
ptr: (box RcBox { | ||
strong: Cell::new(1), | ||
weak: Cell::new(1), | ||
value, | ||
}), | ||
}).into(), | ||
phantom: PhantomData, | ||
} | ||
} | ||
|
@@ -1179,6 +1179,14 @@ impl<T> From<Vec<T>> for Rc<[T]> { | |
} | ||
} | ||
|
||
#[stable(feature = "rc_into_nonnull", since = "1.34.0")] | ||
impl<T: ?Sized> Into<NonNull<T>> for Rc<T> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. I'll wait for #56998 (comment) to be resolved first. |
||
#[inline] | ||
fn into(self) -> NonNull<T> { | ||
unsafe { NonNull::new_unchecked(Rc::into_raw(self) as *mut _) } | ||
} | ||
} | ||
|
||
/// `Weak` is a version of [`Rc`] that holds a non-owning reference to the | ||
/// managed value. The value is accessed by calling [`upgrade`] on the `Weak` | ||
/// pointer, which returns an [`Option`]`<`[`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 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.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, whilebar.into()
method-syntax is nicer to use. Being forced by the coherence rules I think it might be worth keeping the associated function.