Skip to content

Commit 1e57726

Browse files
committed
Introduce into_raw_non_null on Rc and Arc
1 parent 1484d0d commit 1e57726

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/liballoc/rc.rs

+21
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,27 @@ impl<T: ?Sized> Rc<T> {
435435
}
436436
}
437437

438+
/// Consumes the `Rc`, returning the wrapped pointer as `NonNull<T>`.
439+
///
440+
/// # Examples
441+
///
442+
/// ```
443+
/// #![feature(rc_into_raw_non_null)]
444+
///
445+
/// use std::rc::Rc;
446+
///
447+
/// let x = Rc::new(10);
448+
/// let ptr = Rc::into_raw_non_null(x);
449+
/// let deref = unsafe { *ptr.as_ref() };
450+
/// assert_eq!(deref, 10);
451+
/// ```
452+
#[unstable(feature = "rc_into_raw_non_null", issue = "47336")]
453+
#[inline]
454+
pub fn into_raw_non_null(this: Self) -> NonNull<T> {
455+
// safe because Rc guarantees its pointer is non-null
456+
unsafe { NonNull::new_unchecked(Rc::into_raw(this) as *mut _) }
457+
}
458+
438459
/// Creates a new [`Weak`][weak] pointer to this value.
439460
///
440461
/// [weak]: struct.Weak.html

src/liballoc/sync.rs

+21
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,27 @@ impl<T: ?Sized> Arc<T> {
413413
}
414414
}
415415

416+
/// Consumes the `Arc`, returning the wrapped pointer as `NonNull<T>`.
417+
///
418+
/// # Examples
419+
///
420+
/// ```
421+
/// #![feature(rc_into_raw_non_null)]
422+
///
423+
/// use std::sync::Arc;
424+
///
425+
/// let x = Arc::new(10);
426+
/// let ptr = Arc::into_raw_non_null(x);
427+
/// let deref = unsafe { *ptr.as_ref() };
428+
/// assert_eq!(deref, 10);
429+
/// ```
430+
#[unstable(feature = "rc_into_raw_non_null", issue = "47336")]
431+
#[inline]
432+
pub fn into_raw_non_null(this: Self) -> NonNull<T> {
433+
// safe because Arc guarantees its pointer is non-null
434+
unsafe { NonNull::new_unchecked(Arc::into_raw(this) as *mut _) }
435+
}
436+
416437
/// Creates a new [`Weak`][weak] pointer to this value.
417438
///
418439
/// [weak]: struct.Weak.html

0 commit comments

Comments
 (0)