Skip to content

Commit e9de86c

Browse files
authored
Rollup merge of #57934 - dwijnand:from-Arc/Rc-to-NonNull, r=alexcrichton
Introduce into_raw_non_null on Rc and Arc None
2 parents 10ad5a9 + 1e57726 commit e9de86c

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
@@ -433,6 +433,27 @@ impl<T: ?Sized> Rc<T> {
433433
}
434434
}
435435

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