[−][src]Struct dyn_cache::local::SharedLocalCache
Provides shared, synchronized access to a LocalCache and a function-memoization
API in SharedLocalCache::cache_with.
For convenience wrappers around SharedLocalCache::cache_with see
SharedLocalCache::cache for returned types that implement
Clone and SharedLocalCache::hold for values that just need to be stored
without returning a reference.
Example
let storage = dyn_cache::local::SharedLocalCache::default(); let call_count = std::cell::Cell::new(0); let increment_count = |&to_add: &i32| { let new_count = call_count.get() + to_add; call_count.set(new_count); new_count }; assert_eq!(call_count.get(), 0, "not called yet"); let with_one = storage.cache_with(&'a', &1, &increment_count, Clone::clone); assert_eq!(call_count.get(), 1, "called only once"); assert_eq!(call_count.get(), with_one); let with_one_again = storage.cache_with(&'a', &1, &increment_count, Clone::clone); assert_eq!(call_count.get(), 1, "still called only once, previous value cached"); assert_eq!(call_count.get(), with_one_again); let with_two = storage.cache_with(&'a', &2, &increment_count, Clone::clone); assert_eq!(call_count.get(), 3, "called again with a new, larger increment"); assert_eq!(call_count.get(), with_two); let with_other_query = storage.cache_with(&'b', &1, &increment_count, Clone::clone); assert_eq!(call_count.get(), 4, "called again with the same increment, different scope"); assert_eq!(call_count.get(), with_other_query); let with_two_again = storage.cache_with(&'a', &2, &increment_count, Clone::clone); assert_eq!(call_count.get(), 4, "cell still has last mutation's value"); assert_eq!(with_two_again, with_two, "cache should still have previous value"); storage.gc(); // won't drop any values, but sets all of the cached values to be dropped call_count.set(0); // re-run 'b', marking it live let reran_other_query = storage.cache_with(&'b', &1, &increment_count, Clone::clone); assert_eq!(reran_other_query , 4, "returns the cached value"); assert_eq!(call_count.get(), 0, "without running increment_count"); storage.gc(); // query 'a' will be dropped // re-run 'b', observing cached value let reran_other_query = storage.cache_with(&'b', &1, &increment_count, Clone::clone); assert_eq!(reran_other_query , 4, "still returns the cached value"); assert_eq!(call_count.get(), 0, "still without running increment_count"); // run 'a' again, observe no cached value let with_one_again = storage.cache_with(&'a', &1, &increment_count, Clone::clone); assert_eq!(call_count.get(), 1, "called without caching"); assert_eq!(call_count.get(), with_one_again);
Implementations
impl SharedLocalCache[src]
pub fn cache_with<Key: ?Sized, Scope, Arg: ?Sized, Input, Output, Ret>(
&self,
key: &Key,
arg: &Arg,
init: impl FnOnce(&Input) -> Output,
with: impl FnOnce(&Output) -> Ret
) -> Ret where
Key: Eq + Hash + ToOwned<Owned = Scope>,
Scope: 'static + Borrow<Key> + Eq + Hash,
Arg: PartialEq<Input> + ToOwned<Owned = Input>,
Input: 'static + Borrow<Arg>,
Output: 'static,
Ret: 'static, [src]
&self,
key: &Key,
arg: &Arg,
init: impl FnOnce(&Input) -> Output,
with: impl FnOnce(&Output) -> Ret
) -> Ret where
Key: Eq + Hash + ToOwned<Owned = Scope>,
Scope: 'static + Borrow<Key> + Eq + Hash,
Arg: PartialEq<Input> + ToOwned<Owned = Input>,
Input: 'static + Borrow<Arg>,
Output: 'static,
Ret: 'static,
Caches the result of init(arg) once per key, re-running it when arg changes. Always
runs with on the stored Output before returning the result.
See SharedLocalCache::cache for an ergonomic wrapper that requires Output: Clone.
pub fn cache<Key: ?Sized, Scope, Arg: ?Sized, Input, Output>(
&self,
key: &Key,
arg: &Arg,
init: impl FnOnce(&Input) -> Output
) -> Output where
Key: Eq + Hash + ToOwned<Owned = Scope>,
Scope: 'static + Borrow<Key> + Eq + Hash,
Arg: PartialEq<Input> + ToOwned<Owned = Input>,
Input: 'static + Borrow<Arg>,
Output: 'static + Clone, [src]
&self,
key: &Key,
arg: &Arg,
init: impl FnOnce(&Input) -> Output
) -> Output where
Key: Eq + Hash + ToOwned<Owned = Scope>,
Scope: 'static + Borrow<Key> + Eq + Hash,
Arg: PartialEq<Input> + ToOwned<Owned = Input>,
Input: 'static + Borrow<Arg>,
Output: 'static + Clone,
Caches the result of init(arg) once per key, re-running it when arg changes. Clones
the cached output before returning the result.
See SharedLocalCache::cache_with for a lower-level version which does not require
Output: Clone.
pub fn hold<Key: ?Sized, Scope, Arg: ?Sized, Input, Output>(
&self,
key: &Key,
arg: &Arg,
init: impl FnOnce(&Input) -> Output
) where
Key: Eq + Hash + ToOwned<Owned = Scope>,
Scope: 'static + Borrow<Key> + Eq + Hash,
Arg: PartialEq<Input> + ToOwned<Owned = Input>,
Input: 'static + Borrow<Arg>,
Output: 'static, [src]
&self,
key: &Key,
arg: &Arg,
init: impl FnOnce(&Input) -> Output
) where
Key: Eq + Hash + ToOwned<Owned = Scope>,
Scope: 'static + Borrow<Key> + Eq + Hash,
Arg: PartialEq<Input> + ToOwned<Owned = Input>,
Input: 'static + Borrow<Arg>,
Output: 'static,
Caches the result of init(arg) once per key, re-running it when arg changes.
Does not return any reference to the cached value. See SharedLocalCache::cache
for similar functionality that returns a copy of Output or
SharedLocalCache::cache_with which allows specifying other pre-return functions.
pub fn gc(&self)[src]
Forwards to LocalCache::gc.
Trait Implementations
impl Clone for SharedLocalCache[src]
pub fn clone(&self) -> SharedLocalCache[src]
pub fn clone_from(&mut self, source: &Self)1.0.0[src]
impl Debug for SharedLocalCache[src]
impl Default for SharedLocalCache[src]
pub fn default() -> SharedLocalCache[src]
impl Eq for SharedLocalCache[src]
impl From<LocalCache> for SharedLocalCache[src]
pub fn from(inner: LocalCache) -> Self[src]
impl Hash for SharedLocalCache[src]
pub fn hash<H>(&self, hasher: &mut H) where
H: Hasher, [src]
H: Hasher,
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher, 1.3.0[src]
H: Hasher,
impl Ord for SharedLocalCache[src]
pub fn cmp(&self, other: &Self) -> Ordering[src]
#[must_use]pub fn max(self, other: Self) -> Self1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self1.50.0[src]
impl PartialEq<SharedLocalCache> for SharedLocalCache[src]
pub fn eq(&self, other: &Self) -> bool[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool1.0.0[src]
impl PartialOrd<SharedLocalCache> for SharedLocalCache[src]
pub fn partial_cmp(&self, other: &Self) -> Option<Ordering>[src]
#[must_use]pub fn lt(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]pub fn le(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]pub fn gt(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]pub fn ge(&self, other: &Rhs) -> bool1.0.0[src]
impl RefUnwindSafe for SharedLocalCache[src]
impl UnwindSafe for SharedLocalCache[src]
Auto Trait Implementations
impl !Send for SharedLocalCache[src]
impl !Sync for SharedLocalCache[src]
impl Unpin for SharedLocalCache[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized, [src]
T: 'static + ?Sized,
impl<T> AsContext for T where
T: Debug + 'static, [src]
T: Debug + 'static,
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> CallHasher for T where
T: Hash,
T: Hash,
impl<T> Downcast for T where
T: Any, [src]
T: Any,
pub fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>[src]
pub fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>[src]
pub fn as_any(&self) -> &(dyn Any + 'static)[src]
pub fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)[src]
impl<T> DowncastSync for T where
T: Send + Sync + Any, [src]
T: Send + Sync + Any,
impl<T> Erased for T
impl<T> From<T> for T[src]
impl<T, U> Into<U> for T where
U: From<T>, [src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone, [src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T[src]
pub fn clone_into(&self, target: &mut T)[src]
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>,