about summary refs log tree commit diff
path: root/library/alloc/src/rc
diff options
context:
space:
mode:
authorKevin Reid <kpreid@switchb.org>2023-09-24 10:57:54 -0700
committerKevin Reid <kpreid@switchb.org>2024-06-22 08:08:00 -0700
commit88c3db57e4c3cda4e446999202171d9a459069a9 (patch)
tree9c0ece41ecf4386e0207bceb6c68981e17186987 /library/alloc/src/rc
parenta9a4830d257cd1563687448b31638b20ebbfba46 (diff)
downloadrust-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/src/rc')
-rw-r--r--library/alloc/src/rc/tests.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/library/alloc/src/rc/tests.rs b/library/alloc/src/rc/tests.rs
index 0f09be7721f..5e2e4beb94a 100644
--- a/library/alloc/src/rc/tests.rs
+++ b/library/alloc/src/rc/tests.rs
@@ -316,6 +316,24 @@ fn test_cowrc_clone_weak() {
     assert!(cow1_weak.upgrade().is_none());
 }
 
+/// This is similar to the doc-test for `Rc::make_mut()`, but on an unsized type (slice).
+#[test]
+fn test_cowrc_unsized() {
+    use std::rc::Rc;
+
+    let mut data: Rc<[i32]> = Rc::new([10, 20, 30]);
+
+    Rc::make_mut(&mut data)[0] += 1; // Won't clone anything
+    let mut other_data = Rc::clone(&data); // Won't clone inner data
+    Rc::make_mut(&mut data)[1] += 1; // Clones inner data
+    Rc::make_mut(&mut data)[2] += 1; // Won't clone anything
+    Rc::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]);
+}
+
 #[test]
 fn test_show() {
     let foo = Rc::new(75);