[−][src]Struct vial::TypeCache
The TypeCache is heavily inspired by the state
crate and the way
the Rocket framework handles global and local state. You could say
we've immutably borrowed some ideas. Rim-shot!
Basically, we've got a hash that can store different types but
only one of each type. The type id is the key, and you need to
know what type you're asking for when you call get()
to be able
to do anything with it:
let cache = TypeCache::new(); cache.set::<String>("Hi friends".to_string()); assert_eq!(Some(&"Hi friends".to_string()), cache.get::<String>()); cache.set::<usize>(12345); assert_eq!(Some(&12345), cache.get::<usize>());
We use this in Vial for global state (available when compiling
with --features state
) as well as local request state, which is
always available.
Implementations
impl TypeCache
[src]
pub fn new() -> TypeCache
[src]
Create a new, empty TypeCache.
pub fn get<T: Send + Sync + 'static>(&self) -> Option<&T>
[src]
TypeCache works like a regular hash map, but with types as keys. Meaning it can only store one of each type. Choose wisely.
pub fn set<T: Send + Sync + 'static>(&self, v: T)
[src]
As long as your object is Send + Sync + 'static
, TypeCache
can store it.
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for TypeCache
impl !Send for TypeCache
impl !Sync for TypeCache
impl Unpin for TypeCache
impl !UnwindSafe for TypeCache
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
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>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,