pub struct SizeBoundedMap<K, V> { /* private fields */ }Expand description
A lock-free, size-bounded concurrent map.
Reads and writes go through a papaya HashMap; the approximate total
size is tracked in an AtomicUsize. Eviction is approximate and happens
at insert time under the caller’s control (see Self::evict_until_freed),
which keeps get entirely lock-free.
Implementations§
Source§impl<K, V> SizeBoundedMap<K, V>
impl<K, V> SizeBoundedMap<K, V>
Sourcepub fn new(max_size_bytes: usize) -> Self
pub fn new(max_size_bytes: usize) -> Self
Creates a new map with the specified maximum size in bytes.
A max_size_bytes of 0 disables the cache: lookups always miss and
inserts are no-ops.
Sourcepub fn is_disabled(&self) -> bool
pub fn is_disabled(&self) -> bool
Returns true when the cache is disabled (max_size_bytes == 0).
Sourcepub fn current_size(&self) -> usize
pub fn current_size(&self) -> usize
Returns the current approximate accounted size in bytes.
Sourcepub fn with_entry<Q, R>(
&self,
key: &Q,
f: impl FnOnce(&Entry<V>) -> R,
) -> Option<R>
pub fn with_entry<Q, R>( &self, key: &Q, f: impl FnOnce(&Entry<V>) -> R, ) -> Option<R>
Applies f to the entry for key, if present.
Returns None when the key is absent or the cache is disabled. This
gives callers access to the entry’s bookkeeping (e.g. inserted_at
for TTL checks) without cloning more than they need.
Sourcepub fn insert_weighted(
&self,
key: K,
value: V,
size_bytes: usize,
) -> (Option<V>, usize)where
V: Clone,
pub fn insert_weighted(
&self,
key: K,
value: V,
size_bytes: usize,
) -> (Option<V>, usize)where
V: Clone,
Inserts value accounted at size_bytes, timestamped now.
Sourcepub fn insert_weighted_at(
&self,
key: K,
value: V,
size_bytes: usize,
inserted_at: Instant,
) -> (Option<V>, usize)where
V: Clone,
pub fn insert_weighted_at(
&self,
key: K,
value: V,
size_bytes: usize,
inserted_at: Instant,
) -> (Option<V>, usize)where
V: Clone,
Inserts value accounted at size_bytes with an explicit insertion
timestamp (exposed for TTL tests that need to backdate entries).
If an entry already existed for this key, its accounted size is subtracted first so the running total reflects the replacement rather than ratcheting upward on every overwrite.
Returns the replaced value (if any) and the new approximate total
size. Eviction is not performed here; callers decide if and how to
evict based on the returned total (see Self::evict_until_freed).
Sourcepub fn claim_or<R>(
&self,
key: K,
value: V,
size_bytes: usize,
decide: impl Fn(&Entry<V>) -> Option<R>,
) -> Claim<R>where
V: Clone,
pub fn claim_or<R>(
&self,
key: K,
value: V,
size_bytes: usize,
decide: impl Fn(&Entry<V>) -> Option<R>,
) -> Claim<R>where
V: Clone,
Atomically installs value for key unless an existing entry is kept.
decide is consulted for an entry that is already present: it returns
Some(reason) to keep that entry (nothing is written, reason is
handed back) or None to replace it. A vacant slot is always claimed.
The read-modify-write is a single papaya compare-and-swap, so of many
concurrent callers exactly one claims a given slot. A get followed by
an insert cannot promise that: every caller can observe the vacancy
and every caller then claims it, which is how single-flight guards leak
duplicate producers.
decide may run more than once (the CAS retries when the entry changes
underneath it), so it must be free of side effects.
When the cache is disabled nothing is stored and every caller is told it claimed the slot — with no storage there is nothing to deduplicate on.
Sourcepub fn remove_if(&self, key: K, predicate: impl Fn(&Entry<V>) -> bool) -> bool
pub fn remove_if(&self, key: K, predicate: impl Fn(&Entry<V>) -> bool) -> bool
Removes the entry for key when predicate accepts it, adjusting the
accounted total. Returns whether an entry was removed.
The removal is a single atomic compare-and-remove, which is what makes
this safe for withdrawing a marker a caller owns: if another writer has
already replaced the entry, predicate sees the new value and can
decline, so a concurrent legitimate write is never clobbered.
Sourcepub fn evict_until_freed<P: Ord>(
&self,
target_bytes: usize,
priority: impl Fn(&K, &Entry<V>) -> Option<P>,
) -> EvictionStats
pub fn evict_until_freed<P: Ord>( &self, target_bytes: usize, priority: impl Fn(&K, &Entry<V>) -> Option<P>, ) -> EvictionStats
Removes evictable entries in ascending priority order until at least
target_bytes have been freed (or no candidates remain).
priority returns Some(order_key) for evictable entries — smaller
keys are evicted first — and None for entries that must be kept.
Auto Trait Implementations§
impl<K, V> !Freeze for SizeBoundedMap<K, V>
impl<K, V> RefUnwindSafe for SizeBoundedMap<K, V>
impl<K, V> Send for SizeBoundedMap<K, V>
impl<K, V> Sync for SizeBoundedMap<K, V>
impl<K, V> Unpin for SizeBoundedMap<K, V>
impl<K, V> UnsafeUnpin for SizeBoundedMap<K, V>
impl<K, V> UnwindSafe for SizeBoundedMap<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
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> ⓘ
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 moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);