diff options
| author | Kevin Reid <kpreid@switchb.org> | 2023-09-24 10:57:54 -0700 |
|---|---|---|
| committer | Kevin Reid <kpreid@switchb.org> | 2024-06-22 08:08:00 -0700 |
| commit | 88c3db57e4c3cda4e446999202171d9a459069a9 (patch) | |
| tree | 9c0ece41ecf4386e0207bceb6c68981e17186987 /library/alloc/tests | |
| parent | a9a4830d257cd1563687448b31638b20ebbfba46 (diff) | |
| download | rust-88c3db57e4c3cda4e446999202171d9a459069a9.tar.gz rust-88c3db57e4c3cda4e446999202171d9a459069a9.zip | |
Generalize `{Rc,Arc}::make_mut()` to unsized types.
This requires introducing a new internal type `RcUninit` (and `ArcUninit`), which can own an `RcBox<T>` without requiring it to be initialized, sized, or a slice. This is similar to `UniqueRc`, but `UniqueRc` doesn't support the allocator parameter, and there is no `UniqueArc`.
Diffstat (limited to 'library/alloc/tests')
| -rw-r--r-- | library/alloc/tests/arc.rs | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/library/alloc/tests/arc.rs b/library/alloc/tests/arc.rs index d564a30b103..c37a80dca95 100644 --- a/library/alloc/tests/arc.rs +++ b/library/alloc/tests/arc.rs @@ -209,3 +209,21 @@ fn weak_may_dangle() { // `val` dropped here while still borrowed // borrow might be used here, when `val` is dropped and runs the `Drop` code for type `std::sync::Weak` } + +/// This is similar to the doc-test for `Arc::make_mut()`, but on an unsized type (slice). +#[test] +fn make_mut_unsized() { + use alloc::sync::Arc; + + let mut data: Arc<[i32]> = Arc::new([10, 20, 30]); + + Arc::make_mut(&mut data)[0] += 1; // Won't clone anything + let mut other_data = Arc::clone(&data); // Won't clone inner data + Arc::make_mut(&mut data)[1] += 1; // Clones inner data + Arc::make_mut(&mut data)[2] += 1; // Won't clone anything + Arc::make_mut(&mut other_data)[0] *= 10; // Won't clone anything + + // Now `data` and `other_data` point to different allocations. + assert_eq!(*data, [11, 21, 31]); + assert_eq!(*other_data, [110, 20, 30]); +} |
