Struct TypeStore

Source
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

Source

pub fn new() -> Self

Creates an empty Store.

§Example
use extractors::TypeStore;

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

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

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

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

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

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

pub fn clear(&mut self)

Clear the map, removing all items.

§Example
use extractors::TypeStore;

let mut store = TypeStore::new();
store.insert(1u32);
store.clear();
assert!(store.is_empty());
Source

pub fn is_empty(&self) -> bool

Check if the map is empty. Returns true if it is, false if it isn’t.

§Example
use extractors::TypeStore;

let mut store = TypeStore::new();
assert!(store.is_empty());
store.insert(1u32);
assert!(!store.is_empty());

Trait Implementations§

Source§

impl Debug for TypeStore

Source§

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

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

impl Default for TypeStore

Source§

fn default() -> TypeStore

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

Auto Trait Implementations§

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