Skip to content

Commit eebc720

Browse files
committed
Replicate documentation in {Rc,Arc}::from_raw_in
1 parent d63384d commit eebc720

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed

Diff for: library/alloc/src/rc.rs

+34-9
Original file line numberDiff line numberDiff line change
@@ -1186,8 +1186,8 @@ impl<T: ?Sized> Rc<T> {
11861186
/// * If `U` is sized, it must have the same size and alignment as `T`. This
11871187
/// is trivially true if `U` is `T`.
11881188
/// * If `U` is unsized, its data pointer must have the same size and
1189-
/// alignment as `T`. This is trivially true if `Arc<U>` was constructed
1190-
/// through `Arc<T>` and then converted to `Arc<U>` through an [unsized
1189+
/// alignment as `T`. This is trivially true if `Rc<U>` was constructed
1190+
/// through `Rc<T>` and then converted to `Rc<U>` through an [unsized
11911191
/// coercion].
11921192
///
11931193
/// Note that if `U` or `U`'s data pointer is not `T` but has the same size
@@ -1363,13 +1363,20 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
13631363

13641364
/// Constructs an `Rc<T, A>` from a raw pointer in the provided allocator.
13651365
///
1366-
/// The raw pointer must have been previously returned by a call to
1367-
/// [`Rc<U, A>::into_raw`][into_raw] where `U` must have the same size
1368-
/// and alignment as `T`. This is trivially true if `U` is `T`.
1369-
/// Note that if `U` is not `T` but has the same size and alignment, this is
1370-
/// basically like transmuting references of different types. See
1371-
/// [`mem::transmute`] for more information on what
1372-
/// restrictions apply in this case.
1366+
/// The raw pointer must have been previously returned by a call to [`Rc<U,
1367+
/// A>::into_raw`][into_raw] with the following requirements:
1368+
///
1369+
/// * If `U` is sized, it must have the same size and alignment as `T`. This
1370+
/// is trivially true if `U` is `T`.
1371+
/// * If `U` is unsized, its data pointer must have the same size and
1372+
/// alignment as `T`. This is trivially true if `Rc<U>` was constructed
1373+
/// through `Rc<T>` and then converted to `Rc<U>` through an [unsized
1374+
/// coercion].
1375+
///
1376+
/// Note that if `U` or `U`'s data pointer is not `T` but has the same size
1377+
/// and alignment, this is basically like transmuting references of
1378+
/// different types. See [`mem::transmute`][transmute] for more information
1379+
/// on what restrictions apply in this case.
13731380
///
13741381
/// The raw pointer must point to a block of memory allocated by `alloc`
13751382
///
@@ -1380,6 +1387,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
13801387
/// even if the returned `Rc<T>` is never accessed.
13811388
///
13821389
/// [into_raw]: Rc::into_raw
1390+
/// [unsized coercion]: https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions
13831391
///
13841392
/// # Examples
13851393
///
@@ -1402,6 +1410,23 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
14021410
///
14031411
/// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling!
14041412
/// ```
1413+
///
1414+
/// Convert a slice back into its original array:
1415+
///
1416+
/// ```
1417+
/// #![feature(allocator_api)]
1418+
///
1419+
/// use std::rc::Rc;
1420+
/// use std::alloc::System;
1421+
///
1422+
/// let x: Rc<[u32]> = Rc::new_in([1, 2, 3], System);
1423+
/// let x_ptr: *const [u32] = Rc::into_raw(x);
1424+
///
1425+
/// unsafe {
1426+
/// let x: Rc<[u32; 3]> = Rc::from_raw_in(x_ptr.cast::<[u32; 3]>(), System);
1427+
/// assert_eq!(&*x, &[1, 2, 3]);
1428+
/// }
1429+
/// ```
14051430
#[unstable(feature = "allocator_api", issue = "32838")]
14061431
pub unsafe fn from_raw_in(ptr: *const T, alloc: A) -> Self {
14071432
let offset = unsafe { data_offset(ptr) };

Diff for: library/alloc/src/sync.rs

+32-7
Original file line numberDiff line numberDiff line change
@@ -1514,13 +1514,20 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
15141514

15151515
/// Constructs an `Arc<T, A>` from a raw pointer.
15161516
///
1517-
/// The raw pointer must have been previously returned by a call to
1518-
/// [`Arc<U, A>::into_raw`][into_raw] where `U` must have the same size and
1519-
/// alignment as `T`. This is trivially true if `U` is `T`.
1520-
/// Note that if `U` is not `T` but has the same size and alignment, this is
1521-
/// basically like transmuting references of different types. See
1522-
/// [`mem::transmute`] for more information on what
1523-
/// restrictions apply in this case.
1517+
/// The raw pointer must have been previously returned by a call to [`Arc<U,
1518+
/// A>::into_raw`][into_raw] with the following requirements:
1519+
///
1520+
/// * If `U` is sized, it must have the same size and alignment as `T`. This
1521+
/// is trivially true if `U` is `T`.
1522+
/// * If `U` is unsized, its data pointer must have the same size and
1523+
/// alignment as `T`. This is trivially true if `Arc<U>` was constructed
1524+
/// through `Arc<T>` and then converted to `Arc<U>` through an [unsized
1525+
/// coercion].
1526+
///
1527+
/// Note that if `U` or `U`'s data pointer is not `T` but has the same size
1528+
/// and alignment, this is basically like transmuting references of
1529+
/// different types. See [`mem::transmute`][transmute] for more information
1530+
/// on what restrictions apply in this case.
15241531
///
15251532
/// The raw pointer must point to a block of memory allocated by `alloc`
15261533
///
@@ -1531,6 +1538,7 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
15311538
/// even if the returned `Arc<T>` is never accessed.
15321539
///
15331540
/// [into_raw]: Arc::into_raw
1541+
/// [unsized coercion]: https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions
15341542
///
15351543
/// # Examples
15361544
///
@@ -1553,6 +1561,23 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
15531561
///
15541562
/// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling!
15551563
/// ```
1564+
///
1565+
/// Convert a slice back into its original array:
1566+
///
1567+
/// ```
1568+
/// #![feature(allocator_api)]
1569+
///
1570+
/// use std::sync::Arc;
1571+
/// use std::alloc::System;
1572+
///
1573+
/// let x: Arc<[u32]> = Arc::new_in([1, 2, 3], System);
1574+
/// let x_ptr: *const [u32] = Arc::into_raw(x);
1575+
///
1576+
/// unsafe {
1577+
/// let x: Arc<[u32; 3]> = Arc::from_raw_in(x_ptr.cast::<[u32; 3]>(), System);
1578+
/// assert_eq!(&*x, &[1, 2, 3]);
1579+
/// }
1580+
/// ```
15561581
#[inline]
15571582
#[unstable(feature = "allocator_api", issue = "32838")]
15581583
pub unsafe fn from_raw_in(ptr: *const T, alloc: A) -> Self {

0 commit comments

Comments
 (0)