Struct namedlock::LockSpace

source ·
pub struct LockSpace<K: Eq + Hash, V> { /* private fields */ }
Expand description

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.

Implementations

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.

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);

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);

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

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.