@@ -84,9 +84,29 @@ macro_rules! acquire {
84
84
///
85
85
/// Shared references in Rust disallow mutation by default, and `Arc` is no
86
86
/// exception: you cannot generally obtain a mutable reference to something
87
- /// inside an `Arc`. If you need to mutate through an `Arc`, use
88
- /// [`Mutex`][mutex], [`RwLock`][rwlock], or one of the [`Atomic`][atomic]
89
- /// types.
87
+ /// inside an `Arc`. If you do need to mutate through an `Arc`, you have several options:
88
+ ///
89
+ /// 1. Use interior mutability with synchronization primitives like [`Mutex`][mutex],
90
+ /// [`RwLock`][rwlock], or one of the [`Atomic`][atomic] types.
91
+ ///
92
+ /// 2. Use clone-on-write semantics with [`Arc::make_mut`] which provides efficient mutation
93
+ /// without requiring interior mutability. This approach clones the data only when
94
+ /// needed (when there are multiple references) and can be more efficient when mutations
95
+ /// are infrequent.
96
+ ///
97
+ /// 3. Use [`Arc::get_mut`] when you know your `Arc` is not shared (has a reference count of 1),
98
+ /// which provides direct mutable access to the inner value without any cloning.
99
+ ///
100
+ /// ```
101
+ /// use std::sync::Arc;
102
+ ///
103
+ /// let mut data = Arc::new(vec![1, 2, 3]);
104
+ ///
105
+ /// // This will clone the vector only if there are other references to it
106
+ /// Arc::make_mut(&mut data).push(4);
107
+ ///
108
+ /// assert_eq!(*data, vec![1, 2, 3, 4]);
109
+ /// ```
90
110
///
91
111
/// **Note**: This type is only available on platforms that support atomic
92
112
/// loads and stores of pointers, which includes all platforms that support
@@ -1453,11 +1473,14 @@ impl<T: ?Sized> Arc<T> {
1453
1473
///
1454
1474
/// # Safety
1455
1475
///
1456
- /// The pointer must have been obtained through `Arc::into_raw`, and the
1457
- /// associated `Arc` instance must be valid (i.e. the strong count must be at
1476
+ /// The pointer must have been obtained through `Arc::into_raw` and must satisfy the
1477
+ /// same layout requirements specified in [`Arc::from_raw_in`][from_raw_in].
1478
+ /// The associated `Arc` instance must be valid (i.e. the strong count must be at
1458
1479
/// least 1) for the duration of this method, and `ptr` must point to a block of memory
1459
1480
/// allocated by the global allocator.
1460
1481
///
1482
+ /// [from_raw_in]: Arc::from_raw_in
1483
+ ///
1461
1484
/// # Examples
1462
1485
///
1463
1486
/// ```
@@ -1488,13 +1511,16 @@ impl<T: ?Sized> Arc<T> {
1488
1511
///
1489
1512
/// # Safety
1490
1513
///
1491
- /// The pointer must have been obtained through `Arc::into_raw`, and the
1492
- /// associated `Arc` instance must be valid (i.e. the strong count must be at
1514
+ /// The pointer must have been obtained through `Arc::into_raw` and must satisfy the
1515
+ /// same layout requirements specified in [`Arc::from_raw_in`][from_raw_in].
1516
+ /// The associated `Arc` instance must be valid (i.e. the strong count must be at
1493
1517
/// least 1) when invoking this method, and `ptr` must point to a block of memory
1494
1518
/// allocated by the global allocator. This method can be used to release the final
1495
1519
/// `Arc` and backing storage, but **should not** be called after the final `Arc` has been
1496
1520
/// released.
1497
1521
///
1522
+ /// [from_raw_in]: Arc::from_raw_in
1523
+ ///
1498
1524
/// # Examples
1499
1525
///
1500
1526
/// ```
@@ -1806,11 +1832,14 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
1806
1832
///
1807
1833
/// # Safety
1808
1834
///
1809
- /// The pointer must have been obtained through `Arc::into_raw`, and the
1810
- /// associated `Arc` instance must be valid (i.e. the strong count must be at
1811
- /// least 1) for the duration of this method,, and `ptr` must point to a block of memory
1835
+ /// The pointer must have been obtained through `Arc::into_raw` and must satisfy the
1836
+ /// same layout requirements specified in [`Arc::from_raw_in`][from_raw_in].
1837
+ /// The associated `Arc` instance must be valid (i.e. the strong count must be at
1838
+ /// least 1) for the duration of this method, and `ptr` must point to a block of memory
1812
1839
/// allocated by `alloc`.
1813
1840
///
1841
+ /// [from_raw_in]: Arc::from_raw_in
1842
+ ///
1814
1843
/// # Examples
1815
1844
///
1816
1845
/// ```
@@ -1850,13 +1879,16 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
1850
1879
///
1851
1880
/// # Safety
1852
1881
///
1853
- /// The pointer must have been obtained through `Arc::into_raw`, the
1854
- /// associated `Arc` instance must be valid (i.e. the strong count must be at
1882
+ /// The pointer must have been obtained through `Arc::into_raw` and must satisfy the
1883
+ /// same layout requirements specified in [`Arc::from_raw_in`][from_raw_in].
1884
+ /// The associated `Arc` instance must be valid (i.e. the strong count must be at
1855
1885
/// least 1) when invoking this method, and `ptr` must point to a block of memory
1856
1886
/// allocated by `alloc`. This method can be used to release the final
1857
1887
/// `Arc` and backing storage, but **should not** be called after the final `Arc` has been
1858
1888
/// released.
1859
1889
///
1890
+ /// [from_raw_in]: Arc::from_raw_in
1891
+ ///
1860
1892
/// # Examples
1861
1893
///
1862
1894
/// ```
0 commit comments