From d4adb3af585daac5077c8852bc0b26bb661330c8 Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Sun, 28 Jan 2024 16:07:07 +0100 Subject: [PATCH] Add examples for unsized {Rc,Arc}::from_raw --- library/alloc/src/rc.rs | 14 ++++++++++++++ library/alloc/src/sync.rs | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 5e85746656e..0a959738b97 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -1225,6 +1225,20 @@ impl Rc { /// /// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling! /// ``` + /// + /// Convert a slice back into its original array: + /// + /// ``` + /// use std::rc::Rc; + /// + /// let x: Rc<[u32]> = Rc::new([1, 2, 3]); + /// let x_ptr: *const [u32] = Rc::into_raw(x); + /// + /// unsafe { + /// let x: Rc<[u32; 3]> = Rc::from_raw(x_ptr.cast::<[u32; 3]>()) + /// assert_eq!(x.as_ref(), &[1, 2, 3]); + /// } + /// ``` #[inline] #[stable(feature = "rc_raw", since = "1.17.0")] pub unsafe fn from_raw(ptr: *const T) -> Self { diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 95cf2da0ecf..708f11edc44 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -1371,6 +1371,20 @@ impl Arc { /// /// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling! /// ``` + /// + /// Convert a slice back into its original array: + /// + /// ``` + /// use std::sync::Arc; + /// + /// let x: Arc<[u32]> = Arc::new([1, 2, 3]); + /// let x_ptr: *const [u32] = Arc::into_raw(x); + /// + /// unsafe { + /// let x: Arc<[u32; 3]> = Arc::from_raw(x_ptr.cast::<[u32; 3]>()) + /// assert_eq!(x.as_ref(), &[1, 2, 3]); + /// } + /// ``` #[inline] #[stable(feature = "rc_raw", since = "1.17.0")] pub unsafe fn from_raw(ptr: *const T) -> Self {