SharedLocalCache

Struct SharedLocalCache 

Source
pub struct SharedLocalCache { /* private fields */ }
Expand description

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§

Source§

impl SharedLocalCache

Source

pub fn cache_with<Key, Scope, Arg, 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> + ?Sized, Scope: 'static + Borrow<Key> + Eq + Hash, Arg: PartialEq<Input> + ToOwned<Owned = Input> + ?Sized, 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.

Source

pub fn cache<Key, Scope, Arg, Input, Output>( &self, key: &Key, arg: &Arg, init: impl FnOnce(&Input) -> Output, ) -> Output
where Key: Eq + Hash + ToOwned<Owned = Scope> + ?Sized, Scope: 'static + Borrow<Key> + Eq + Hash, Arg: PartialEq<Input> + ToOwned<Owned = Input> + ?Sized, 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.

Source

pub fn hold<Key, Scope, Arg, Input, Output>( &self, key: &Key, arg: &Arg, init: impl FnOnce(&Input) -> Output, )
where Key: Eq + Hash + ToOwned<Owned = Scope> + ?Sized, Scope: 'static + Borrow<Key> + Eq + Hash, Arg: PartialEq<Input> + ToOwned<Owned = Input> + ?Sized, 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.

Source

pub fn gc(&self)

Forwards to LocalCache::gc.

Trait Implementations§

Source§

impl Clone for SharedLocalCache

Source§

fn clone(&self) -> SharedLocalCache

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SharedLocalCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SharedLocalCache

Source§

fn default() -> SharedLocalCache

Returns the “default value” for a type. Read more
Source§

impl From<LocalCache> for SharedLocalCache

Source§

fn from(inner: LocalCache) -> Self

Converts to this type from the input type.
Source§

impl Hash for SharedLocalCache

Source§

fn hash<H>(&self, hasher: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for SharedLocalCache

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for SharedLocalCache

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for SharedLocalCache

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for SharedLocalCache

Source§

impl RefUnwindSafe for SharedLocalCache

Source§

impl UnwindSafe for SharedLocalCache

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsContext for T
where T: Debug + 'static,

Source§

fn offer<R>(self, op: impl FnOnce() -> R) -> R

Call op within the context of a Layer containing self.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CallHasher for T
where T: Hash + ?Sized,

Source§

default fn get_hash<H, B>(value: &H, build_hasher: &B) -> u64
where H: Hash + ?Sized, B: BuildHasher,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Erased for T