1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use crate::lib::*;
use crate::{
algorithms::{Algorithm, RateLimitState},
clock, DirectRateLimiter, InconsistentCapacity, NegativeMultiDecision,
};
#[derive(Default, Copy, Clone, Debug)]
pub struct Allower {}
impl Allower {
pub fn ratelimiter() -> DirectRateLimiter<Allower, ForeverClock> {
DirectRateLimiter::per_second(nonzero!(1u32))
}
}
impl RateLimitState<Allower, Always> for () {
fn last_touched(&self, _params: &Allower) -> Option<Always> {
None
}
}
#[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 clock::Reference for Always {
fn duration_since(&self, _other: Self) -> Duration {
Duration::new(0, 0)
}
fn saturating_sub(&self, _: Duration) -> Self {
*self
}
}
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()
}
}
#[derive(Default, Debug, Clone)]
pub struct ForeverClock();
impl ForeverClock {
pub fn now() -> Always {
Always()
}
}
impl clock::Clock for ForeverClock {
type Instant = Always;
fn now(&self) -> Self::Instant {
Always()
}
}