Skip to main content

FifoCache

Struct FifoCache 

Source
pub struct FifoCache<T, const N: usize>
where T: Clone + Debug + Eq + Hash,
{ /* private fields */ }
Expand description

A bounded cache that maintains a set of IDs with O(1) lookups.

Uses an ArrayDeque for FIFO ordering and an AHashSet for fast membership checks. When capacity is exceeded, the oldest entry is automatically evicted.

§Examples

use nautilus_common::cache::fifo::FifoCache;

let mut cache: FifoCache<u32, 3> = FifoCache::new();
cache.add(1);
cache.add(2);
cache.add(3);
assert!(cache.contains(&1));

// Adding beyond capacity evicts the oldest
cache.add(4);
assert!(!cache.contains(&1));
assert!(cache.contains(&4));

Zero capacity is a compile-time error:

use nautilus_common::cache::fifo::FifoCache;

// This fails to compile: capacity must be > 0
let cache: FifoCache<u32, 0> = FifoCache::new();

Default also enforces non-zero capacity:

use nautilus_common::cache::fifo::FifoCache;

// This also fails to compile
let cache: FifoCache<u32, 0> = FifoCache::default();

Implementations§

Source§

impl<T, const N: usize> FifoCache<T, N>
where T: Clone + Debug + Eq + Hash,

Source

pub fn new() -> Self

Creates a new empty FifoCache with capacity N.

§Panics

Compile-time panic if N == 0.

Source

pub const fn capacity(&self) -> usize

Returns the capacity of the cache.

Source

pub fn len(&self) -> usize

Returns the number of IDs in the cache.

Source

pub fn is_empty(&self) -> bool

Returns whether the cache is empty.

Source

pub fn contains(&self, id: &T) -> bool

Returns whether the cache contains the given ID (O(1) lookup).

Source

pub fn add(&mut self, id: T)

Adds an ID to the cache.

If the ID already exists, this is a no-op. If the cache is at capacity, the oldest entry is evicted.

Source

pub fn remove(&mut self, id: &T)

Removes an ID from the cache.

Source

pub fn clear(&mut self)

Clears all entries from the cache.

Trait Implementations§

Source§

impl<T, const N: usize> Debug for FifoCache<T, N>
where T: Clone + Debug + Eq + Hash + Debug,

Source§

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

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

impl<T, const N: usize> Default for FifoCache<T, N>
where T: Clone + Debug + Eq + Hash,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for FifoCache<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for FifoCache<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for FifoCache<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for FifoCache<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for FifoCache<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnsafeUnpin for FifoCache<T, N>
where T: UnsafeUnpin,

§

impl<T, const N: usize> UnwindSafe for FifoCache<T, N>
where T: 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
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

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
§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

§

impl<T> Ungil for T
where T: Send,