pub(super) struct BoundedMap<K, V> {
entries: VecDeque<(K, Instant, V)>,
capacity: usize,
max_duration: Duration,
}Expand description
A map bounded by element age and maximum amount of items. Elements only get inserted and taken. A full map takes no more elements until one of them got taken or expired, a valid element never gets dropped for a new one. Clients take their element right after it got inserted, the map holds few of them and a linear search is quick enough.
Fields§
§entries: VecDeque<(K, Instant, V)>The elements with the time of their insert, the oldest first.
capacity: usize§max_duration: DurationImplementations§
Source§impl<K, V> BoundedMap<K, V>
impl<K, V> BoundedMap<K, V>
Sourcepub(super) fn insert(&mut self, k: K, v: V) -> Result<(), V>
pub(super) fn insert(&mut self, k: K, v: V) -> Result<(), V>
Insert an element into the map, after removing the timed-out ones. Returns the new one back if the map is full. The keys are random tokens, which do not repeat, so the map does not look for an element with the same key.
Sourcepub(super) fn remove(&mut self, k: &K) -> Option<V>
pub(super) fn remove(&mut self, k: &K) -> Option<V>
Remove an element from the map and return it if the element has not expired. The expired ones get removed as well, so that they do not linger until the next insert, they may hold sockets.
Sourcepub(super) fn contains_key(&self, k: &K) -> bool
pub(super) fn contains_key(&self, k: &K) -> bool
Whether the map holds an element for the key which has not expired, without taking it.
Sourcepub(super) fn get(&self, k: &K) -> Option<&V>
pub(super) fn get(&self, k: &K) -> Option<&V>
The element for the key if it has not expired, without taking it.
Sourcepub(super) fn sweep(&mut self)
pub(super) fn sweep(&mut self)
Remove the expired elements, which may hold resources until then.
fn position(&self, k: &K) -> Option<usize>
Sourcefn remove_expired(&mut self, now: Instant)
fn remove_expired(&mut self, now: Instant)
Remove expired items from the oldest end. If the oldest one is not expired, no entries are expired, so this can stop early.
Trait Implementations§
Source§impl<K, V> Debug for BoundedMap<K, V>
impl<K, V> Debug for BoundedMap<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for BoundedMap<K, V>
impl<K, V> RefUnwindSafe for BoundedMap<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for BoundedMap<K, V>
impl<K, V> Sync for BoundedMap<K, V>
impl<K, V> Unpin for BoundedMap<K, V>
impl<K, V> UnsafeUnpin for BoundedMap<K, V>
impl<K, V> UnwindSafe for BoundedMap<K, V>where
K: UnwindSafe,
V: UnwindSafe,
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
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].