TypeMapV

Struct TypeMapV 

Source
pub struct TypeMapV<K, V>
where K: Clone + Eq + Hash + Debug, V: Send + Sync,
{ /* 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>
where K: Clone + Eq + Hash + Debug, V: Send + Sync,

Source

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

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.

Source

pub fn get(&self, key: &K) -> Result<V, MapError>
where V: Clone,

Retrieves a clone of a value from the map

§Errors
  • Returns MapError::LockError if the internal lock cannot be acquired
  • Returns MapError::KeyNotFound if the key doesn’t exist
Source

pub fn remove(&self, key: &K) -> Result<bool, MapError>

Removes a value from the map

§Errors

Returns MapError::LockError if the internal lock cannot be acquired.

§Returns

Returns Ok(true) if the key was present and removed, Ok(false) if not present.

Source

pub fn apply<F>(&self, f: F) -> Result<(), MapError>
where F: FnMut(&K, &V) -> 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.

Source

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.

Source

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.

Source

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.

Source

pub fn keys(&self) -> Result<Vec<K>, MapError>
where K: Clone,

Returns a vector of all keys in the map

§Errors

Returns MapError::LockError if the internal lock cannot be acquired.

Source

pub fn values(&self) -> Result<Vec<V>, MapError>
where V: Clone,

Returns a vector of all values in the map

§Errors

Returns MapError::LockError if the internal lock cannot be acquired.

Trait Implementations§

Source§

impl<K, V> Clone for TypeMapV<K, V>
where K: Clone + Eq + Hash + Debug + Clone, V: Send + Sync + Clone,

Source§

fn clone(&self) -> TypeMapV<K, V>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<K, V> Debug for TypeMapV<K, V>
where K: Clone + Eq + Hash + Debug + Debug, V: Send + Sync + Debug,

Source§

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

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

impl<K, V> Default for TypeMapV<K, V>
where K: Clone + Eq + Hash + Debug, V: Send + Sync,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K, V> Freeze for TypeMapV<K, V>

§

impl<K, V> RefUnwindSafe for TypeMapV<K, V>

§

impl<K, V> Send for TypeMapV<K, V>
where K: Send,

§

impl<K, V> Sync for TypeMapV<K, V>
where K: Send,

§

impl<K, V> Unpin for TypeMapV<K, V>

§

impl<K, V> UnwindSafe for TypeMapV<K, V>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

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

Source§

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

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

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

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.