TimedMap

Struct TimedMap 

Source
pub struct TimedMap<K, V, C = StdClock> { /* private fields */ }
Expand description

Associates keys of type K with values of type V. Each entry may optionally expire after a specified duration.

Mutable functions automatically clears expired entries when called.

If no expiration is set, the entry remains constant.

Implementations§

Source§

impl<K, V> TimedMap<K, V, StdClock>
where K: GenericKey,

Source

pub fn new() -> Self

Creates an empty map.

Source

pub fn new_with_map_kind(map_kind: MapKind) -> Self

Creates an empty map based on the chosen map implementation specified by MapKind.

Source§

impl<K, V, C> TimedMap<K, V, C>
where C: Clock, K: GenericKey,

Source

pub fn expiration_tick_cap(self, expiration_tick_cap: u16) -> Self

Configures expiration_tick_cap, which sets how often TimedMap::drop_expired_entries is automatically called. The default value is 1.

On each insert (excluding unchecked ones), an internal counter expiration_tick is incremented. When expiration_tick meets or exceeds expiration_tick_cap, TimedMap::drop_expired_entries is triggered to remove expired entries.

Use this to control cleanup frequency and optimize performance. For example, if your workload involves about 100 inserts within couple seconds, setting expiration_tick_cap to 100 can improve the performance significantly.

Source

pub fn get(&self, k: &K) -> Option<&V>

Returns the associated value if present and not expired.

To retrieve the value without checking expiration, use TimedMap::get_unchecked.

Source

pub fn get_mut(&mut self, k: &K) -> Option<&mut V>

Returns a mutable reference to the value corresponding to the key.

To retrieve the value without checking expiration, use TimedMap::get_mut_unchecked.

Source

pub fn get_unchecked(&self, k: &K) -> Option<&V>

Returns the associated value if present, regardless of whether it is expired.

If you only want non-expired entries, use TimedMap::get instead.

Source

pub fn get_mut_unchecked(&mut self, k: &K) -> Option<&mut V>

Returns a mutable reference to the associated value if present, regardless of whether it is expired.

If you only want non-expired entries, use TimedMap::get_mut instead.

Source

pub fn get_remaining_duration(&self, k: &K) -> Option<Duration>

Returns the associated value’s Duration if present and not expired.

Returns None if the entry does not exist or is constant.

Source

pub fn len(&self) -> usize

Returns the number of unexpired elements in the map.

See TimedMap::len_expired and TimedMap::len_unchecked for other usages.

Source

pub fn len_expired(&self) -> usize

Returns the number of expired elements in the map.

See TimedMap::len and TimedMap::len_unchecked for other usages.

Source

pub fn len_unchecked(&self) -> usize

Returns the total number of elements (including expired ones) in the map.

See TimedMap::len and TimedMap::len_expired for other usages.

Source

pub fn keys(&self) -> Vec<K>

Returns keys of the map

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

Source

pub fn insert_expirable(&mut self, k: K, v: V, duration: Duration) -> Option<V>

Inserts a key-value pair with an expiration duration, and then drops the expired entries.

If a value already exists for the given key, it will be updated and then the old one will be returned.

If you don’t want to the check expired entries, consider using TimedMap::insert_expirable_unchecked instead.

Source

pub fn insert_expirable_unchecked( &mut self, k: K, v: V, duration: Duration, ) -> Option<V>

Inserts a key-value pair with an expiration duration, without checking the expired entries.

If a value already exists for the given key, it will be updated and then the old one will be returned.

If you want to check the expired entries, consider using TimedMap::insert_expirable instead.

Source

pub fn insert_constant(&mut self, k: K, v: V) -> Option<V>

Inserts a key-value pair with that doesn’t expire, and then drops the expired entries.

If a value already exists for the given key, it will be updated and then the old one will be returned.

If you don’t want to check the expired entries, consider using TimedMap::insert_constant_unchecked instead.

Source

pub fn insert_constant_unchecked(&mut self, k: K, v: V) -> Option<V>

Inserts a key-value pair with that doesn’t expire without checking the expired entries.

If a value already exists for the given key, it will be updated and then the old one will be returned.

If you want to check the expired entries, consider using TimedMap::insert_constant instead.

Source

pub fn remove(&mut self, k: &K) -> Option<V>

Removes a key-value pair from the map and returns the associated value if present and not expired.

If you want to retrieve the entry after removal even if it is expired, consider using TimedMap::remove_unchecked.

Source

pub fn remove_unchecked(&mut self, k: &K) -> Option<V>

Removes a key-value pair from the map and returns the associated value if present, regardless of expiration status.

If you only want the entry when it is not expired, consider using TimedMap::remove.

Source

pub fn clear(&mut self)

Clears the map, removing all elements.

Source

pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>

Returns an iterator over non-expired key-value pairs.

Source

pub fn iter_unchecked(&self) -> impl Iterator<Item = (&K, &V)>

Returns an iterator over all key-value pairs, including expired ones.

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)>

Returns a mutable iterator over non-expired key-value pairs.

Source

pub fn iter_mut_unchecked(&mut self) -> impl Iterator<Item = (&K, &mut V)>

Returns a mutable iterator over all key-value pairs, including expired ones.

Source

pub fn update_expiration_status( &mut self, key: K, duration: Duration, ) -> Result<Option<EntryStatus>, &'static str>

Updates the expiration status of an entry and returns the old status.

If the entry does not exist, returns Err. If the entry’s old status is EntryStatus::Constant, returns None.

Source

pub fn drop_expired_entries(&mut self) -> Vec<(K, V)>

Clears expired entries from the map and returns them.

Call this function when using *_unchecked inserts, as these do not automatically clear expired entries.

Source

pub fn contains_key(&self, k: &K) -> bool

Returns true if the map contains a non-expired value for the given key.

To include expired entries as well, use TimedMap::contains_key_unchecked.

Source

pub fn contains_key_unchecked(&self, k: &K) -> bool

Returns true if the map contains a value for the given key regardless of expiration status.

To exclude expired entries, use TimedMap::contains_key instead.

Trait Implementations§

Source§

impl<K: Clone, V: Clone, C: Clone> Clone for TimedMap<K, V, C>

Source§

fn clone(&self) -> TimedMap<K, V, C>

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<K: Debug, V: Debug, C: Debug> Debug for TimedMap<K, V, C>

Source§

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

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

impl<K, V, C> Default for TimedMap<K, V, C>
where C: Default,

Source§

fn default() -> Self

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

impl<'a, K, V, C> IntoIterator for &'a TimedMap<K, V, C>
where K: GenericKey, C: Clock,

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<(&'a K, &'a V)>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K, V, C> IntoIterator for &'a mut TimedMap<K, V, C>
where K: GenericKey, C: Clock,

Source§

type Item = (&'a K, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<(&'a K, &'a mut V)>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V, C> IntoIterator for TimedMap<K, V, C>
where K: GenericKey, C: Clock,

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<(K, V)>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<K, V, C> Freeze for TimedMap<K, V, C>
where C: Freeze,

§

impl<K, V, C> RefUnwindSafe for TimedMap<K, V, C>

§

impl<K, V, C> Send for TimedMap<K, V, C>
where C: Send, K: Send, V: Send,

§

impl<K, V, C> Sync for TimedMap<K, V, C>
where C: Sync, K: Sync, V: Sync,

§

impl<K, V, C> Unpin for TimedMap<K, V, C>
where C: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, C> UnwindSafe for TimedMap<K, V, C>

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