Arcm

Struct Arcm 

Source
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>

Source

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}
Source

pub fn modify<F, R>(&self, f: F) -> R
where F: FnOnce(&mut T) -> 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}
Source

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}
Source

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}
Source

pub fn replace(&self, value: T) -> T

Replace the value without cloning the old one, returns the old value.

Trait Implementations§

Source§

impl<T: Clone> Clone for Arcm<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Clone + Debug> Debug for Arcm<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Clone + Default> Default for Arcm<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Clone> From<T> for Arcm<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.