TraitTypeMap

Struct TraitTypeMap 

Source
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>
where K: Clone + Eq + Hash + Debug,

Source

pub fn new() -> Self

Creates a new, empty TraitTypeMap.

Source

pub fn set_trait<T, U>(&self, key: K, value: U) -> Result<(), MapError>
where T: ?Sized + Any + Send + Sync + 'static, U: 'static + Into<Box<T>> + Send + Sync + Clone,

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.

Source

pub fn with<V: 'static, F, R>(&self, key: &K, f: F) -> Result<R, MapError>
where F: FnOnce(&V) -> R,

Accesses a value by its concrete type with a read-only closure.

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

pub fn with_mut<V: 'static, F, R>(&self, key: &K, f: F) -> Result<R, MapError>
where F: FnOnce(&mut V) -> R,

Accesses a value by its concrete type with a read-write closure.

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

pub fn with_trait<T, F, R>(&self, key: &K, f: F) -> Result<R, MapError>
where T: ?Sized + Any + Send + Sync + 'static, F: FnOnce(&T) -> R,

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::LockError if the internal lock cannot be acquired
  • Returns MapError::KeyNotFound if the key doesn’t exist
  • Returns MapError::TypeMismatch if the trait type doesn’t match
Source

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

Removes a value from the store.

§Errors

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

§Returns

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

Source

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.

Source

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.

Source

pub fn len(&self) -> Result<usize, MapError>

Gets the number of items in the store.

§Errors

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

Source

pub fn is_empty(&self) -> Result<bool, MapError>

Checks if the store is empty.

§Errors

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

Trait Implementations§

Source§

impl<K> Default for TraitTypeMap<K>
where K: Clone + Eq + Hash + Debug,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K> Freeze for TraitTypeMap<K>

§

impl<K> RefUnwindSafe for TraitTypeMap<K>

§

impl<K> Send for TraitTypeMap<K>
where K: Send,

§

impl<K> Sync for TraitTypeMap<K>
where K: Send,

§

impl<K> Unpin for TraitTypeMap<K>

§

impl<K> UnwindSafe for TraitTypeMap<K>

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
§

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, 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.