sovran_arc::arcm

Struct WeakArcm

Source
pub struct WeakArcm<T: Clone> { /* private fields */ }
Expand description

A weak reference wrapper for Arcm

Implementations§

Source§

impl<T: Clone> WeakArcm<T>

Source

pub fn modify<F, R>(&self, f: F) -> Option<R>
where F: FnOnce(&mut T) -> R,

Attempts to modify the value if the original Arcm still exists

Examples found in repository?
examples/arcm_example.rs (line 22)
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());
}
Source

pub fn value(&self) -> Option<T>

Attempts to get a copy of the value if the original Arcm still exists

Examples found in repository?
examples/arcm_example.rs (line 19)
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§

Source§

impl<T: Clone> Debug for WeakArcm<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for WeakArcm<T>

§

impl<T> RefUnwindSafe for WeakArcm<T>

§

impl<T> Send for WeakArcm<T>
where T: Send,

§

impl<T> Sync for WeakArcm<T>
where T: Send,

§

impl<T> Unpin for WeakArcm<T>

§

impl<T> UnwindSafe for WeakArcm<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> 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, 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.