pub struct CacheSetter<T, Fn: FnOnce() -> T = fn() -> T>(Arc<SetOnce<T>>, Option<Fn>);Expand description
A value returned on cache misses. The owner of this struct should compute
the value, then call CacheSetter::set to write the value into the cache.
If this struct is dropped before being written to (including due to panic),
the value will remain empty and associated CacheFuts will never resolve.
This can be avoided by calling CacheSetter::with_fallback which will
specify a fallback closure in case it is prematurely dropped.
Tuple Fields§
§0: Arc<SetOnce<T>>§1: Option<Fn>Implementations§
Source§impl<T, Fn: FnOnce() -> T> CacheSetter<T, Fn>
impl<T, Fn: FnOnce() -> T> CacheSetter<T, Fn>
Sourcepub const fn new(arc: Arc<SetOnce<T>>) -> Self
pub const fn new(arc: Arc<SetOnce<T>>) -> Self
Constructs a new CacheSetter writing into the given SetOnce.
By default, no fallback is configured.
Sourcepub fn with_fallback<F: FnOnce() -> T>(self, default: F) -> CacheSetter<T, F>
pub fn with_fallback<F: FnOnce() -> T>(self, default: F) -> CacheSetter<T, F>
Returns a new CacheSetter with the configured fallback closure and
writing into the same SetOnce.
Sourcepub fn set(self, value: T)
pub fn set(self, value: T)
Writes the given value into the cache, consuming this CacheSetter.
Sourcepub fn dissociated() -> Self
pub fn dissociated() -> Self
Returns a new dissociated CacheSetter. That is, a setter which is
not backed by any value within the cache. This can be useful to let
uncacheable entities use the same cache-handling logic.
Trait Implementations§
Source§impl<T, Fn: FnOnce() -> T> Drop for CacheSetter<T, Fn>
Drop implementation that calls the stored CacheSetter::with_fallback
closure, if it is configured and no value has been manually stored.
impl<T, Fn: FnOnce() -> T> Drop for CacheSetter<T, Fn>
Drop implementation that calls the stored CacheSetter::with_fallback
closure, if it is configured and no value has been manually stored.
Auto Trait Implementations§
impl<T, Fn> Freeze for CacheSetter<T, Fn>where
Fn: Freeze,
impl<T, Fn = fn() -> T> !RefUnwindSafe for CacheSetter<T, Fn>
impl<T, Fn> Send for CacheSetter<T, Fn>
impl<T, Fn> Sync for CacheSetter<T, Fn>
impl<T, Fn> Unpin for CacheSetter<T, Fn>where
Fn: Unpin,
impl<T, Fn> UnsafeUnpin for CacheSetter<T, Fn>where
Fn: UnsafeUnpin,
impl<T, Fn = fn() -> T> !UnwindSafe for CacheSetter<T, Fn>
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
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>
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>
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