Weak

Struct Weak 

Source
pub struct Weak<T: ?Sized>(/* 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> Weak<T>

Source

pub fn new() -> Weak<T>

Constructs a new Weak<T>, without allocating any memory. Calling upgrade on the return value always gives None.

Source§

impl<T: ?Sized> Weak<T>

Source

pub fn into_std(self) -> StdWeak<T>

Source

pub fn from_std(w: StdWeak<T>) -> Self

Source

pub fn upgrade(&self) -> Option<Arc<T>>

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.

Source

pub fn strong_count(&self) -> usize

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

If self was created using Weak::new, this will return 0.

Source

pub fn weak_count(&self) -> usize

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

If self was created using Weak::new, or if there are no remaining strong pointers, this will return 0.

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

Source

pub fn ptr_eq(&self, other: &Self) -> bool

Returns true if the two Weaks point to the same allocation (similar to std::ptr::eq), or if both don’t point to any allocation (because they were created with Weak::new()).

Trait Implementations§

Source§

impl<T: Clone + ?Sized> Clone for Weak<T>

Source§

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

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: ?Sized + Debug> Debug for Weak<T>

Source§

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

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

impl<T: Default + ?Sized> Default for Weak<T>

Source§

fn default() -> Weak<T>

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Weak<T>
where 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.