pub struct OnceCache<K: Eq + Hash + Clone, V: Clone> { /* private fields */ }Expand description
Per-key once-init cache. See module docs for the rationale and pattern.
K is the cache key (hashable, cloneable, equatable). V
is the cached value type — typically Arc<Something> so
the clone on hit is just a refcount bump.
Implementations§
Source§impl<K: Eq + Hash + Clone, V: Clone> OnceCache<K, V>
impl<K: Eq + Hash + Clone, V: Clone> OnceCache<K, V>
Sourcepub fn get_or_init<F>(&self, key: K, init: F) -> Result<V, String>
pub fn get_or_init<F>(&self, key: K, init: F) -> Result<V, String>
Get the value for key, computing it via init exactly
once across all concurrent callers. Subsequent callers
for the same key see the cached Result (including
errors — see module docs §“Failure semantics”).
init may take seconds (HTTPS download, cert load, file
open). The outer Mutex is only held long enough to
install the per-key slot; init runs without it,
guarded only by the per-key OnceLock.
Trait Implementations§
Auto Trait Implementations§
impl<K, V> !Freeze for OnceCache<K, V>
impl<K, V> RefUnwindSafe for OnceCache<K, V>
impl<K, V> Send for OnceCache<K, V>
impl<K, V> Sync for OnceCache<K, V>
impl<K, V> Unpin for OnceCache<K, V>
impl<K, V> UnsafeUnpin for OnceCache<K, V>
impl<K, V> UnwindSafe for OnceCache<K, V>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more