#![allow(unused)]
use core::num::NonZeroU32;
use crate::{
error::InsufficientCapacity,
gcra::NotUntil,
quota::Quota,
snapshot::RateSnapshot,
state::InMemoryState,
timer::{DefaultTimer, Timer},
};
#[derive(PartialEq, Debug, Eq)]
pub enum NotKeyed {
NonKey,
}
pub(crate) trait DirectStateStore: StateStore<Key = NotKeyed> {}
impl<T> DirectStateStore for T where T: StateStore<Key = NotKeyed> {}
impl RateLimiter<NotKeyed, InMemoryState, DefaultTimer> {
pub(crate) fn direct(quota: Quota) -> RateLimiter<NotKeyed, InMemoryState, DefaultTimer> {
let clock = DefaultTimer;
Self::direct_with_clock(quota, &clock)
}
}
impl<C> RateLimiter<NotKeyed, InMemoryState, C>
where
C: Timer,
{
pub(crate) fn direct_with_clock(quota: Quota, clock: &C) -> Self {
let state: InMemoryState = Default::default();
RateLimiter::new(quota, state, clock)
}
}
impl<S, C> RateLimiter<NotKeyed, S, C>
where
S: DirectStateStore,
C: Timer,
{
pub(crate) fn check(&self) -> Result<RateSnapshot, NotUntil<C::Instant>> {
self.gcra.test_and_update::<NotKeyed, C::Instant, S>(
self.start,
&NotKeyed::NonKey,
&self.state,
self.clock.now(),
)
}
#[cfg(test)]
pub(crate) fn check_n(
&self,
n: NonZeroU32,
) -> Result<Result<RateSnapshot, NotUntil<C::Instant>>, InsufficientCapacity> {
self.gcra.test_n_all_and_update::<NotKeyed, C::Instant, S>(
self.start,
&NotKeyed::NonKey,
n,
&self.state,
self.clock.now(),
)
}
}
use crate::state::{RateLimiter, StateStore};
#[cfg(test)]
mod test {
use super::*;
#[test]
fn not_keyed_impls_coverage() {
assert_eq!(NotKeyed::NonKey, NotKeyed::NonKey);
}
}