Skip to content

Commit

Permalink
Rollup merge of rust-lang#57934 - dwijnand:from-Arc/Rc-to-NonNull, r=…
Browse files Browse the repository at this point in the history
…alexcrichton

Introduce into_raw_non_null on Rc and Arc

None
  • Loading branch information
Centril committed Jan 30, 2019
2 parents 8950561 + 1e57726 commit f255361
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions src/liballoc/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f255361

Please sign in to comment.