[][src]Struct crndm::sync::Weak

pub struct Weak<T: PSafe + ?Sized, A: MemPool> { /* fields omitted */ }

Weak is a version of Parc that holds a non-owning reference to the managed allocation. The allocation is accessed by calling upgrade on the Weak pointer, which returns an Option<Parc<T>>.

Since a Weak reference does not count towards ownership, it will not prevent the value stored in the allocation from being dropped, and Weak itself makes no guarantees about the value still being present. Thus it may return None when upgraded. Note however that a Weak reference does prevent the allocation itself (the backing store) from being deallocated.

A Weak pointer is useful for keeping a temporary reference to the allocation managed by Parc without preventing its inner value from being dropped. It is also used to prevent circular references between Parc pointers, since mutual owning references would never allow either Parc to be dropped. For example, a tree could have strong Parc pointers from parent nodes to children, and Weak pointers from children back to their parents.

The typical way to obtain a Weak pointer is to call Parc::downgrade.

Implementations

impl<T: PSafe, A: MemPool> Weak<T, A>[src]

pub fn as_raw(&self) -> *const T[src]

pub fn into_raw(self) -> *const T[src]

pub unsafe fn from_raw(ptr: *const T) -> Self[src]

impl<T: PSafe + ?Sized, A: MemPool> Weak<T, A>[src]

pub fn new() -> Weak<T, A>[src]

Creates a new dangling weak pointer

pub fn upgrade(&self, _journal: &Journal<A>) -> Option<Parc<T, A>>[src]

Attempts to upgrade the Weak pointer to an Parc, delaying dropping of the inner value if successful.

Returns None if the inner value has since been dropped.

Examples

use crndm::alloc::*;
use crndm::sync::Parc;

Heap::transaction(|j| {
    let five = Parc::new(5, j);
    let weak_five = Parc::downgrade(&five, j);
    let strong_five = weak_five.upgrade(j);
    assert!(strong_five.is_some());
     
    // Destroy all strong pointers.
    drop(strong_five);
    drop(five);
     
    assert!(weak_five.upgrade(j).is_none());
}).unwrap()

pub fn strong_count(&self) -> usize[src]

Gets the number of strong (Parc) pointers pointing to this allocation.

If self was created using Weak::new, this will return 0.

pub fn weak_count(&self) -> usize[src]

Gets an approximation of the number of Weak pointers pointing to this allocation.

If self was created using Weak::new, or if there are no remaining strong pointers, this will return 0.

Accuracy

Due to implementation details, the returned value can be off by 1 in either direction when other threads are manipulating any Parcs or Weaks pointing to the same allocation.

pub fn ptr_eq(&self, other: &Self) -> bool[src]

Returns true if the two Weaks point to the same allocation (similar to std::ptr::eq), or if both don't point to any allocation (because they were created with Weak::new()).

Notes

Since this compares pointers it means that Weak::new() will equal each other, even though they don't point to any allocation.

Trait Implementations

impl<T: PSafe + ?Sized + Debug, A: MemPool> Debug for Weak<T, A>[src]

impl<T: PSafe + ?Sized, A: MemPool> Default for Weak<T, A>[src]

impl<T: PSafe + ?Sized, A: MemPool> Drop for Weak<T, A>[src]

impl<T: PSafe + ?Sized, A: MemPool> PClone<A> for Weak<T, A>[src]

impl<T: ?Sized, A: MemPool> !Send for Weak<T, A>[src]

impl<T: ?Sized, A: MemPool> !Sync for Weak<T, A>[src]

impl<T: ?Sized, A: MemPool> !TxOutSafe for Weak<T, A>[src]

impl<T: ?Sized, A: MemPool> !VSafe for Weak<T, A>[src]

Auto Trait Implementations

impl<T: ?Sized, A> LooseTxInUnsafe for Weak<T, A> where
    A: LooseTxInUnsafe,
    T: LooseTxInUnsafe
[src]

impl<T: ?Sized, A> PSafe for Weak<T, A>[src]

impl<T: ?Sized, A> RefUnwindSafe for Weak<T, A> where
    A: RefUnwindSafe,
    T: RefUnwindSafe
[src]

impl<T, A> !TxInSafe for Weak<T, A>[src]

impl<T: ?Sized, A> Unpin for Weak<T, A> where
    A: Unpin,
    T: Unpin
[src]

impl<T: ?Sized, A> UnwindSafe for Weak<T, A> where
    A: UnwindSafe,
    T: UnwindSafe
[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,