lockmap

Struct LockMap

Source
pub struct LockMap<K, V> { /* private fields */ }
Expand description

A thread-safe hashmap that supports locking entries at the key level.

Implementations§

Source§

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

The main thread-safe map type providing per-key level locking.

Source

pub fn new() -> Self

Creates a new LockMap with the default number of shards.

§Returns

A new LockMap instance.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new LockMap with the specified initial capacity and the default number of shards.

§Arguments
  • capacity - The initial capacity of the hashmap.
§Returns

A new LockMap instance.

Source

pub fn with_capacity_and_shard_amount( capacity: usize, shard_amount: usize, ) -> Self

Creates a new LockMap with the specified initial capacity and number of shards.

§Arguments
  • capacity - The initial capacity of the hashmap.
  • shard_amount - The number of shards to create.
§Returns

A new LockMap instance.

Source

pub fn entry(&self, key: K) -> Entry<'_, K, V>
where K: Clone,

Gets exclusive access to an entry in the map.

The returned Entry provides exclusive access to the key and its associated value until it is dropped.

Locking behaviour: Deadlock if called when holding the same entry.

§Examples
let map = LockMap::<String, u32>::new();
{
    let mut entry = map.entry("key".to_string());
    entry.value.replace(42);
    // let _ = map.get("key".to_string()); // DEADLOCK!
    // map.set("key".to_string(), 21); // DEADLOCK!
    // map.remove("key".to_string()); // DEADLOCK!
    // let mut entry2 = map.entry("key".to_string()); // DEADLOCK!
}
Source

pub fn entry_by_ref<'a, 'b, Q>( &'a self, key: &'b Q, ) -> EntryByRef<'a, 'b, K, Q, V>
where K: Borrow<Q> + for<'c> From<&'c Q>, Q: Eq + Hash + ?Sized,

Gets exclusive access to an entry in the map.

The returned Entry provides exclusive access to the key and its associated value until it is dropped.

Locking behaviour: Deadlock if called when holding the same entry.

§Examples
let map = LockMap::<String, u32>::new();
{
    let mut entry = map.entry_by_ref("key");
    entry.value.replace(42);
    // let _ = map.get("key"); // DEADLOCK!
    // map.set_by_ref("key", 21); // DEADLOCK!
    // map.remove("key"); // DEADLOCK!
    // let mut entry2 = map.entry_by_ref("key"); // DEADLOCK!
}
Source

pub fn set(&self, key: K, value: V)

Sets a value in the map.

If other threads are currently accessing the key, this will wait until exclusive access is available before updating.

§Arguments
  • key - The key to update
  • value - The value to set

Locking behaviour: Deadlock if called when holding the same entry.

§Examples
use lockmap::LockMap;

let map = LockMap::<String, u32>::new();

// Set a value
map.set("key".to_string(), 42);

// Update existing value
map.set("key".to_string(), 123);
Source

pub fn set_by_ref<Q>(&self, key: &Q, value: V)
where K: Borrow<Q> + for<'c> From<&'c Q>, Q: Eq + Hash + ?Sized,

Sets a value in the map.

If other threads are currently accessing the key, this will wait until exclusive access is available before updating.

§Arguments
  • key - The key to update
  • value - The value to set

Locking behaviour: Deadlock if called when holding the same entry.

§Examples
use lockmap::LockMap;

let map = LockMap::<String, u32>::new();

// Set a value
map.set_by_ref("key", 42);

// Update existing value
map.set_by_ref("key", 123);
Source

pub fn remove<Q>(&self, key: &Q)
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

Removes a key from the map.

If other threads are currently accessing the key, this will wait until exclusive access is available before removing.

§Arguments
  • key - The key to remove

Locking behaviour: Deadlock if called when holding the same entry.

§Examples
use lockmap::LockMap;

let map = LockMap::<String, u32>::new();
map.set_by_ref("key", 42);
map.remove("key");
assert_eq!(map.get("key"), None);
Source§

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

Source

pub fn get<Q>(&self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

Gets the value associated with the given key.

If other threads are currently accessing the key, this will wait until exclusive access is available before returning.

§Arguments
  • key - The key to look up
§Returns
  • Some(V) if the key exists
  • None if the key doesn’t exist

Locking behaviour: Deadlock if called when holding the same entry.

§Examples
use lockmap::LockMap;

let map = LockMap::<String, u32>::new();
map.set_by_ref("key", 42);
assert_eq!(map.get("key"), Some(42));
assert_eq!(map.get("missing"), None);

Trait Implementations§

Source§

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

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K, V> Freeze for LockMap<K, V>

§

impl<K, V> RefUnwindSafe for LockMap<K, V>

§

impl<K, V> Send for LockMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for LockMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Unpin for LockMap<K, V>
where K: Unpin,

§

impl<K, V> UnwindSafe for LockMap<K, V>

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> 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, 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.