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: ?Sized> Weak<T>
impl<T: ?Sized> Weak<T>
pub fn into_std(self) -> StdWeak<T>
pub fn from_std(w: StdWeak<T>) -> Self
Sourcepub fn strong_count(&self) -> usize
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.
Sourcepub fn weak_count(&self) -> usize
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.
Sourcepub fn ptr_eq(&self, other: &Self) -> bool
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()).