pub struct TypeStore { /* private fields */ }Expand description
A thread-safe container that stores exactly one value per type.
TypeStore provides a simple way to store and retrieve values using their
type as the key. This is useful for dependency injection, service locators,
and managing application-wide state without explicit key management.
Unlike TypeMap which uses explicit keys, TypeStore uses the type itself
as the key, meaning you can only store one value of each type.
§Examples
use sovran_typemap::{TypeStore, MapError};
#[derive(Clone, Debug)]
struct DatabaseConfig {
host: String,
port: u16,
}
#[derive(Clone, Debug)]
struct AppConfig {
name: String,
debug: bool,
}
fn main() -> Result<(), MapError> {
let store = TypeStore::new();
// Store configurations by type
store.set(DatabaseConfig {
host: "localhost".to_string(),
port: 5432,
})?;
store.set(AppConfig {
name: "MyApp".to_string(),
debug: true,
})?;
// Retrieve by type - no key needed
let db_config = store.get::<DatabaseConfig>()?;
println!("Database: {}:{}", db_config.host, db_config.port);
// Modify in place
store.with_mut::<AppConfig, _, _>(|cfg| {
cfg.debug = false;
})?;
Ok(())
}Implementations§
Source§impl TypeStore
impl TypeStore
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty TypeStore.
§Examples
use sovran_typemap::TypeStore;
let store = TypeStore::new();Sourcepub fn set<V>(&self, value: V) -> Result<(), MapError>
pub fn set<V>(&self, value: V) -> Result<(), MapError>
Stores a value, using its type as the key.
If a value of this type already exists, it will be replaced.
§Errors
Returns MapError::LockError if the internal lock cannot be acquired.
§Examples
let store = TypeStore::new();
store.set(42i32)?;
store.set("hello".to_string())?;
store.set(vec![1, 2, 3])?;
// Overwrites the previous i32
store.set(100i32)?;
assert_eq!(store.get::<i32>()?, 100);Sourcepub fn set_with<V, F>(&self, f: F) -> Result<(), MapError>
pub fn set_with<V, F>(&self, f: F) -> Result<(), MapError>
Stores a value generated by a closure.
This is useful for lazy initialization or when value construction should only happen if the lock can be acquired.
§Errors
Returns MapError::LockError if the internal lock cannot be acquired.
§Examples
let store = TypeStore::new();
// Lazily construct a value
store.set_with(|| {
vec![1, 2, 3, 4, 5]
})?;
store.with::<Vec<i32>, _, _>(|v| {
assert_eq!(v.len(), 5);
})?;Sourcepub fn get<V>(&self) -> Result<V, MapError>where
V: 'static + Clone,
pub fn get<V>(&self) -> Result<V, MapError>where
V: 'static + Clone,
Retrieves a clone of a value by its type.
§Errors
- Returns
MapError::LockErrorif the internal lock cannot be acquired - Returns
MapError::KeyNotFoundif no value of this type exists
§Examples
let store = TypeStore::new();
store.set(42i32)?;
let value = store.get::<i32>()?;
assert_eq!(value, 42);
// Type not found
match store.get::<String>() {
Err(MapError::KeyNotFound(type_name)) => {
println!("No value of type: {}", type_name);
}
_ => {}
}Sourcepub fn with<V: 'static, F, R>(&self, f: F) -> Result<R, MapError>
pub fn with<V: 'static, F, R>(&self, f: F) -> Result<R, MapError>
Accesses a value by type with a read-only closure.
§Errors
- Returns
MapError::LockErrorif the internal lock cannot be acquired - Returns
MapError::KeyNotFoundif no value of this type exists
§Examples
let store = TypeStore::new();
store.set(vec![1, 2, 3, 4, 5])?;
let sum = store.with::<Vec<i32>, _, _>(|numbers| {
numbers.iter().sum::<i32>()
})?;
assert_eq!(sum, 15);Sourcepub fn with_mut<V: 'static, F, R>(&self, f: F) -> Result<R, MapError>
pub fn with_mut<V: 'static, F, R>(&self, f: F) -> Result<R, MapError>
Accesses a value by type with a read-write closure.
§Errors
- Returns
MapError::LockErrorif the internal lock cannot be acquired - Returns
MapError::KeyNotFoundif no value of this type exists
§Examples
let store = TypeStore::new();
store.set(vec![1, 2, 3])?;
store.with_mut::<Vec<i32>, _, _>(|numbers| {
numbers.push(4);
numbers.push(5);
})?;
let len = store.with::<Vec<i32>, _, _>(|v| v.len())?;
assert_eq!(len, 5);Sourcepub fn remove<V: 'static>(&self) -> Result<bool, MapError>
pub fn remove<V: 'static>(&self) -> Result<bool, MapError>
Removes a value by its type.
§Errors
Returns MapError::LockError if the internal lock cannot be acquired.
§Returns
Returns Ok(true) if a value was removed, Ok(false) if no value
of that type existed.
§Examples
let store = TypeStore::new();
store.set(42i32)?;
assert!(store.remove::<i32>()?);
assert!(!store.remove::<i32>()?); // Already removedTrait Implementations§
Auto Trait Implementations§
impl Freeze for TypeStore
impl RefUnwindSafe for TypeStore
impl Send for TypeStore
impl Sync for TypeStore
impl Unpin for TypeStore
impl UnwindSafe for TypeStore
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)