TimedMap

Struct TimedMap 

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

Provides a hash map with expiring key-value pairs.

§Basic Example

use timedmap::TimedMap;
use std::time::Duration;

let tm = TimedMap::new();
tm.insert("foo", "bar", Duration::from_secs(10));
assert_eq!(tm.get(&"foo"), Some("bar"));

Implementations§

Source§

impl<K, V> TimedMap<K, V>

Source

pub fn new() -> Self

Create a new instance of TimedMap with the default TimeSource implementation Instant.

Source§

impl<K, V, TS> TimedMap<K, V, TS>

Source

pub fn new_with_timesource() -> Self

Create a new instance of TimedMap with a custom TimeSource implementation.

Source§

impl<K, V, TS> TimedMap<K, V, TS>
where K: Eq + PartialEq + Hash + Clone, V: Clone, TS: TimeSource,

Source

pub fn insert(&self, key: K, value: V, lifetime: Duration)

Add a new key-value pair to the map with the given lifetime.

When the lifetime has passed, the key-value pair will be no more accessible.

§Example
use timedmap::TimedMap;
use std::time::Duration;

let tm = TimedMap::new();
tm.insert("foo", "bar", Duration::from_millis(10));
assert_eq!(tm.get(&"foo"), Some("bar"));

std::thread::sleep(Duration::from_millis(20));
assert_eq!(tm.get(&"foo"), None);
Source

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

Returns a copy of the value corresponding to the given key.

None is returned when the values lifetime has been passed.

§Behavior

If the key-value pair has expired and not been cleaned up before, it will be removed from the map on next retrival try.

Source

pub fn contains(&self, key: &K) -> bool

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

§Behavior

Because this method is basically a shorthand for get(key).is_some(), it behaves the same on retrival of expired pairs.

Source

pub fn remove<Q>(&self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes the given key-value pair from the map and returns the value if it was previously in the map and is not expired.

Source

pub fn refresh(&self, key: &K, new_lifetime: Duration) -> bool

Sets the lifetime of the value coresponding to the given key to the new lifetime from now.

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

Source

pub fn extend(&self, key: &K, added_lifetime: Duration) -> bool

Extends the lifetime of the value coresponding to the given key to the new lifetime from now.

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

Source

pub fn len(&self) -> usize

Returns the number of key-value pairs in the map which have not been expired.

Source

pub fn is_empty(&self) -> bool

Returns true when the map does not contain any non-expired key-value pair.

Source

pub fn clear(&self)

Clears the map, removing all key-value pairs.

Source

pub fn snapshot<B: FromIterator<(K, V)>>(&self) -> B

Create a snapshot of the current state of the maps key-value entries.

It does only contain all non-expired key-value pairs.

Source

pub fn get_value<Q>(&self, key: &Q) -> Option<Value<V, TS>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Retrieves the raw Value wrapper by the given key if the key-value pair has not been expired yet.

If the given key-value pair is expired and not cleaned up yet, it will be removed from the map automatically.

Source

pub fn get_value_unchecked<Q>(&self, key: &Q) -> Option<Value<V, TS>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Retrieves the raw Value wrapper by the given key without checking expiry.

Trait Implementations§

Source§

impl<K, V, TS> Cleanup for TimedMap<K, V, TS>
where K: Eq + PartialEq + Hash + Clone + Send + Sync, V: Clone + Send + Sync, TS: TimeSource + Send + Sync,

Source§

fn cleanup(&self)

Cleanup removes all elements which have been expired.
Source§

impl<K: Debug, V: Debug, TS: Debug> Debug for TimedMap<K, V, TS>

Source§

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

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

impl<K, V> Default for TimedMap<K, V>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K, V, TS = Instant> !Freeze for TimedMap<K, V, TS>

§

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

§

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

§

impl<K, V, TS> Sync for TimedMap<K, V, TS>
where K: Send + Sync, V: Send + Sync, TS: Send + Sync,

§

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

§

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

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