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.
impl<K: Eq + Hash + Clone, V> LockMap<K, V>
The main thread-safe map type providing per-key level locking.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Sourcepub fn with_capacity_and_shard_amount(
capacity: usize,
shard_amount: usize,
) -> Self
pub fn with_capacity_and_shard_amount( capacity: usize, shard_amount: usize, ) -> Self
Sourcepub fn entry(&self, key: K) -> Entry<'_, K, V>where
K: Clone,
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!
}Sourcepub fn entry_by_ref<'a, 'b, Q>(
&'a self,
key: &'b Q,
) -> EntryByRef<'a, 'b, K, Q, V>
pub fn entry_by_ref<'a, 'b, Q>( &'a self, key: &'b Q, ) -> EntryByRef<'a, 'b, K, Q, V>
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!
}Sourcepub fn set(&self, key: K, value: V)
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 updatevalue- 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);Sourcepub fn set_by_ref<Q>(&self, key: &Q, value: V)
pub fn set_by_ref<Q>(&self, key: &Q, 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 updatevalue- 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);Sourcepub fn remove<Q>(&self, key: &Q)
pub fn remove<Q>(&self, key: &Q)
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>
impl<K: Eq + Hash + Clone, V: Clone> LockMap<K, V>
Sourcepub fn get<Q>(&self, key: &Q) -> Option<V>
pub fn get<Q>(&self, key: &Q) -> Option<V>
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 existsNoneif 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);