Struct cachemap::CacheMap[][src]

pub struct CacheMap<K: Hash + Eq, V: ?Sized> { /* fields omitted */ }

An insert-only map for caching the result of functions

Implementations

impl<K: Hash + Eq, V> CacheMap<K, V>[src]

pub fn cache<F: FnOnce() -> V>(&self, key: K, f: F) -> ArcRef<'_, V>[src]

Fetch the value associated with the key, or run the profvided function to insert one.

#Example

use cachemap::CacheMap;

let m = CacheMap::new();

let fst = m.cache("key", || 5u32).as_ref();
let snd = m.cache("key", || 7u32).as_ref();

assert_eq!(*fst, *snd);
assert_eq!(*fst, 5u32);

impl<K: Hash + Eq, V: ?Sized> CacheMap<K, V>[src]

pub fn new() -> Self[src]

Creates a new CacheMap

pub fn cache_arc<F: FnOnce() -> Arc<V>>(&self, key: K, f: F) -> ArcRef<'_, V>[src]

Fetch the value associated with the key, or run the profvided function to insert one. With this version, the function returns an Arc, whch allows caching unsized types.

#Example

use cachemap::CacheMap;

let m: CacheMap<_, [usize]> = CacheMap::new();

let a = m.cache_arc("a", || {
	let a = &[1,2,3][..];
    a.into()
}).as_ref();

let b = m.cache_arc("b", || {
	let b = &[9,9][..];
    b.into()
}).as_ref();

assert_eq!(a, &[1,2,3]);
assert_eq!(b, &[9,9]);

Auto Trait Implementations

impl<K, V> !RefUnwindSafe for CacheMap<K, V>

impl<K, V: ?Sized> Send for CacheMap<K, V> where
    K: Send,
    V: Send + Sync

impl<K, V: ?Sized> Sync for CacheMap<K, V> where
    K: Send + Sync,
    V: Send + Sync

impl<K, V: ?Sized> Unpin for CacheMap<K, V>

impl<K, V: ?Sized> UnwindSafe for CacheMap<K, V> where
    K: UnwindSafe,
    V: RefUnwindSafe

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.