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

Source§

impl<'a, T> Weak<'a, T>

Source

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

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

impl<T> Weak<'_, T>

Source

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

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§

Source§

impl<T> Clone for Weak<'_, T>

Source§

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 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Drop for Weak<'_, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, T> Freeze for Weak<'a, T>

§

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§

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