[][src]Struct sublock::sync::prooflock::MainLock

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

A variant of RwLock with sublocks that can be opened at no cost by providing a proof that the main lock is opened.

use sublock::sync::prooflock::*;
use std::collections::HashMap;

type State = HashMap<usize, SubCell<usize>>;
let data : MainLock<State> = MainLock::new(HashMap::new());

{
    println!("* Attempt to read in the MainLock.");
    let (_, guard) = data.read().unwrap();
    assert_eq!(guard.len(), 0);
}

{
    println!("* Attempt to write in the MainLock.");
    let (proof, mut guard) = data.write().unwrap();
    guard.insert(0, SubCell::new(&proof, 42));
    assert_eq!(guard.len(), 1);
}

{
    println!("* Attempt to read in a SubCell.");
    let (proof, guard) = data.read().unwrap();
    assert_eq!(guard.len(), 1);
    let cell = guard.get(&0).unwrap();
    assert_eq!(*cell.borrow(&proof), 42);
}

{
    println!("* Attempt to read and write in a SubCell.");
    let (proof, guard) = data.write().unwrap();
    assert_eq!(guard.len(), 1);
    let cell = guard.get(&0).unwrap();
    assert_eq!(*cell.borrow(&proof), 42);

    *cell.borrow_mut(&proof) = 99;
    assert_eq!(*cell.borrow(&proof), 99);
}

{
    println!("* Check that the SubCell changes are kept.");
    let (proof, guard) = data.read().unwrap();
    assert_eq!(guard.len(), 1);
    let cell = guard.get(&0).unwrap();
    assert_eq!(*cell.borrow(&proof), 99);
}

Methods

impl<T> MainLock<T>[src]

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

pub fn read(&self) -> LockResult<ReadGuard<T>>[src]

pub fn try_read(&self) -> TryLockResult<ReadGuard<T>>[src]

pub fn write(&self) -> LockResult<WriteGuard<T>>[src]

pub fn try_write(&self) -> TryLockResult<WriteGuard<T>>[src]

Auto Trait Implementations

impl<T> Send for MainLock<T> where
    T: Send

impl<T> Sync for MainLock<T> where
    T: Send + Sync

Blanket Implementations

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

impl<T> From for T[src]

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

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

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

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

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

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

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