about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJohn-John Tedro <udoprog@tedro.se>2024-01-28 16:07:07 +0100
committerJohn-John Tedro <udoprog@tedro.se>2024-01-28 16:07:07 +0100
commitd4adb3af585daac5077c8852bc0b26bb661330c8 (patch)
treecc41e83ae89feb4bfcc058416373bf8cdbd7107a
parent57e0dea178eba21de2b001f731aeb95692ee84b9 (diff)
downloadrust-d4adb3af585daac5077c8852bc0b26bb661330c8.tar.gz
rust-d4adb3af585daac5077c8852bc0b26bb661330c8.zip
Add examples for unsized {Rc,Arc}::from_raw
-rw-r--r--library/alloc/src/rc.rs14
-rw-r--r--library/alloc/src/sync.rs14
2 files changed, 28 insertions, 0 deletions
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<T: ?Sized> Rc<T> {
     ///
     /// // 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<T: ?Sized> Arc<T> {
     ///
     /// // 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 {