From 1e577269da046b5e2b862830b72210c855fca123 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Sun, 27 Jan 2019 17:03:03 +0000 Subject: [PATCH] Introduce into_raw_non_null on Rc and Arc --- src/liballoc/rc.rs | 21 +++++++++++++++++++++ src/liballoc/sync.rs | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index d3a55c59ff69c..456f7cd5e2ff1 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -435,6 +435,27 @@ impl Rc { } } + /// Consumes the `Rc`, returning the wrapped pointer as `NonNull`. + /// + /// # Examples + /// + /// ``` + /// #![feature(rc_into_raw_non_null)] + /// + /// use std::rc::Rc; + /// + /// let x = Rc::new(10); + /// let ptr = Rc::into_raw_non_null(x); + /// let deref = unsafe { *ptr.as_ref() }; + /// assert_eq!(deref, 10); + /// ``` + #[unstable(feature = "rc_into_raw_non_null", issue = "47336")] + #[inline] + pub fn into_raw_non_null(this: Self) -> NonNull { + // safe because Rc guarantees its pointer is non-null + unsafe { NonNull::new_unchecked(Rc::into_raw(this) as *mut _) } + } + /// Creates a new [`Weak`][weak] pointer to this value. /// /// [weak]: struct.Weak.html diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 390a079165054..5cffa93db11d4 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -413,6 +413,27 @@ impl Arc { } } + /// Consumes the `Arc`, returning the wrapped pointer as `NonNull`. + /// + /// # Examples + /// + /// ``` + /// #![feature(rc_into_raw_non_null)] + /// + /// use std::sync::Arc; + /// + /// let x = Arc::new(10); + /// let ptr = Arc::into_raw_non_null(x); + /// let deref = unsafe { *ptr.as_ref() }; + /// assert_eq!(deref, 10); + /// ``` + #[unstable(feature = "rc_into_raw_non_null", issue = "47336")] + #[inline] + pub fn into_raw_non_null(this: Self) -> NonNull { + // safe because Arc guarantees its pointer is non-null + unsafe { NonNull::new_unchecked(Arc::into_raw(this) as *mut _) } + } + /// Creates a new [`Weak`][weak] pointer to this value. /// /// [weak]: struct.Weak.html