LocalCache

Struct LocalCache 

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

Holds arbitrary query results which are namespaced by arbitrary scope types. Usually used through SharedLocalCache::cache_with and SharedLocalCache::gc.

§Query types

Note: the types referenced in this documentation are only visible on individual methods, as LocalCache is not itself a generic type.

Storage is sharded by the type of the query. The type of a query has three parts:

The query scope is the value which indexes the storage for a particular query type, it has the bound Scope: 'static + Eq + Hash.

Each Scope corresponds to at most a single Input: 'static and a single Output: 'static value at any given time.

§Reading stored values

See LocalCache::get which accepts borrowed forms of Scope and Input: Key and Arg respectively. Arg must satisfy PartialEq<Input> to determine whether to return a stored output.

§Garbage Collection

Each time LocalCache::gc is called it removes any values which haven’t been referenced since the prior call.

After each GC, all values still in the cache are marked garbage. They are marked live again when inserted with LocalCache::store or read with LocalCache::get.

Implementations§

Source§

impl LocalCache

Source

pub fn get<'k, Key, Scope, Arg, Input, Output>( &self, key: &'k Key, arg: &Arg, ) -> Result<&Output, CacheMiss<'k, Key, Scope, 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,

Return a reference to a query’s stored output if a result is stored and arg equals the previously-stored Input. If a reference is returned, the stored input/output is marked as a root and will not be GC’d the next call.

If no reference is found, a CacheMiss is returned. Call CacheMiss::init to get a CacheEntry to pass to LocalCache::store.

Source

pub fn store<Key, Scope, Input, Output>( &mut self, entry: CacheEntry<'_, Key, Scope, Input, Output>, )
where Key: Eq + Hash + ToOwned<Owned = Scope> + ?Sized, Scope: 'static + Borrow<Key> + Eq + Hash, Input: 'static, Output: 'static,

Stores a fresh CacheEntry whose input/output will not be GC’d at the next call. Call LocalCache::get to get a CacheMiss and CacheMiss::init to get a CacheEntry.

Source

pub fn gc(&mut self)

Drop any values which have not been marked alive since the last call to this method.

Trait Implementations§

Source§

impl Debug for LocalCache

Source§

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

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

impl Default for LocalCache

Source§

fn default() -> LocalCache

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 RefUnwindSafe for LocalCache

Source§

impl UnwindSafe for LocalCache

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> 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, 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