diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 83adcce5c742c..e42b249d7919b 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -170,7 +170,8 @@ impl Box { #[stable(feature = "box_raw", since = "1.4.0")] #[inline] pub fn into_raw(b: Box) -> *mut T { - Box::into_raw_non_null(b).as_ptr() + let p: NonNull = b.into(); + p.as_ptr() } /// Consumes the `Box`, returning the wrapped pointer as `NonNull`. @@ -193,6 +194,7 @@ impl Box { /// /// ``` /// #![feature(box_into_raw_non_null)] + /// #![allow(deprecated)] /// /// fn main() { /// let x = Box::new(5); @@ -201,6 +203,7 @@ impl Box { /// ``` #[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) -> NonNull { Box::into_unique(b).into() } @@ -479,6 +482,15 @@ impl From> for Box<[u8]> { } } +#[allow(incoherent_fundamental_impls)] +#[stable(feature = "box_into_non_null", since = "1.34.0")] +impl Into> for Box { + #[inline] + fn into(self) -> NonNull { + Box::into_unique(self).into() + } +} + impl Box { #[inline] #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/liballoc/boxed_test.rs b/src/liballoc/boxed_test.rs index f340ea01c5f07..26a5de121c393 100644 --- a/src/liballoc/boxed_test.rs +++ b/src/liballoc/boxed_test.rs @@ -148,3 +148,11 @@ fn boxed_slice_from_iter() { assert_eq!(boxed.len(), 100); assert_eq!(boxed[7], 7); } + +#[test] +fn to_nonnull() { + let boxed: Box = Box::from(0); + let ptr: std::ptr::NonNull = boxed.into(); + let deref = unsafe { *ptr.as_ref() }; + assert_eq!(deref, 0); +} diff --git a/src/liballoc/collections/linked_list.rs b/src/liballoc/collections/linked_list.rs index ba46fafaf169f..2e1919794f34f 100644 --- a/src/liballoc/collections/linked_list.rs +++ b/src/liballoc/collections/linked_list.rs @@ -156,7 +156,7 @@ impl LinkedList { unsafe { node.next = self.head; node.prev = None; - let node = Some(Box::into_raw_non_null(node)); + let node = Some(node.into()); match self.head { None => self.tail = node, @@ -191,7 +191,7 @@ impl LinkedList { unsafe { node.next = None; node.prev = self.tail; - let node = Some(Box::into_raw_non_null(node)); + let node = Some(node.into()); match self.tail { None => self.head = node, @@ -933,11 +933,11 @@ impl<'a, T> IterMut<'a, T> { Some(prev) => prev, }; - let node = Some(Box::into_raw_non_null(box Node { + let node = Some((box Node { next: Some(head), prev: Some(prev), element, - })); + }).into()); prev.as_mut().next = node; head.as_mut().prev = node; diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 6769a70ddbe0a..548b47901fc61 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -316,11 +316,11 @@ impl Rc { // 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 From> for Rc<[T]> { } } +#[stable(feature = "rc_into_nonnull", since = "1.34.0")] +impl Into> for Rc { + #[inline] + fn into(self) -> NonNull { + 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`]`>`. diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index e596694fb9d4b..28940ee55d67b 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -300,7 +300,7 @@ impl Arc { weak: atomic::AtomicUsize::new(1), data, }; - Arc { ptr: Box::into_raw_non_null(x), phantom: PhantomData } + Arc { ptr: x.into(), phantom: PhantomData } } #[unstable(feature = "pin", issue = "49150")] @@ -1574,6 +1574,14 @@ impl From> for Arc<[T]> { } } +#[stable(feature = "arc_into_nonnull", since = "1.34.0")] +impl Into> for Arc { + #[inline] + fn into(self) -> NonNull { + unsafe { NonNull::new_unchecked(Arc::into_raw(self) as *mut _) } + } +} + #[cfg(test)] mod tests { use std::boxed::Box; diff --git a/src/liballoc/tests/arc.rs b/src/liballoc/tests/arc.rs index ec589710216c3..3604466703828 100644 --- a/src/liballoc/tests/arc.rs +++ b/src/liballoc/tests/arc.rs @@ -95,3 +95,10 @@ fn eq() { assert!(!(x != x)); assert_eq!(*x.0.borrow(), 0); } + +#[test] +fn to_nonnull() { + let ptr: std::ptr::NonNull = Arc::new(0).into(); + let deref = unsafe { *ptr.as_ref() }; + assert_eq!(deref, 0); +} diff --git a/src/liballoc/tests/rc.rs b/src/liballoc/tests/rc.rs index 02e1dfe13bb36..876da79d350d4 100644 --- a/src/liballoc/tests/rc.rs +++ b/src/liballoc/tests/rc.rs @@ -95,3 +95,10 @@ fn eq() { assert!(!(x != x)); assert_eq!(*x.0.borrow(), 0); } + +#[test] +fn to_nonnull() { + let ptr: std::ptr::NonNull = Rc::new(0).into(); + let deref = unsafe { *ptr.as_ref() }; + assert_eq!(deref, 0); +}