use core::num::NonZeroU32;
use crate::{
clock,
errors::InsufficientCapacity,
middleware::{NoOpMiddleware, RateLimitingMiddleware},
state::InMemoryState,
Quota,
};
#[derive(PartialEq, Debug, Eq)]
pub enum NotKeyed {
NonKey,
}
pub trait DirectStateStore: StateStore<Key = NotKeyed> {}
impl<T> DirectStateStore for T where T: StateStore<Key = NotKeyed> {}
#[cfg(feature = "std")]
impl RateLimiter<NotKeyed, InMemoryState, clock::DefaultClock, NoOpMiddleware> {
pub fn direct(
quota: Quota,
) -> RateLimiter<NotKeyed, InMemoryState, clock::DefaultClock, NoOpMiddleware> {
let clock = clock::DefaultClock::default();
Self::direct_with_clock(quota, clock)
}
}
impl<C> RateLimiter<NotKeyed, InMemoryState, C, NoOpMiddleware<C::Instant>>
where
C: clock::Clock,
{
pub fn direct_with_clock(quota: Quota, clock: C) -> Self {
let state: InMemoryState = Default::default();
RateLimiter::new(quota, state, clock)
}
}
impl<S, C, MW> RateLimiter<NotKeyed, S, C, MW>
where
S: DirectStateStore,
C: clock::Clock,
MW: RateLimitingMiddleware<C::Instant>,
{
pub fn check(&self) -> Result<MW::PositiveOutcome, MW::NegativeOutcome> {
self.gcra.test_and_update::<NotKeyed, C::Instant, S, MW>(
self.start,
&NotKeyed::NonKey,
&self.state,
self.clock.now(),
)
}
pub fn check_n(
&self,
n: NonZeroU32,
) -> Result<Result<MW::PositiveOutcome, MW::NegativeOutcome>, InsufficientCapacity> {
self.gcra
.test_n_all_and_update::<NotKeyed, C::Instant, S, MW>(
self.start,
&NotKeyed::NonKey,
n,
&self.state,
self.clock.now(),
)
}
}
#[cfg(feature = "std")]
mod future;
#[cfg(feature = "std")]
mod sinks;
#[cfg(feature = "std")]
pub use sinks::*;
#[cfg(feature = "std")]
mod streams;
use crate::state::{RateLimiter, StateStore};
#[cfg(feature = "std")]
pub use streams::*;
#[cfg(test)]
mod test {
use super::*;
#[test]
fn not_keyed_impls_coverage() {
assert_eq!(NotKeyed::NonKey, NotKeyed::NonKey);
}
}