use lib::*;
use {
algorithms::{Algorithm, RateLimitState, RateLimitStateWithClock},
instant,
instant::Absolute,
DirectRateLimiter, InconsistentCapacity, NegativeMultiDecision,
};
#[derive(Default, Copy, Clone, Debug)]
pub struct Allower {}
impl Allower {
pub fn ratelimiter() -> DirectRateLimiter<Allower, Always> {
DirectRateLimiter::per_second(nonzero!(1u32))
}
}
impl RateLimitState<Allower, Always> for () {}
impl RateLimitStateWithClock<Allower, Always> for () {
fn last_touched(&self, _params: &Allower) -> Always {
Always::now()
}
}
#[derive(Debug, PartialEq)]
pub enum Impossible {}
impl fmt::Display for Impossible {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "can't happen")
}
}
impl Algorithm<Always> for Allower {
type BucketState = ();
type NegativeDecision = Impossible;
fn construct(
_capacity: NonZeroU32,
_cell_weight: NonZeroU32,
_per_time_unit: Duration,
) -> Result<Self, InconsistentCapacity> {
Ok(Allower {})
}
fn test_n_and_update(
&self,
_state: &Self::BucketState,
_n: u32,
_t0: Always,
) -> Result<(), NegativeMultiDecision<Impossible>> {
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Always();
impl instant::Relative for Always {
fn duration_since(&self, _other: Self) -> Duration {
Duration::new(0, 0)
}
}
impl instant::Absolute for Always {
fn now() -> Self {
Always()
}
}
impl Add<Duration> for Always {
type Output = Always;
fn add(self, _rhs: Duration) -> Always {
Always()
}
}
impl Sub<Duration> for Always {
type Output = Always;
fn sub(self, _rhs: Duration) -> Always {
Always()
}
}