Struct namedlock::LockSpace [] [src]

pub struct LockSpace<K: Eq + Hash, V> {
    // some fields omitted
}

A LockSpace<K,V> holds many Mutex<V>'s, keyed by K.

All accesses to the internal value must go through one of the lock methods.

See the crate documentation for an example.

Key parameters

Most of the LockSpace<K,V> methods take a key: K. This is because we make a lot of use of the HashMap::entry API. If that API changes to accept e.g. Cow, this crate will adopt that too.

Methods

impl<K: Eq + Hash + Clone, V> LockSpace<K, V>

fn new(cleanup: Cleanup) -> LockSpace<K, V>

Create a new LockSpace.

If cleanup is AutoCleanup, values will be deleted automatically when the last lock is released. Otherwise, values will remain in the space until try_remove() returns Success.

fn lock<'a, C>(&'a self, key: K, initial: C) -> Result<LockSpaceGuard<'a, K, V>> where C: FnOnce() -> V

Find the object by key, or create it by calling initial if it does not exist. Then, lock it and return a LockSpaceGuard over the object. Once the guard is dropped, its object is unlocked, and if AutoCleanup is specified for this space, removed if this is the last use.

let space=namedlock::LockSpace::<String,i32>::new(namedlock::KeepUnused);

let value=space.lock("test".to_owned(),||0);
*value.unwrap()+=1;
let value=space.lock("test".to_owned(),||0);
assert_eq!(*value.unwrap(),1);

fn with_lock<F, R, C>(&self, key: K, initial: C, f: F) -> Result<R> where C: FnOnce() -> V, F: FnOnce(&mut V) -> R

Find the object by key, or create it by calling initial if it does not exist. Then, call f on that object.

let space=namedlock::LockSpace::<String,i32>::new(namedlock::KeepUnused);

space.with_lock("test".to_owned(),||0,|i|*i+=1);
assert_eq!(space.with_lock("test".to_owned(),||0,|i|*i).unwrap(),1);

fn try_remove(&self, key: K) -> LockSpaceRemoveResult

Find the object by key, then delete it if it is not actively being used. If it is actually being used, WouldBlock will be returned.

This is only useful if this LockSpace is of the KeepUnused kind.

Trait Implementations

impl<K: Eq + Hash, V> Clone for LockSpace<K, V>

fn clone(&self) -> LockSpace<K, V>

fn clone_from(&mut self, source: &Self)