1
Fork 0

Introduce into_raw_non_null on Rc and Arc

This commit is contained in:
Dale Wijnand 2019-01-27 17:03:03 +00:00
parent 1484d0d123
commit 1e577269da
No known key found for this signature in database
GPG key ID: 4F256E3D151DF5EF
2 changed files with 42 additions and 0 deletions

View file

@ -435,6 +435,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

View file

@ -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