pub struct TraitTypeMap<K> { /* private fields */ }Expand description
A thread-safe heterogeneous container that supports trait object access.
TraitTypeMap extends the concept of TypeMap to support storing values
that can be accessed either by their concrete type or through a trait interface.
This is useful when you need polymorphic access to stored values.
§Examples
use sovran_typemap::{TraitTypeMap, MapError};
use std::any::Any;
// Define a trait
trait Greeter: Any + Send + Sync {
fn greet(&self) -> String;
}
#[derive(Clone)]
struct EnglishGreeter { name: String }
impl Greeter for EnglishGreeter {
fn greet(&self) -> String { format!("Hello, {}!", self.name) }
}
impl Into<Box<dyn Greeter>> for EnglishGreeter {
fn into(self) -> Box<dyn Greeter> { Box::new(self) }
}
let store = TraitTypeMap::<String>::new();
store.set_trait::<dyn Greeter, _>("greeter".to_string(), EnglishGreeter { name: "World".to_string() }).unwrap();
// Access via trait
store.with_trait::<dyn Greeter, _, _>(&"greeter".to_string(), |g| {
assert_eq!(g.greet(), "Hello, World!");
}).unwrap();Implementations§
Source§impl<K> TraitTypeMap<K>
impl<K> TraitTypeMap<K>
Sourcepub fn set_trait<T, U>(&self, key: K, value: U) -> Result<(), MapError>
pub fn set_trait<T, U>(&self, key: K, value: U) -> Result<(), MapError>
Stores a value with its associated trait type.
The value can later be accessed either by its concrete type or through the trait interface.
§Type Parameters
T- The trait type (e.g.,dyn MyTrait)U- The concrete type that implements the trait
§Errors
Returns MapError::LockError if the internal lock cannot be acquired.
Sourcepub fn with<V: 'static, F, R>(&self, key: &K, f: F) -> Result<R, MapError>
pub fn with<V: 'static, F, R>(&self, key: &K, f: F) -> Result<R, MapError>
Accesses a value by its concrete type with a read-only closure.
§Errors
- Returns
MapError::LockErrorif the internal lock cannot be acquired - Returns
MapError::KeyNotFoundif the key doesn’t exist - Returns
MapError::TypeMismatchif the concrete type doesn’t match
Sourcepub fn with_mut<V: 'static, F, R>(&self, key: &K, f: F) -> Result<R, MapError>
pub fn with_mut<V: 'static, F, R>(&self, key: &K, f: F) -> Result<R, MapError>
Accesses a value by its concrete type with a read-write closure.
§Errors
- Returns
MapError::LockErrorif the internal lock cannot be acquired - Returns
MapError::KeyNotFoundif the key doesn’t exist - Returns
MapError::TypeMismatchif the concrete type doesn’t match
Sourcepub fn with_trait<T, F, R>(&self, key: &K, f: F) -> Result<R, MapError>
pub fn with_trait<T, F, R>(&self, key: &K, f: F) -> Result<R, MapError>
Accesses a value through its trait interface with a read-only closure.
This enables polymorphic access to stored values without knowing their concrete type.
§Errors
- Returns
MapError::LockErrorif the internal lock cannot be acquired - Returns
MapError::KeyNotFoundif the key doesn’t exist - Returns
MapError::TypeMismatchif the trait type doesn’t match
Sourcepub fn contains_key(&self, key: &K) -> Result<bool, MapError>
pub fn contains_key(&self, key: &K) -> Result<bool, MapError>
Checks if a key exists in the store.
§Errors
Returns MapError::LockError if the internal lock cannot be acquired.
Sourcepub fn keys(&self) -> Result<Vec<K>, MapError>where
K: Clone,
pub fn keys(&self) -> Result<Vec<K>, MapError>where
K: Clone,
Gets all keys in the store.
§Errors
Returns MapError::LockError if the internal lock cannot be acquired.