From 7a786d453519cd5fb73c16a5e0266fa1a864ffe0 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 19 Dec 2018 23:18:02 +0000 Subject: [PATCH 01/14] Add Into> impls for Arc/Rc --- src/liballoc/rc.rs | 8 ++++++++ src/liballoc/sync.rs | 8 ++++++++ src/liballoc/tests/arc.rs | 5 +++++ src/liballoc/tests/rc.rs | 5 +++++ 4 files changed, 26 insertions(+) diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 6769a70ddbe0a..9b116b042ecb5 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -1179,6 +1179,14 @@ impl From> for Rc<[T]> { } } +#[unstable(feature = "rc_into_nonnull", reason = "newly added", issue = "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..d6d213355b60f 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -1574,6 +1574,14 @@ impl From> for Arc<[T]> { } } +#[unstable(feature = "arc_into_nonnull", reason = "newly added", issue = "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..8c19d704b3e58 100644 --- a/src/liballoc/tests/arc.rs +++ b/src/liballoc/tests/arc.rs @@ -95,3 +95,8 @@ fn eq() { assert!(!(x != x)); assert_eq!(*x.0.borrow(), 0); } + +#[test] +fn to_nonnull() { + let _: std::ptr::NonNull = Arc::new(0).into(); +} diff --git a/src/liballoc/tests/rc.rs b/src/liballoc/tests/rc.rs index 02e1dfe13bb36..0f618d1f9e6d8 100644 --- a/src/liballoc/tests/rc.rs +++ b/src/liballoc/tests/rc.rs @@ -95,3 +95,8 @@ fn eq() { assert!(!(x != x)); assert_eq!(*x.0.borrow(), 0); } + +#[test] +fn to_nonnull() { + let _: std::ptr::NonNull = Rc::new(0).into(); +} From dd74e564dbb1558bc53ae65290fbf27979898c71 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 19 Dec 2018 23:31:27 +0000 Subject: [PATCH 02/14] Add assert_eq to Arc/Rc -> NonNull test --- src/liballoc/tests/arc.rs | 4 +++- src/liballoc/tests/rc.rs | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/liballoc/tests/arc.rs b/src/liballoc/tests/arc.rs index 8c19d704b3e58..3604466703828 100644 --- a/src/liballoc/tests/arc.rs +++ b/src/liballoc/tests/arc.rs @@ -98,5 +98,7 @@ fn eq() { #[test] fn to_nonnull() { - let _: std::ptr::NonNull = Arc::new(0).into(); + 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 0f618d1f9e6d8..876da79d350d4 100644 --- a/src/liballoc/tests/rc.rs +++ b/src/liballoc/tests/rc.rs @@ -98,5 +98,7 @@ fn eq() { #[test] fn to_nonnull() { - let _: std::ptr::NonNull = Rc::new(0).into(); + let ptr: std::ptr::NonNull = Rc::new(0).into(); + let deref = unsafe { *ptr.as_ref() }; + assert_eq!(deref, 0); } From 98b46ba2132e0fef721c0b5ef6c1933c44df310e Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Sun, 23 Dec 2018 21:46:36 +0000 Subject: [PATCH 03/14] Add an Into> impl for Box --- src/liballoc/boxed.rs | 9 +++++++++ src/liballoc/boxed_test.rs | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 83adcce5c742c..40baf59b42ded 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -479,6 +479,15 @@ impl From> for Box<[u8]> { } } +#[allow(incoherent_fundamental_impls)] +#[unstable(feature = "box_into_raw_non_null", issue = "47336")] +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); +} From 6faee577de917918cfe6b74ef1de390bddd0316c Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Mon, 7 Jan 2019 17:27:19 +0000 Subject: [PATCH 04/14] Mark Into> for Box as #stable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From SimonSapin: > an impl cannot actually be unstable when both the trait and the > receiver type are stable. (The attribute doesn’t do anything.) --- src/liballoc/boxed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 40baf59b42ded..9bd5485c7dffb 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -480,7 +480,7 @@ impl From> for Box<[u8]> { } #[allow(incoherent_fundamental_impls)] -#[unstable(feature = "box_into_raw_non_null", issue = "47336")] +#[stable(feature = "box_into_raw_non_null", issue = "47336")] impl Into> for Box { #[inline] fn into(self) -> NonNull { From 93de0b69123d6f7f579d62b2e74195731a4d3600 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Mon, 7 Jan 2019 17:30:15 +0000 Subject: [PATCH 05/14] Deprecate Box::into_raw_non_null --- src/liballoc/boxed.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 9bd5485c7dffb..261bf05666c43 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -201,6 +201,7 @@ impl Box { /// ``` #[unstable(feature = "box_into_raw_non_null", issue = "47336")] #[inline] + #[deprecated(note = "Use `.into()`")] pub fn into_raw_non_null(b: Box) -> NonNull { Box::into_unique(b).into() } From 8c1237b2debb1970ebaa86f86da8ae8cb919e17b Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Mon, 7 Jan 2019 17:51:55 +0000 Subject: [PATCH 06/14] Add a "since" to #[stable] --- src/liballoc/boxed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 261bf05666c43..a43f40141aa5b 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -481,7 +481,7 @@ impl From> for Box<[u8]> { } #[allow(incoherent_fundamental_impls)] -#[stable(feature = "box_into_raw_non_null", issue = "47336")] +#[stable(feature = "box_into_raw_non_null", issue = "47336", since = "1.32.0")] impl Into> for Box { #[inline] fn into(self) -> NonNull { From fdb02361ebf4b82cece5339944fab8dc850b5fc0 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Mon, 7 Jan 2019 19:17:33 +0000 Subject: [PATCH 07/14] Use rustc_deprecated instead, for "staged api" --- src/liballoc/boxed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index a43f40141aa5b..6a9991f92f3a4 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -201,7 +201,7 @@ impl Box { /// ``` #[unstable(feature = "box_into_raw_non_null", issue = "47336")] #[inline] - #[deprecated(note = "Use `.into()`")] + #[rustc_deprecated(note = "Use `.into()`")] pub fn into_raw_non_null(b: Box) -> NonNull { Box::into_unique(b).into() } From 8456019f79a00ba9a77e42b60517ca15ff1bb247 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Mon, 7 Jan 2019 19:18:04 +0000 Subject: [PATCH 08/14] Drop "issue" in #[stable], new feature name... --- src/liballoc/boxed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 6a9991f92f3a4..e5dc1aaad2cef 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -481,7 +481,7 @@ impl From> for Box<[u8]> { } #[allow(incoherent_fundamental_impls)] -#[stable(feature = "box_into_raw_non_null", issue = "47336", since = "1.32.0")] +#[stable(feature = "box_into_non_null", since = "1.32.0")] impl Into> for Box { #[inline] fn into(self) -> NonNull { From b1241693599980d9a528afb8fe70474e665444f1 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Mon, 7 Jan 2019 21:22:55 +0000 Subject: [PATCH 09/14] Give rustc_deprecated a "since" version --- src/liballoc/boxed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index e5dc1aaad2cef..9ba4dc4e32f76 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -201,7 +201,7 @@ impl Box { /// ``` #[unstable(feature = "box_into_raw_non_null", issue = "47336")] #[inline] - #[rustc_deprecated(note = "Use `.into()`")] + #[rustc_deprecated(note = "Use `.into()`", since = "1.32.0")] pub fn into_raw_non_null(b: Box) -> NonNull { Box::into_unique(b).into() } From 243c8417d3f3e450bff190ff5ab5b5998ce775e3 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 8 Jan 2019 06:46:01 +0000 Subject: [PATCH 10/14] Replace use of "note" with "reason" with rustc_deprecated --- src/liballoc/boxed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 9ba4dc4e32f76..cf3b8efd72000 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -201,7 +201,7 @@ impl Box { /// ``` #[unstable(feature = "box_into_raw_non_null", issue = "47336")] #[inline] - #[rustc_deprecated(note = "Use `.into()`", since = "1.32.0")] + #[rustc_deprecated(reason = "Use `.into()`", since = "1.32.0")] pub fn into_raw_non_null(b: Box) -> NonNull { Box::into_unique(b).into() } From 0761aa20a8b84de64786bdf16d236d028ee0c728 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 8 Jan 2019 08:22:10 +0000 Subject: [PATCH 11/14] Remove usage of deprecated Box::into_raw_non_null --- src/liballoc/boxed.rs | 3 ++- src/liballoc/collections/linked_list.rs | 8 ++++---- src/liballoc/rc.rs | 4 ++-- src/liballoc/sync.rs | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index cf3b8efd72000..f90ecba8160ed 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`. 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 9b116b042ecb5..0bfd5f65ebde4 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, } } diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index d6d213355b60f..58ce9d085b323 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")] From d52a14e0ab24a962f3a71663e9126fd6de659d12 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 8 Jan 2019 12:39:39 +0000 Subject: [PATCH 12/14] Ashes to ashes, deprecated to deprecated --- src/liballoc/boxed.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index f90ecba8160ed..2612dda07f876 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -194,6 +194,7 @@ impl Box { /// /// ``` /// #![feature(box_into_raw_non_null)] + /// #![allow(deprecated)] /// /// fn main() { /// let x = Box::new(5); From d7d526bda2f7a71ea18ad7f9a02318c535040f6f Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 9 Jan 2019 17:21:12 +0000 Subject: [PATCH 13/14] Fix target version in "since" --- src/liballoc/boxed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 2612dda07f876..e42b249d7919b 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -483,7 +483,7 @@ impl From> for Box<[u8]> { } #[allow(incoherent_fundamental_impls)] -#[stable(feature = "box_into_non_null", since = "1.32.0")] +#[stable(feature = "box_into_non_null", since = "1.34.0")] impl Into> for Box { #[inline] fn into(self) -> NonNull { From 0d0f2fe75b98a928f37e85ac48dc87bcc39fd17e Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 9 Jan 2019 17:26:04 +0000 Subject: [PATCH 14/14] Mark Rc/Arc into NonNull as insta-stable Like Simon explained for Box into NonNull. --- src/liballoc/rc.rs | 2 +- src/liballoc/sync.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 0bfd5f65ebde4..548b47901fc61 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -1179,7 +1179,7 @@ impl From> for Rc<[T]> { } } -#[unstable(feature = "rc_into_nonnull", reason = "newly added", issue = "0")] +#[stable(feature = "rc_into_nonnull", since = "1.34.0")] impl Into> for Rc { #[inline] fn into(self) -> NonNull { diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 58ce9d085b323..28940ee55d67b 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -1574,7 +1574,7 @@ impl From> for Arc<[T]> { } } -#[unstable(feature = "arc_into_nonnull", reason = "newly added", issue = "0")] +#[stable(feature = "arc_into_nonnull", since = "1.34.0")] impl Into> for Arc { #[inline] fn into(self) -> NonNull {