diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-01-31 02:10:44 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-31 02:10:44 +0100 |
| commit | e8173a654db41b5b3615cab052a60edb1ac63f08 (patch) | |
| tree | 0c2a5f784ef94bf628ee538e3e139ce7d44709d9 /src/liballoc | |
| parent | 01346563c1d64f666920abc79389d0db5531989e (diff) | |
| parent | 1e577269da046b5e2b862830b72210c855fca123 (diff) | |
| download | rust-e8173a654db41b5b3615cab052a60edb1ac63f08.tar.gz rust-e8173a654db41b5b3615cab052a60edb1ac63f08.zip | |
Rollup merge of #57934 - dwijnand:from-Arc/Rc-to-NonNull, r=alexcrichton
Introduce into_raw_non_null on Rc and Arc None
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/rc.rs | 21 | ||||
| -rw-r--r-- | src/liballoc/sync.rs | 21 |
2 files changed, 42 insertions, 0 deletions
diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index c0dc010fe59..5efb74bc120 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -433,6 +433,27 @@ impl<T: ?Sized> Rc<T> { } } + /// Consumes the `Rc`, returning the wrapped pointer as `NonNull<T>`. + /// + /// # 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<T> { + // 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 390a0791650..5cffa93db11 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -413,6 +413,27 @@ impl<T: ?Sized> Arc<T> { } } + /// Consumes the `Arc`, returning the wrapped pointer as `NonNull<T>`. + /// + /// # 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<T> { + // 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 |
