Struct without_alloc::rc::Weak
source · [−]pub struct Weak<'a, T> { /* private fields */ }
Expand description
A reference-counting pointer to the allocation of an Rc
.
TODO
Evaluate an interface:
ⓘ
fn reinit(&self, val: T) -> Result<Rc<T>, T>;
Implementations
sourceimpl<'a, T> Weak<'a, T>
impl<'a, T> Weak<'a, T>
sourcepub fn try_unwrap(self) -> Result<Uninit<'a, ()>, Self>
pub fn try_unwrap(self) -> Result<Uninit<'a, ()>, Self>
Try to unwrap the original allocation of the Rc
.
This will only work when this is the only pointer to the allocation. That is, there are
neither Weak
nor Rc
still pointing at it.
Example
use without_alloc::{alloc::LocalAllocLeakExt, rc::Rc};
use static_alloc::Bump;
struct Foo;
let slab: Bump<[u8; 1024]> = Bump::uninit();
let rc = slab.rc(Foo).unwrap();
let (_, weak) = Rc::try_unwrap(rc).ok().unwrap();
// This is the only one pointing at the allocation.
let memory = weak.try_unwrap().ok().unwrap();
sourcepub fn upgrade(&self) -> Option<Rc<'a, T>>
pub fn upgrade(&self) -> Option<Rc<'a, T>>
Attempt to upgrade to a shared pointer to the value.
This operation will only succeed if there are still strong pointers to the value, i.e.
strong_count
is not zero. Then the value has not been dropped yet and its lifetime is
extended.
use without_alloc::{alloc::LocalAllocLeakExt, rc::Rc};
use static_alloc::Bump;
let memory: Bump<[u8; 1024]> = Bump::uninit();
let rc = memory.rc(0usize).unwrap();
let weak = Rc::downgrade(&rc);
let rc2 = weak.upgrade().unwrap();
drop(rc);
drop(rc2);
// No more strong pointers left.
assert!(weak.upgrade().is_none());
sourceimpl<T> Weak<'_, T>
impl<T> Weak<'_, T>
sourcepub fn strong_count(&self) -> usize
pub fn strong_count(&self) -> usize
Gets the number of strong pointers pointing at the value.
Example
use without_alloc::{alloc::LocalAllocLeakExt, rc::Rc, rc::Weak};
use static_alloc::Bump;
struct Foo;
let slab: Bump<[u8; 1024]> = Bump::uninit();
let rc = slab.rc(Foo).unwrap();
let (_, weak) = Rc::try_unwrap(rc).ok().unwrap();
// We just destroyed the only one.
assert_eq!(Weak::strong_count(&weak), 0);
sourcepub fn weak_count(&self) -> usize
pub fn weak_count(&self) -> usize
Gets the number of weak pointers pointing at the value.
Example
use without_alloc::{alloc::LocalAllocLeakExt, rc::Rc, rc::Weak};
use static_alloc::Bump;
struct Foo;
let slab: Bump<[u8; 1024]> = Bump::uninit();
let rc = slab.rc(Foo).unwrap();
let (_, weak) = Rc::try_unwrap(rc).ok().unwrap();
// This is the only one pointing at the allocation.
assert_eq!(Weak::weak_count(&weak), 1);
Trait Implementations
sourceimpl<T> Clone for Weak<'_, T>
impl<T> Clone for Weak<'_, T>
sourcefn clone(&self) -> Self
fn clone(&self) -> Self
Clone the Weak
.
This will increment the weak reference count.
Examples
use without_alloc::{alloc::LocalAllocLeakExt, rc::Rc};
use static_alloc::Bump;
struct Foo;
let slab: Bump<[u8; 1024]> = Bump::uninit();
let foo = slab.rc(Foo).unwrap();
let (_, weak) = Rc::try_unwrap(foo).ok().unwrap();
assert_eq!(weak.weak_count(), 1);
let weak2 = weak.clone();
assert_eq!(weak.weak_count(), 2);
assert_eq!(weak2.weak_count(), 2);
1.0.0 · sourceconst fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations
impl<'a, T> !RefUnwindSafe for Weak<'a, T>
impl<'a, T> !Send for Weak<'a, T>
impl<'a, T> !Sync for Weak<'a, T>
impl<'a, T> Unpin for Weak<'a, T>
impl<'a, T> !UnwindSafe for Weak<'a, T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more