Struct LockStore

Source
pub struct LockStore<K>
where K: Hash + Eq + Clone + Send + Sync + 'static,
{ /* private fields */ }
Expand description

A LockStore for sharing and managing a set of locks.

To lock a specific key with the store use LockStore::lock(key), any subsequent calls for that key will remain locked until the current holder is released (the returned LockGuard is dropped). When there are no longer any guards (when nothing is locking the lock) and there are no waiters left (no subsequent calls are waiting for the lock) then the lock will be placed into an unused locks queue and reused for a different key in the future.

To configure the number of locks kept in the queue, the LockStore can be created with LockStore::with_custom_unused_locks. The default value for LockStore::new() is 100 locks.

§Example

use sero::LockStore;

let store = LockStore::with_custom_unused_locks(1000);
let waiter = store.lock("test");
let guard = waiter.wait();
// the lock is released here
drop(guard);

Implementations§

Source§

impl<K> LockStore<K>
where K: Hash + Eq + Clone + Send + Sync + 'static,

Source

pub fn new() -> Self

Create a new LockStore.

§Example
use sero::LockStore;

let store = LockStore::new();

let guard = store.lock("test").wait();
Source

pub fn with_custom_unused_locks(keep_unused_locks: usize) -> Self

Provide a custom number of unused locks to keep in the internal queue rather than recreating locks.

The default value when this function is not used is 100 locks.

§Example
use sero::LockStore;

// now the store will keep up to 1000 unused locks in the queue to prevent reallocating them.
let store: LockStore<String> = LockStore::with_custom_unused_locks(1000);
Source

pub fn lock(&self, key: K) -> LockWaiter<K>

Lock a specific key and get the relevant LockWaiter. To actually acquire the lock either use the wait() method to lock synchronously or .await to lock asynchronously.

§Example
let store = LockStore::new();
// acquire a lock
let guard = store.lock("test").wait();

// to acquire the lock asynchronously use
let guard = store.lock("test").await;

// the lock is released here
drop(guard);

Trait Implementations§

Source§

impl<K> Clone for LockStore<K>
where K: Hash + Eq + Clone + Send + Sync + 'static + Clone,

Source§

fn clone(&self) -> LockStore<K>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<K> Debug for LockStore<K>
where K: Hash + Eq + Clone + Send + Sync + 'static + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K> Default for LockStore<K>
where K: Hash + Eq + Clone + Send + Sync + 'static,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K> Freeze for LockStore<K>

§

impl<K> !RefUnwindSafe for LockStore<K>

§

impl<K> Send for LockStore<K>

§

impl<K> Sync for LockStore<K>

§

impl<K> Unpin for LockStore<K>

§

impl<K> !UnwindSafe for LockStore<K>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.