Skip to main content

BoundedMap

Struct BoundedMap 

Source
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: Duration

Implementations§

Source§

impl<K, V> BoundedMap<K, V>
where K: Eq + Debug,

Source

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.

Source

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.

Source

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.

Source

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

The element for the key if it has not expired, without taking it.

Source

pub(super) fn sweep(&mut self)

Remove the expired elements, which may hold resources until then.

Source

fn position(&self, k: &K) -> Option<usize>

Source

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.

Source

pub(super) fn is_empty(&self) -> bool

Whether the map holds no elements, also no expired ones.

Trait Implementations§

Source§

impl<K, V> Debug for BoundedMap<K, V>

Source§

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

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

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

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K, V> Freeze for BoundedMap<K, V>

§

impl<K, V> RefUnwindSafe for BoundedMap<K, V>

§

impl<K, V> Send for BoundedMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for BoundedMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for BoundedMap<K, V>
where K: Unpin, V: Unpin,

§

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

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more