[][src]Struct vial::TypeCache

pub struct TypeCache { /* fields omitted */ }

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 as well as local request state, however this design is flawed as the local cache shouldn't be forced into Send + Sync. These two will be separated in a future release.

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

impl Debug for TypeCache[src]

impl Default for TypeCache[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.