pub struct TypeStoreValue { /* private fields */ }Expand description
A cloneable, value-based container that stores exactly one value per type.
Unlike TypeStore, TypeStoreValue does not use Arc<Mutex<>> internally,
making it cloneable and suitable for single-threaded contexts or when you
need to snapshot state.
§Examples
use sovran_typemap::TypeStoreValue;
#[derive(Clone, Debug, PartialEq)]
struct Config {
debug: bool,
max_retries: u32,
}
let mut store = TypeStoreValue::new();
store.set(Config { debug: true, max_retries: 3 });
store.set(42i32);
// Clone the entire store
let snapshot = store.clone();
// Modify original
store.with_mut::<Config, _, _>(|cfg| {
cfg.debug = false;
});
// Snapshot is unchanged
assert_eq!(snapshot.get::<Config>().unwrap().debug, true);
assert_eq!(store.get::<Config>().unwrap().debug, false);Implementations§
Source§impl TypeStoreValue
impl TypeStoreValue
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty TypeStoreValue.
§Examples
use sovran_typemap::TypeStoreValue;
let store = TypeStoreValue::new();
assert!(store.is_empty());Sourcepub fn set<V>(&mut self, value: V)
pub fn set<V>(&mut self, value: V)
Stores a value, using its type as the key.
If a value of this type already exists, it will be replaced.
§Examples
use sovran_typemap::TypeStoreValue;
let mut store = TypeStoreValue::new();
store.set(42i32);
store.set("hello".to_string());
// Overwrites the previous i32
store.set(100i32);
assert_eq!(store.get::<i32>(), Some(100));Sourcepub fn set_with<V, F>(&mut self, f: F)
pub fn set_with<V, F>(&mut self, f: F)
Stores a value generated by a closure.
§Examples
use sovran_typemap::TypeStoreValue;
let mut store = TypeStoreValue::new();
store.set_with(|| vec![1, 2, 3, 4, 5]);
assert_eq!(store.get::<Vec<i32>>(), Some(vec![1, 2, 3, 4, 5]));Sourcepub fn get<V>(&self) -> Option<V>
pub fn get<V>(&self) -> Option<V>
Retrieves a clone of a value by its type.
Returns None if no value of this type exists.
§Examples
use sovran_typemap::TypeStoreValue;
let mut store = TypeStoreValue::new();
store.set(42i32);
assert_eq!(store.get::<i32>(), Some(42));
assert_eq!(store.get::<String>(), None);Sourcepub fn with<V, F, R>(&self, f: F) -> Option<R>
pub fn with<V, F, R>(&self, f: F) -> Option<R>
Accesses a value by type with a read-only closure.
Returns None if no value of this type exists.
§Examples
use sovran_typemap::TypeStoreValue;
let mut store = TypeStoreValue::new();
store.set(vec![1, 2, 3, 4, 5]);
let sum = store.with::<Vec<i32>, _, _>(|numbers| {
numbers.iter().sum::<i32>()
});
assert_eq!(sum, Some(15));Sourcepub fn with_mut<V, F, R>(&mut self, f: F) -> Option<R>
pub fn with_mut<V, F, R>(&mut self, f: F) -> Option<R>
Accesses a value by type with a read-write closure.
Returns None if no value of this type exists.
§Examples
use sovran_typemap::TypeStoreValue;
let mut store = TypeStoreValue::new();
store.set(vec![1, 2, 3]);
store.with_mut::<Vec<i32>, _, _>(|numbers| {
numbers.push(4);
numbers.push(5);
});
assert_eq!(store.get::<Vec<i32>>(), Some(vec![1, 2, 3, 4, 5]));Sourcepub fn remove<V: Any>(&mut self) -> bool
pub fn remove<V: Any>(&mut self) -> bool
Removes a value by its type.
Returns true if a value was removed, false if no value of that type existed.
§Examples
use sovran_typemap::TypeStoreValue;
let mut store = TypeStoreValue::new();
store.set(42i32);
assert!(store.remove::<i32>());
assert!(!store.remove::<i32>()); // Already removedSourcepub fn contains<V: Any>(&self) -> bool
pub fn contains<V: Any>(&self) -> bool
Checks if a value of the given type exists.
§Examples
use sovran_typemap::TypeStoreValue;
let mut store = TypeStoreValue::new();
assert!(!store.contains::<i32>());
store.set(42i32);
assert!(store.contains::<i32>());Trait Implementations§
Source§impl Clone for TypeStoreValue
impl Clone for TypeStoreValue
Source§fn clone(&self) -> TypeStoreValue
fn clone(&self) -> TypeStoreValue
1.0.0§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TypeStoreValue
impl Debug for TypeStoreValue
Source§impl Default for TypeStoreValue
impl Default for TypeStoreValue
Source§fn default() -> TypeStoreValue
fn default() -> TypeStoreValue
Auto Trait Implementations§
impl Freeze for TypeStoreValue
impl !RefUnwindSafe for TypeStoreValue
impl Send for TypeStoreValue
impl Sync for TypeStoreValue
impl Unpin for TypeStoreValue
impl !UnwindSafe for TypeStoreValue
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)