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
//! A minimal, auditable, lock-free token bucket rate limiter.
//!
//! # Design goals
//! - Zero dependencies
//! - Zero heap allocations
//! - No `unsafe`
//! - `no_std` compatible core
//! - Deterministic tests via custom [`Clock`] implementations
//!
//! # Behavior guarantees
//! - [`RateLimiter::remaining`] is always bounded by `capacity`
//! - [`RateLimiter::allow_n`] is atomic for token deduction
//! - If time goes backwards (`now < last_refill`), refill is skipped
//! - All public operations are panic-free
//!
//! # Example (`std`)
//! ```rust
//! # #[cfg(feature = "std")]
//! # {
//! use ratelock::RateLimiter;
//!
//! let limiter = RateLimiter::new(10, 5);
//! assert!(limiter.allow());
//! assert_eq!(limiter.remaining(), 9);
//! # }
//! ```
//!
//! # Example (`no_std` compatible API)
//! ```rust
//! use ratelock::{Clock, RateLimiter};
//!
//! struct FixedClock(u64);
//! impl Clock for FixedClock {
//! fn now_ns(&self) -> u64 {
//! self.0
//! }
//! }
//!
//! let clock = FixedClock(0);
//! let limiter = RateLimiter::with_clock(3, 0, clock);
//! assert!(limiter.allow());
//! assert!(limiter.allow());
//! assert!(limiter.allow());
//! assert!(!limiter.allow());
//! ```
pub use Clock;
pub use StdClock;
pub use ;
pub use ShardedRateLimiter;