[−][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 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
Auto Trait Implementations
impl !RefUnwindSafe for TypeCache
[src]
impl !Send for TypeCache
[src]
impl !Sync for TypeCache
[src]
impl Unpin for TypeCache
[src]
impl !UnwindSafe for TypeCache
[src]
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,
pub 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.
pub 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>,