From 7a786d453519cd5fb73c16a5e0266fa1a864ffe0 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 19 Dec 2018 23:18:02 +0000 Subject: [PATCH 1/3] 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 2/3] 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 3/3] 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); +}