pub struct TypeStore { /* private fields */ }
Expand description
A generic type map for storing arbitrary data by type.
§Example
use extractors::TypeStore;
let mut store = TypeStore::new();
store.insert(1u32);
store.insert("hello");
assert_eq!(store.get::<u32>(), Some(&1u32));
assert_eq!(store.get::<&str>(), Some(&"hello"));
assert_eq!(store.get::<u64>(), None);
Implementations§
Source§impl TypeStore
impl TypeStore
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty Store
.
§Example
use extractors::TypeStore;
let store = TypeStore::new();
assert!(store.is_empty());
Sourcepub fn insert<T: 'static>(&mut self, val: T)
pub fn insert<T: 'static>(&mut self, val: T)
Insert an item into the map.
If an item of this type was already stored, it will be replaced.
§Example
use extractors::TypeStore;
let mut store = TypeStore::new();
store.insert(1u32);
assert_eq!(store.get::<u32>(), Some(&1u32));
store.insert(2u32);
assert_eq!(store.get::<u32>(), Some(&2u32));
Sourcepub fn get<T: 'static>(&self) -> Option<&T>
pub fn get<T: 'static>(&self) -> Option<&T>
Get a reference to an item in the map.
Returns None
if the item is not present.
§Example
use extractors::TypeStore;
let mut store = TypeStore::new();
store.insert(1u32);
assert_eq!(store.get::<u32>(), Some(&1u32));
assert_eq!(store.get::<u64>(), None);
Sourcepub fn get_mut<T: 'static>(&mut self) -> Option<&mut T>
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T>
Get a mutable reference to an item in the map.
Returns None
if the item is not present.
§Example
use extractors::TypeStore;
let mut store = TypeStore::new();
store.insert(1u32);
let val = store.get_mut::<u32>().unwrap();
*val = 2;
assert_eq!(store.get::<u32>(), Some(&2u32));
Sourcepub fn remove<T: 'static>(&mut self) -> Option<T>
pub fn remove<T: 'static>(&mut self) -> Option<T>
Remove an item from the map.
Returns None
if the item is not present, Some(T)
if it was.
§Example
use extractors::TypeStore;
let mut store = TypeStore::new();
store.insert(1u32);
assert_eq!(store.remove::<u32>(), Some(1u32));
assert_eq!(store.remove::<u32>(), None);
Sourcepub fn contains<T: 'static>(&self) -> bool
pub fn contains<T: 'static>(&self) -> bool
Check if the map contains an item of type T
.
Returns true
if it does, false
if it doesn’t.
§Example
use extractors::TypeStore;
let mut store = TypeStore::new();
store.insert(1u32);
assert!(store.contains::<u32>());
assert!(!store.contains::<u64>());
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TypeStore
impl !RefUnwindSafe for TypeStore
impl !Send for TypeStore
impl !Sync for TypeStore
impl Unpin for TypeStore
impl !UnwindSafe for TypeStore
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