[][src]Struct parc::ParentArc

pub struct ParentArc<T> { /* fields omitted */ }

Owner of multiple atomically reference counted children.

The type ParentArc<T> allows for shared access of the inner data by multiple threads through LockWeak references. Call downgrade on a ParentArc to create a child reference that can be upgraded into a temporary reader of the inner data. This allows for the locking and the consumption of the parent at any time because no strong references are held permanently.

Unlike Arc<T> this structure will die along with it's readers.

Thread Safety

The LockWeak can be passed around through threads safely because they do not guaranty the existence of the data at upgrade time. ParentArc<T> makes it thread safe to have multiple owned reference of the same data, but it doesn't add thread safety to its data.

Methods

impl<T> ParentArc<T>[src]

pub fn new(data: T) -> Self[src]

Build a new ParentArc

Examples

use parc::ParentArc;
use std::sync::Mutex;
fn main() {
    let parent = ParentArc::new(Mutex::new(true));
}

pub fn pin(data: T) -> Pin<ParentArc<T>>[src]

Constructs a new Pin<ParentArc<T>>. If T does not implement Unpin, then data will be pinned in memory and unable to be moved.

pub fn lock(&self)[src]

Locks all LockWeak of this instance, it will prevent all further upgrades until unlocked. It is advised to call this before attempting a try_into_inner.

Examples

use parc::ParentArc;
use std::sync::Mutex;
fn main() {
    let parent = ParentArc::new(Mutex::new(0));
    parent.lock(); // LockWeaks are no longer able to upgrade successfully
    assert!(parent.is_locked());
}

pub fn is_locked(&self) -> bool[src]

Check wether the LockWeaks are locked. Since only the Parent can unlock it is considered a somewhat trustable result.

pub fn unlock(&self)[src]

Unlocks all LockWeak of this ParentArc, this allows for their ugrade to start again.

Examples

use parc::ParentArc;
use std::sync::Mutex;
fn main() {
    let parent = ParentArc::new(Mutex::new(0));
     
    parent.lock(); // LockWeaks are no longer able to upgrade successfully
    assert!(parent.is_locked());
     
    parent.unlock(); // LockWeaks can upgrade successfully again
    assert!(!parent.is_locked());
}

pub fn downgrade(other: &Self) -> LockWeak<T>[src]

Downgrade a ParentArc into a LockWeak

Examples

use parc::{ParentArc, LockWeak};
use std::sync::Mutex;
fn main() {
    let parent = ParentArc::new(Mutex::new(true));
    let weak: LockWeak<_> = ParentArc::downgrade(&parent);
}

pub fn try_downgrade(other: &Self) -> Option<LockWeak<T>>[src]

Tries to downgrade a ParentArc into a LockWeak if the inner state allows the latter to upgrade.

Examples

use parc::{ParentArc, LockWeak};
use std::sync::Mutex;
fn main() {
    let parent = ParentArc::new(Mutex::new(true));
    parent.lock(); // LockWeaks are no longer able to upgrade successfully
     
    if let Some(_) = ParentArc::try_downgrade(&parent) {
        assert!(false);
    }
}

pub fn block_into_inner(self) -> T[src]

Blocks the thread until all ChildArc of this instance have dropped, returning the underlying data.

Safety

This call will indefinitly spin if a child has not droped correctly.

Examples

use parc::{ParentArc, LockWeak};
use std::sync::Mutex;
fn main() {
    let parent = ParentArc::new(Mutex::new(true));
     
    let weak1: LockWeak<_> = ParentArc::downgrade(&parent);
    let weak2: LockWeak<_> = ParentArc::downgrade(&parent);
     
    let child = weak1.upgrade().unwrap();
    drop(child);

    let _: Mutex<bool> = parent.block_into_inner();
}

pub fn try_unwrap(other: Self) -> TryUnwrapResult<T>[src]

Non-blocking version of block_into_inner. It is advised to call lock before calling this one, unless you know for sure there are no ChildArc alive at this instance.

Safety

This will never unwrap Ok(T) if a child has not droped correctly.

Examples

use parc::{ParentArc, LockWeak, TryUnwrapError::*};
use std::sync::Mutex;
fn main() {
    let mut parent = ParentArc::new(Mutex::new(true));
     
    let weak1: LockWeak<_> = ParentArc::downgrade(&parent);
    let weak2: LockWeak<_> = ParentArc::downgrade(&parent);
     
    let child = weak1.upgrade().unwrap();
     
    // Unlocked LockWeaks
    parent = if let Err(WouldLock(parent)) = ParentArc::try_unwrap(parent) {
        parent
    } else {
        unreachable!()
    };

    // Locked LockWeaks
    parent.lock();
    parent = if let Err(WouldBlock(parent)) = ParentArc::try_unwrap(parent) {
        parent
    } else {
        unreachable!()
    };
    parent.unlock();

    // Droped children
    drop(child);
    let value: Mutex<bool> = ParentArc::try_unwrap(parent).unwrap();
}

Trait Implementations

impl<T> Deref for ParentArc<T>[src]

type Target = T

The resulting type after dereferencing.

impl<T> Drop for ParentArc<T>[src]

impl<T> AsRef<T> for ParentArc<T>[src]

Auto Trait Implementations

impl<T> Unpin for ParentArc<T>

impl<T> !Send for ParentArc<T>

impl<T> !Sync for ParentArc<T>

impl<T> UnwindSafe for ParentArc<T> where
    T: RefUnwindSafe

impl<T> RefUnwindSafe for ParentArc<T> where
    T: RefUnwindSafe

Blanket Implementations

impl<T> From<T> for 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> Into<U> for T where
    U: From<T>, 
[src]

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<T> Borrow<T> for T where
    T: ?Sized
[src]

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

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