Skip to content

Commit d4adb3a

Browse files
committed
Add examples for unsized {Rc,Arc}::from_raw
1 parent 57e0dea commit d4adb3a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

library/alloc/src/rc.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,20 @@ impl<T: ?Sized> Rc<T> {
12251225
///
12261226
/// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling!
12271227
/// ```
1228+
///
1229+
/// Convert a slice back into its original array:
1230+
///
1231+
/// ```
1232+
/// use std::rc::Rc;
1233+
///
1234+
/// let x: Rc<[u32]> = Rc::new([1, 2, 3]);
1235+
/// let x_ptr: *const [u32] = Rc::into_raw(x);
1236+
///
1237+
/// unsafe {
1238+
/// let x: Rc<[u32; 3]> = Rc::from_raw(x_ptr.cast::<[u32; 3]>())
1239+
/// assert_eq!(x.as_ref(), &[1, 2, 3]);
1240+
/// }
1241+
/// ```
12281242
#[inline]
12291243
#[stable(feature = "rc_raw", since = "1.17.0")]
12301244
pub unsafe fn from_raw(ptr: *const T) -> Self {

library/alloc/src/sync.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,20 @@ impl<T: ?Sized> Arc<T> {
13711371
///
13721372
/// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling!
13731373
/// ```
1374+
///
1375+
/// Convert a slice back into its original array:
1376+
///
1377+
/// ```
1378+
/// use std::sync::Arc;
1379+
///
1380+
/// let x: Arc<[u32]> = Arc::new([1, 2, 3]);
1381+
/// let x_ptr: *const [u32] = Arc::into_raw(x);
1382+
///
1383+
/// unsafe {
1384+
/// let x: Arc<[u32; 3]> = Arc::from_raw(x_ptr.cast::<[u32; 3]>())
1385+
/// assert_eq!(x.as_ref(), &[1, 2, 3]);
1386+
/// }
1387+
/// ```
13741388
#[inline]
13751389
#[stable(feature = "rc_raw", since = "1.17.0")]
13761390
pub unsafe fn from_raw(ptr: *const T) -> Self {

0 commit comments

Comments
 (0)