Weak

Struct Weak 

Source
pub struct Weak<T, A = Global>
where T: ?Sized, A: Allocator,
{ /* private fields */ }
Expand description

Weak is a version of Arc that holds a non-owning reference to the managed allocation.

The allocation is accessed by calling upgrade on the Weak pointer, which returns an Option<Arc<T>>.

Since a Weak reference does not count towards ownership, it will not prevent the value stored in the allocation from being dropped, and Weak itself makes no guarantees about the value still being present. Thus it may return None when upgraded. Note however that a Weak reference does prevent the allocation itself (the backing store) from being deallocated.

A Weak pointer is useful for keeping a temporary reference to the allocation managed by Arc without preventing its inner value from being dropped. It is also used to prevent circular references between Arc pointers, since mutual owning references would never allow either Arc to be dropped. For example, a tree could have strong Arc pointers from parent nodes to children, and Weak pointers from children back to their parents.

The typical way to obtain a Weak pointer is to call Arc::downgrade.

Implementations§

Source§

impl<T, A> Weak<T, A>
where T: ?Sized, A: Allocator,

Source

pub fn upgrade(&self) -> Option<Arc<T, A>>
where A: Clone,

Attempts to upgrade the Weak pointer to an Arc, delaying dropping of the inner value if successful.

Returns None if the inner value has since been dropped.

§Examples
use rune::sync::Arc;

let five = Arc::try_new(5)?;

let weak_five = Arc::downgrade(&five);

let strong_five: Option<Arc<_>> = weak_five.upgrade();
assert!(strong_five.is_some());

// Destroy all strong pointers.
drop(strong_five);
drop(five);

assert!(weak_five.upgrade().is_none());
Source

pub fn strong_count(&self) -> usize

Gets the number of strong (Arc) pointers pointing to this allocation.

Source

pub fn weak_count(&self) -> usize

Gets an approximation of the number of Weak pointers pointing to this allocation.

§Accuracy

Due to implementation details, the returned value can be off by 1 in either direction when other threads are manipulating any Arcs or Weaks pointing to the same allocation.

Trait Implementations§

Source§

impl<T, A> Clone for Weak<T, A>
where T: ?Sized, A: Allocator + Clone,

Source§

fn clone(&self) -> Weak<T, A>

Makes a clone of the Weak pointer that points to the same allocation.

§Examples
use rune::alloc::sync::{Arc, Weak};

let weak_five = Arc::downgrade(&Arc::try_new(5)?);

let _ = Weak::clone(&weak_five);
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T, A> Debug for Weak<T, A>
where T: ?Sized, A: Allocator,

Source§

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

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

impl<T, A> Drop for Weak<T, A>
where T: ?Sized, A: Allocator,

Source§

fn drop(&mut self)

Drops the Weak pointer.

§Examples
use rune::alloc::sync::{Arc, Weak};

struct Foo;

impl Drop for Foo {
    fn drop(&mut self) {
        println!("dropped!");
    }
}

let foo = Arc::try_new(Foo)?;
let weak_foo = Arc::downgrade(&foo);
let other_weak_foo = Weak::clone(&weak_foo);

drop(weak_foo);   // Doesn't print anything
drop(foo);        // Prints "dropped!"

assert!(other_weak_foo.upgrade().is_none());
Source§

impl<T, A> Send for Weak<T, A>
where T: ?Sized + Sync + Send, A: Allocator + Send,

Source§

impl<T, A> Sync for Weak<T, A>
where T: ?Sized + Sync + Send, A: Allocator + Sync,

Auto Trait Implementations§

§

impl<T, A> Freeze for Weak<T, A>
where A: Freeze, T: ?Sized,

§

impl<T, A> RefUnwindSafe for Weak<T, A>

§

impl<T, A> Unpin for Weak<T, A>
where A: Unpin, T: ?Sized,

§

impl<T, A> UnwindSafe for Weak<T, A>
where A: UnwindSafe, T: RefUnwindSafe + ?Sized,

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