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)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
fn main() {
let v = Arcm::new(42);
println!("v = {:?}", v);
println!("v.value() = {}", v.value());
v.modify(|x| *x += 1);
println!("v = {:?}", v);
println!("v.value() = {}", v.value());
v.modify(|x| *x *= 2);
println!("v = {:?}", v);
println!("v.value() = {}", v.value());
// Create a weak reference
let weak = v.downgrade();
println!("weak = {:?}", weak);
println!("weak.value() = {:?}", weak.value());
// Modify through weak reference
weak.modify(|x| *x += 10);
println!("After weak modify, v.value() = {}", v.value());
// Demonstrate what happens when strong reference is dropped
drop(v);
println!("After dropping strong ref, weak.value() = {:?}", weak.value());
// Examples of From and Into
println!("\n=== From and Into Examples ===");
// Using From
let v2 = Arcm::from(100);
println!("\nUsing From:");
println!("v2 = {:?}", v2);
println!("v2.value() = {}", v2.value());
// Using Into
let v3: Arcm<i32> = 200.into();
println!("\nUsing Into:");
println!("v3 = {:?}", v3);
println!("v3.value() = {}", v3.value());
// Using Into with String
let str_arcm: Arcm<String> = "Hello, World!".to_string().into();
println!("\nUsing Into with String:");
println!("str_arcm = {:?}", str_arcm);
println!("str_arcm.value() = {}", str_arcm.value());
// Using Into with Vec
let vec_arcm: Arcm<Vec<i32>> = vec![1, 2, 3].into();
println!("\nUsing Into with Vec:");
println!("vec_arcm = {:?}", vec_arcm);
println!("vec_arcm.value() = {:?}", vec_arcm.value());
}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)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
fn main() {
let v = Arcm::new(42);
println!("v = {:?}", v);
println!("v.value() = {}", v.value());
v.modify(|x| *x += 1);
println!("v = {:?}", v);
println!("v.value() = {}", v.value());
v.modify(|x| *x *= 2);
println!("v = {:?}", v);
println!("v.value() = {}", v.value());
// Create a weak reference
let weak = v.downgrade();
println!("weak = {:?}", weak);
println!("weak.value() = {:?}", weak.value());
// Modify through weak reference
weak.modify(|x| *x += 10);
println!("After weak modify, v.value() = {}", v.value());
// Demonstrate what happens when strong reference is dropped
drop(v);
println!("After dropping strong ref, weak.value() = {:?}", weak.value());
// Examples of From and Into
println!("\n=== From and Into Examples ===");
// Using From
let v2 = Arcm::from(100);
println!("\nUsing From:");
println!("v2 = {:?}", v2);
println!("v2.value() = {}", v2.value());
// Using Into
let v3: Arcm<i32> = 200.into();
println!("\nUsing Into:");
println!("v3 = {:?}", v3);
println!("v3.value() = {}", v3.value());
// Using Into with String
let str_arcm: Arcm<String> = "Hello, World!".to_string().into();
println!("\nUsing Into with String:");
println!("str_arcm = {:?}", str_arcm);
println!("str_arcm.value() = {}", str_arcm.value());
// Using Into with Vec
let vec_arcm: Arcm<Vec<i32>> = vec![1, 2, 3].into();
println!("\nUsing Into with Vec:");
println!("vec_arcm = {:?}", vec_arcm);
println!("vec_arcm.value() = {:?}", vec_arcm.value());
}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)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
fn main() {
let v = Arcm::new(42);
println!("v = {:?}", v);
println!("v.value() = {}", v.value());
v.modify(|x| *x += 1);
println!("v = {:?}", v);
println!("v.value() = {}", v.value());
v.modify(|x| *x *= 2);
println!("v = {:?}", v);
println!("v.value() = {}", v.value());
// Create a weak reference
let weak = v.downgrade();
println!("weak = {:?}", weak);
println!("weak.value() = {:?}", weak.value());
// Modify through weak reference
weak.modify(|x| *x += 10);
println!("After weak modify, v.value() = {}", v.value());
// Demonstrate what happens when strong reference is dropped
drop(v);
println!("After dropping strong ref, weak.value() = {:?}", weak.value());
// Examples of From and Into
println!("\n=== From and Into Examples ===");
// Using From
let v2 = Arcm::from(100);
println!("\nUsing From:");
println!("v2 = {:?}", v2);
println!("v2.value() = {}", v2.value());
// Using Into
let v3: Arcm<i32> = 200.into();
println!("\nUsing Into:");
println!("v3 = {:?}", v3);
println!("v3.value() = {}", v3.value());
// Using Into with String
let str_arcm: Arcm<String> = "Hello, World!".to_string().into();
println!("\nUsing Into with String:");
println!("str_arcm = {:?}", str_arcm);
println!("str_arcm.value() = {}", str_arcm.value());
// Using Into with Vec
let vec_arcm: Arcm<Vec<i32>> = vec![1, 2, 3].into();
println!("\nUsing Into with Vec:");
println!("vec_arcm = {:?}", vec_arcm);
println!("vec_arcm.value() = {:?}", vec_arcm.value());
}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)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
fn main() {
let v = Arcm::new(42);
println!("v = {:?}", v);
println!("v.value() = {}", v.value());
v.modify(|x| *x += 1);
println!("v = {:?}", v);
println!("v.value() = {}", v.value());
v.modify(|x| *x *= 2);
println!("v = {:?}", v);
println!("v.value() = {}", v.value());
// Create a weak reference
let weak = v.downgrade();
println!("weak = {:?}", weak);
println!("weak.value() = {:?}", weak.value());
// Modify through weak reference
weak.modify(|x| *x += 10);
println!("After weak modify, v.value() = {}", v.value());
// Demonstrate what happens when strong reference is dropped
drop(v);
println!("After dropping strong ref, weak.value() = {:?}", weak.value());
// Examples of From and Into
println!("\n=== From and Into Examples ===");
// Using From
let v2 = Arcm::from(100);
println!("\nUsing From:");
println!("v2 = {:?}", v2);
println!("v2.value() = {}", v2.value());
// Using Into
let v3: Arcm<i32> = 200.into();
println!("\nUsing Into:");
println!("v3 = {:?}", v3);
println!("v3.value() = {}", v3.value());
// Using Into with String
let str_arcm: Arcm<String> = "Hello, World!".to_string().into();
println!("\nUsing Into with String:");
println!("str_arcm = {:?}", str_arcm);
println!("str_arcm.value() = {}", str_arcm.value());
// Using Into with Vec
let vec_arcm: Arcm<Vec<i32>> = vec![1, 2, 3].into();
println!("\nUsing Into with Vec:");
println!("vec_arcm = {:?}", vec_arcm);
println!("vec_arcm.value() = {:?}", vec_arcm.value());
}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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit)