pub struct TypeMapV<K, V>{ /* private fields */ }Expand description
A thread-safe map that stores values of a specific type
TypeMapV allows you to create a type-safe, thread-safe map where all values
must be of the same type (or implement the same trait). This is particularly
useful for storing collections of trait objects or other homogeneous values.
§Examples
use sovran_typemap::{TypeMapV, MapError};
// Store trait objects
trait Handler: Send + Sync {
fn handle(&self) -> Result<(), MapError>;
}
let store = TypeMapV::<String, Box<dyn Handler>>::new();
// Apply an operation to all handlers
let result = store.apply(|key, handler| {
println!("Running handler {}", key);
handler.handle()
});Implementations§
Source§impl<K, V> TypeMapV<K, V>
impl<K, V> TypeMapV<K, V>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty TypeMapV
§Examples
use sovran_typemap::TypeMapV;
// Create a map storing strings
let string_store = TypeMapV::<String, String>::new();
// Create a map storing trait objects
trait MyTrait: Send + Sync {}
let trait_store = TypeMapV::<u32, Box<dyn MyTrait>>::new();Sourcepub fn set(&self, key: K, value: V) -> Result<(), MapError>
pub fn set(&self, key: K, value: V) -> Result<(), MapError>
Stores a value in the map
§Errors
Returns MapError::LockError if the internal lock cannot be acquired.
Sourcepub fn get(&self, key: &K) -> Result<V, MapError>where
V: Clone,
pub fn get(&self, key: &K) -> Result<V, MapError>where
V: Clone,
Retrieves a clone of a value from the map
§Errors
- Returns
MapError::LockErrorif the internal lock cannot be acquired - Returns
MapError::KeyNotFoundif the key doesn’t exist
Sourcepub fn apply<F>(&self, f: F) -> Result<(), MapError>
pub fn apply<F>(&self, f: F) -> Result<(), MapError>
Applies a function to all key-value pairs in the map
This method allows you to perform operations on all stored values while maintaining thread safety. The function is called with a reference to both the key and value for each entry.
§Examples
trait Handler: Send + Sync {
fn handle(&self) -> Result<(), MapError>;
}
let store = TypeMapV::<String, Box<dyn Handler>>::new();
// Apply to all handlers
let result = store.apply(|key, handler| {
handler.handle().map_err(|_| MapError::LockError)
});§Errors
Returns MapError::LockError if the internal lock cannot be acquired,
or any error returned by the provided function.
Sourcepub fn len(&self) -> Result<usize, MapError>
pub fn len(&self) -> Result<usize, MapError>
Returns the number of entries in the map
§Errors
Returns MapError::LockError if the internal lock cannot be acquired.
Sourcepub fn is_empty(&self) -> Result<bool, MapError>
pub fn is_empty(&self) -> Result<bool, MapError>
Returns true if the map contains no entries
§Errors
Returns MapError::LockError if the internal lock cannot be acquired.
Sourcepub fn contains_key(&self, key: &K) -> Result<bool, MapError>
pub fn contains_key(&self, key: &K) -> Result<bool, MapError>
Returns true if the map contains the specified key
§Errors
Returns MapError::LockError if the internal lock cannot be acquired.