wiwi::rc

Trait Counter

Source
pub unsafe trait Counter: Sized {
    // Required methods
    fn new() -> Self;
    fn strong_count(&self) -> usize;
    fn weak_count(&self) -> usize;
    fn inc_strong_for_new_ref(&self);
    fn dec_strong_for_drop(&self) -> bool;
    fn inc_weak_for_new_ref(&self);
    fn dec_weak_for_drop(&self) -> bool;
    fn try_inc_strong_for_upgrade(&self) -> bool;
}
Expand description

Trait for structs that can count references

wiwi includes two implementations: one for single threaded access (akin to std’s Rc), and the other for atomic multithreaded access (akin to std’s Arc).

§Safety

You must implement this trait correctly (ie. functions must return correct values), as values returned from functions are directly used to control the allocation/deallocation of memory and dropping of values.

Required Methods§

Source

fn new() -> Self

Create a new couter with strong and weak count both set to 1

Source

fn strong_count(&self) -> usize

Get the strong reference count

Source

fn weak_count(&self) -> usize

Get the weak reference count

Don’t subtract the “fake” weak reference that is held by all the strong references.

Source

fn inc_strong_for_new_ref(&self)

Increment the strong count for creation of a new strong reference

Source

fn dec_strong_for_drop(&self) -> bool

Decrements the strong count for dropping a reference, returning true if there are no more strong pointers left (and the value and items in the slice should be dropped)

Source

fn inc_weak_for_new_ref(&self)

Increments the weak count for creation of a new weak reference

Source

fn dec_weak_for_drop(&self) -> bool

Decrements the weak count for dropping a reference, returning true if there are no more weak pointers left (and the allocation should be deallocated)

Source

fn try_inc_strong_for_upgrade(&self) -> bool

Increment the strong count if it is possible to upgrade a weak pointer to strong, and return true, otherwise return false and do nothing

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§