TypeStoreValue

Struct TypeStoreValue 

Source
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

Source

pub fn new() -> Self

Creates a new, empty TypeStoreValue.

§Examples
use sovran_typemap::TypeStoreValue;

let store = TypeStoreValue::new();
assert!(store.is_empty());
Source

pub fn set<V>(&mut self, value: V)
where V: Clone + Any + Send + Sync,

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));
Source

pub fn set_with<V, F>(&mut self, f: F)
where V: Clone + Any + Send + Sync, F: FnOnce() -> V,

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]));
Source

pub fn get<V>(&self) -> Option<V>
where V: Clone + Any + Send + Sync,

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);
Source

pub fn with<V, F, R>(&self, f: F) -> Option<R>
where V: Any, F: FnOnce(&V) -> 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));
Source

pub fn with_mut<V, F, R>(&mut self, f: F) -> Option<R>
where V: Any, F: FnOnce(&mut V) -> 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]));
Source

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 removed
Source

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>());
Source

pub fn len(&self) -> usize

Gets the number of values in the store.

§Examples
use sovran_typemap::TypeStoreValue;

let mut store = TypeStoreValue::new();
assert_eq!(store.len(), 0);

store.set(42i32);
store.set("hello".to_string());
assert_eq!(store.len(), 2);
Source

pub fn is_empty(&self) -> bool

Checks if the store is empty.

§Examples
use sovran_typemap::TypeStoreValue;

let mut store = TypeStoreValue::new();
assert!(store.is_empty());

store.set(42i32);
assert!(!store.is_empty());

Trait Implementations§

Source§

impl Clone for TypeStoreValue

Source§

fn clone(&self) -> TypeStoreValue

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TypeStoreValue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TypeStoreValue

Source§

fn default() -> TypeStoreValue

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneAny for T
where T: Clone + Any + Send + Sync,

Source§

fn clone_any(&self) -> Box<dyn CloneAny>

Clone this value into a boxed trait object.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Get a reference to the underlying Any.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Get a mutable reference to the underlying Any.
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.