pub struct TypeMap<T> { /* private fields */ }Expand description
Maps type identities to values of type T. Useful for registries and dispatch tables.
§Example
use typex::TypeMap;
struct GetUser;
struct GetInvoice;
let mut routes = TypeMap::<&'static str>::new();
routes.insert::<GetUser>("/users/:id");
routes.insert::<GetInvoice>("/invoices/:id");
assert_eq!(routes.get::<GetUser>(), Some(&"/users/:id"));
assert_eq!(routes.get::<GetInvoice>(), Some(&"/invoices/:id"));
assert!(routes.contains::<GetInvoice>());
assert_eq!(routes.remove::<GetInvoice>(), Some("/invoices/:id"));
assert!(!routes.contains::<GetInvoice>());
assert_eq!(routes.len(), 1);Implementation note: TypeMap is backed by a Vec, so it works under no_std.
Scans are O(n), which is suitable for static type registries.
Implementations§
Source§impl<T> TypeMap<T>
impl<T> TypeMap<T>
Sourcepub fn insert<U: 'static>(&mut self, value: T) -> Option<T>
pub fn insert<U: 'static>(&mut self, value: T) -> Option<T>
Binds value to U, returning the previous value if the type was already bound.
Sourcepub fn get_mut<U: 'static>(&mut self) -> Option<&mut T>
pub fn get_mut<U: 'static>(&mut self) -> Option<&mut T>
Returns a mutable reference to the value bound to U.
Sourcepub fn type_names(&self) -> impl Iterator<Item = &'static str> + '_
pub fn type_names(&self) -> impl Iterator<Item = &'static str> + '_
Iterates over the bound Rust type names, in insertion order.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for TypeMap<T>
impl<T> RefUnwindSafe for TypeMap<T>
impl<T> Send for TypeMap<T>
impl<T> Sync for TypeMap<T>
impl<T> Unpin for TypeMap<T>
impl<T> UnsafeUnpin for TypeMap<T>
impl<T> UnwindSafe for TypeMap<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more