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

pub struct MainLock<T> {
    // some 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::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>

fn new(value: T) -> Self

fn read(&self) -> LockResult<ReadGuard<T>>

fn try_read(&self) -> TryLockResult<ReadGuard<T>>

fn write(&self) -> LockResult<WriteGuard<T>>

fn try_write(&self) -> TryLockResult<WriteGuard<T>>