Struct hybrid_rc::Weak [−][src]
pub struct Weak<T: ?Sized> { /* fields omitted */ }
Expand description
Weak<T>
represents a non-owning reference to a value managed by a HybridRc<T, _>
.
The value is accessed by calling upgrade()
or upgrade_local()
on Weak
.
Weak
references are typically used to prevent circular references that would keep
the shared value alive indefinitely.
The typical way to obtain a Weak<T>
is to call HybridRc::downgrade()
.
Implementations
Returns a raw pointer to the value referenced by this Weak<T>
.
The pointer is valid only if there are some strong references. It may be dangling, unaligned or even null otherwise.
Example
use hybrid_rc::Rc;
let strong = Rc::new(42i32);
let weak = Rc::downgrade(&strong);
{
let pointer = weak.as_ptr();
// As long as strong is not dropped, the pointer stays valid
assert_eq!(42, unsafe { *pointer });
}
drop(strong);
{
// Calling weak.as_ptr() is still safe, but dereferencing it would lead
// to undefined behaviour.
let pointer = weak.as_ptr();
// assert_eq!(42, unsafe { &*pointer }); // undefined behaviour
}
Attempts to upgrade the Weak pointer to an Rc
.
Note: Only one thread can have Rc
s for a value at any point in time.
See upgrade()
to upgrade to an Arc
.
In no_std
environments this will only succeed if no Rc
exists on any thread.
Errors
ValueDropped
: the referenced value has already been dropped.WrongThread
: another thread currently holdsRc
s for the value.
Example
use hybrid_rc::{Arc, Rc, Weak, UpgradeError};
let strong = Arc::new(42i32);
let weak = Arc::downgrade(&strong);
{
let strong2 = weak.upgrade_local()?;
assert_eq!(Arc::as_ptr(&strong), Rc::as_ptr(&strong2));
}
std::mem::drop(strong);
let error = Weak::upgrade_local(&weak).unwrap_err();
assert_eq!(error, UpgradeError::ValueDropped);
Attempts to upgrade the Weak pointer to an Arc
.
Also see upgrade_local()
to upgrade to an Rc
.
Errors
ValueDropped
: the referenced value has already been dropped.
Gets a lower bound to the number of strong pointers to the inner value.
If self
was created using Weak::new
, this will return 0.
Please understand that another thread might change the count at any time, including potentially between calling this method and acting on the result.
Examples
use hybrid_rc::{Arc, Rc, Weak};
let reference = Rc::new(42);
let _2nd_ref = Rc::clone(&reference);
let shared_ref = Rc::to_shared(&reference);
let _2nd_shared_ref = Arc::clone(&shared_ref);
let weak = Rc::downgrade(&reference);
// shared_ref only knows the count of shared references and that there is at least one
// local reference, so it will show 3 instead of 4:
assert_eq!(Weak::strong_count(&weak), 3);
Gets the number of Weak
pointers to this allocation.
Please understand that another thread may change the count at any time, including potentially between calling this method and acting on the result. Also there might by off-by-one errors when other threads concurrently upgrade or downgrade pointers.
Examples
use hybrid_rc::{Rc, Weak};
let reference = Rc::new(42);
let weak = Rc::downgrade(&reference);
let _weak_2 = weak.clone();
assert_eq!(Weak::weak_count(&weak), 2);
Constructs a dummy Weak<T>
, without allocating any memory.
Trying to upgrade the result will always result in a ValueDropped
error.
Trait Implementations
Constructs a dummy Weak<T>
, without allocating any memory.
See Weak<T>::new()
.
Auto Trait Implementations
Blanket Implementations
Mutably borrows from an owned value. Read more