pub struct Arcm<T: Clone> { /* private fields */ }Expand description
A wrapper combining Arc and Mutex for convenient shared mutable access Only works with types that implement Clone
Implementations§
Source§impl<T: Clone> Arcm<T>
impl<T: Clone> Arcm<T>
Sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
Creates a new Arcm containing the given value
Examples found in repository?
examples/arcm_example.rs (line 4)
3fn main() {
4 let v = Arcm::new(42);
5 println!("v = {:?}", v);
6 println!("v.value() = {}", v.value());
7
8 v.modify(|x| *x += 1);
9 println!("v = {:?}", v);
10 println!("v.value() = {}", v.value());
11
12 v.modify(|x| *x *= 2);
13 println!("v = {:?}", v);
14 println!("v.value() = {}", v.value());
15
16 // Create a weak reference
17 let weak = v.downgrade();
18 println!("weak = {:?}", weak);
19 println!("weak.value() = {:?}", weak.value());
20
21 // Modify through weak reference
22 weak.modify(|x| *x += 10);
23 println!("After weak modify, v.value() = {}", v.value());
24
25 // Demonstrate what happens when strong reference is dropped
26 drop(v);
27 println!(
28 "After dropping strong ref, weak.value() = {:?}",
29 weak.value()
30 );
31
32 // Examples of From and Into
33 println!("\n=== From and Into Examples ===");
34
35 // Using From
36 let v2 = Arcm::from(100);
37 println!("\nUsing From:");
38 println!("v2 = {:?}", v2);
39 println!("v2.value() = {}", v2.value());
40
41 // Using Into
42 let v3: Arcm<i32> = 200.into();
43 println!("\nUsing Into:");
44 println!("v3 = {:?}", v3);
45 println!("v3.value() = {}", v3.value());
46
47 // Using Into with String
48 let str_arcm: Arcm<String> = "Hello, World!".to_string().into();
49 println!("\nUsing Into with String:");
50 println!("str_arcm = {:?}", str_arcm);
51 println!("str_arcm.value() = {}", str_arcm.value());
52
53 // Using Into with Vec
54 let vec_arcm: Arcm<Vec<i32>> = vec![1, 2, 3].into();
55 println!("\nUsing Into with Vec:");
56 println!("vec_arcm = {:?}", vec_arcm);
57 println!("vec_arcm.value() = {:?}", vec_arcm.value());
58}Sourcepub fn modify<F, R>(&self, f: F) -> R
pub fn modify<F, R>(&self, f: F) -> R
Modifies the contained value using the provided closure
Examples found in repository?
examples/arcm_example.rs (line 8)
3fn main() {
4 let v = Arcm::new(42);
5 println!("v = {:?}", v);
6 println!("v.value() = {}", v.value());
7
8 v.modify(|x| *x += 1);
9 println!("v = {:?}", v);
10 println!("v.value() = {}", v.value());
11
12 v.modify(|x| *x *= 2);
13 println!("v = {:?}", v);
14 println!("v.value() = {}", v.value());
15
16 // Create a weak reference
17 let weak = v.downgrade();
18 println!("weak = {:?}", weak);
19 println!("weak.value() = {:?}", weak.value());
20
21 // Modify through weak reference
22 weak.modify(|x| *x += 10);
23 println!("After weak modify, v.value() = {}", v.value());
24
25 // Demonstrate what happens when strong reference is dropped
26 drop(v);
27 println!(
28 "After dropping strong ref, weak.value() = {:?}",
29 weak.value()
30 );
31
32 // Examples of From and Into
33 println!("\n=== From and Into Examples ===");
34
35 // Using From
36 let v2 = Arcm::from(100);
37 println!("\nUsing From:");
38 println!("v2 = {:?}", v2);
39 println!("v2.value() = {}", v2.value());
40
41 // Using Into
42 let v3: Arcm<i32> = 200.into();
43 println!("\nUsing Into:");
44 println!("v3 = {:?}", v3);
45 println!("v3.value() = {}", v3.value());
46
47 // Using Into with String
48 let str_arcm: Arcm<String> = "Hello, World!".to_string().into();
49 println!("\nUsing Into with String:");
50 println!("str_arcm = {:?}", str_arcm);
51 println!("str_arcm.value() = {}", str_arcm.value());
52
53 // Using Into with Vec
54 let vec_arcm: Arcm<Vec<i32>> = vec![1, 2, 3].into();
55 println!("\nUsing Into with Vec:");
56 println!("vec_arcm = {:?}", vec_arcm);
57 println!("vec_arcm.value() = {:?}", vec_arcm.value());
58}Sourcepub fn value(&self) -> T
pub fn value(&self) -> T
Returns a copy of the contained value
Examples found in repository?
examples/arcm_example.rs (line 6)
3fn main() {
4 let v = Arcm::new(42);
5 println!("v = {:?}", v);
6 println!("v.value() = {}", v.value());
7
8 v.modify(|x| *x += 1);
9 println!("v = {:?}", v);
10 println!("v.value() = {}", v.value());
11
12 v.modify(|x| *x *= 2);
13 println!("v = {:?}", v);
14 println!("v.value() = {}", v.value());
15
16 // Create a weak reference
17 let weak = v.downgrade();
18 println!("weak = {:?}", weak);
19 println!("weak.value() = {:?}", weak.value());
20
21 // Modify through weak reference
22 weak.modify(|x| *x += 10);
23 println!("After weak modify, v.value() = {}", v.value());
24
25 // Demonstrate what happens when strong reference is dropped
26 drop(v);
27 println!(
28 "After dropping strong ref, weak.value() = {:?}",
29 weak.value()
30 );
31
32 // Examples of From and Into
33 println!("\n=== From and Into Examples ===");
34
35 // Using From
36 let v2 = Arcm::from(100);
37 println!("\nUsing From:");
38 println!("v2 = {:?}", v2);
39 println!("v2.value() = {}", v2.value());
40
41 // Using Into
42 let v3: Arcm<i32> = 200.into();
43 println!("\nUsing Into:");
44 println!("v3 = {:?}", v3);
45 println!("v3.value() = {}", v3.value());
46
47 // Using Into with String
48 let str_arcm: Arcm<String> = "Hello, World!".to_string().into();
49 println!("\nUsing Into with String:");
50 println!("str_arcm = {:?}", str_arcm);
51 println!("str_arcm.value() = {}", str_arcm.value());
52
53 // Using Into with Vec
54 let vec_arcm: Arcm<Vec<i32>> = vec![1, 2, 3].into();
55 println!("\nUsing Into with Vec:");
56 println!("vec_arcm = {:?}", vec_arcm);
57 println!("vec_arcm.value() = {:?}", vec_arcm.value());
58}Sourcepub fn downgrade(&self) -> WeakArcm<T>
pub fn downgrade(&self) -> WeakArcm<T>
Returns a weak reference to the contained value
Examples found in repository?
examples/arcm_example.rs (line 17)
3fn main() {
4 let v = Arcm::new(42);
5 println!("v = {:?}", v);
6 println!("v.value() = {}", v.value());
7
8 v.modify(|x| *x += 1);
9 println!("v = {:?}", v);
10 println!("v.value() = {}", v.value());
11
12 v.modify(|x| *x *= 2);
13 println!("v = {:?}", v);
14 println!("v.value() = {}", v.value());
15
16 // Create a weak reference
17 let weak = v.downgrade();
18 println!("weak = {:?}", weak);
19 println!("weak.value() = {:?}", weak.value());
20
21 // Modify through weak reference
22 weak.modify(|x| *x += 10);
23 println!("After weak modify, v.value() = {}", v.value());
24
25 // Demonstrate what happens when strong reference is dropped
26 drop(v);
27 println!(
28 "After dropping strong ref, weak.value() = {:?}",
29 weak.value()
30 );
31
32 // Examples of From and Into
33 println!("\n=== From and Into Examples ===");
34
35 // Using From
36 let v2 = Arcm::from(100);
37 println!("\nUsing From:");
38 println!("v2 = {:?}", v2);
39 println!("v2.value() = {}", v2.value());
40
41 // Using Into
42 let v3: Arcm<i32> = 200.into();
43 println!("\nUsing Into:");
44 println!("v3 = {:?}", v3);
45 println!("v3.value() = {}", v3.value());
46
47 // Using Into with String
48 let str_arcm: Arcm<String> = "Hello, World!".to_string().into();
49 println!("\nUsing Into with String:");
50 println!("str_arcm = {:?}", str_arcm);
51 println!("str_arcm.value() = {}", str_arcm.value());
52
53 // Using Into with Vec
54 let vec_arcm: Arcm<Vec<i32>> = vec![1, 2, 3].into();
55 println!("\nUsing Into with Vec:");
56 println!("vec_arcm = {:?}", vec_arcm);
57 println!("vec_arcm.value() = {:?}", vec_arcm.value());
58}Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Arcm<T>
impl<T> RefUnwindSafe for Arcm<T>
impl<T> Send for Arcm<T>where
T: Send,
impl<T> Sync for Arcm<T>where
T: Send,
impl<T> Unpin for Arcm<T>
impl<T> UnwindSafe for Arcm<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more