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

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();

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());

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);

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

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);
Performs copy-assignment from source. Read more
Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.