rl_core/
tracker_time.rs

1impl<T: crate::TimeNow> crate::Tracker<T> {
2	/// Create a tracker that starts empty at the current time.
3	///
4	/// Equivalent to [`Tracker::empty_at(T::now())`][Tracker::empty_at()].
5	pub fn empty() -> Self {
6		Self::empty_at(T::now())
7	}
8
9	/// Create a tracker that has the specified capacity.
10	///
11	/// Equivalent to [`Tracker::with_capacity_at(config, capacity, T::now())`](Tracker::with_capacity_at()).
12	pub fn with_capacity(
13		config: &crate::Config<T>,
14		capacity: u32,
15	) -> Self {
16		Self::with_capacity_at(config, capacity, T::now())
17	}
18
19	/// Create a tracker that has the specified capacity without overfilling.
20	///
21	/// Equivalent to [`Tracker::with_limited_capacity_at(config, capacity, T::now())`](Tracker::with_limited_capacity_at()).
22	pub fn with_limited_capacity(
23		config: &crate::Config<T>,
24		capacity: u32,
25	) -> Self {
26		Self::with_limited_capacity_at(config, capacity, T::now())
27	}
28
29	/// Returns the tokens currently available.
30	///
31	/// Equivalent to [`Tracker::capacity_at(T::now())`](Tracker::capacity_at).
32	pub fn capacity(&self,
33		config: &crate::Config<T>,
34	) -> u32 {
35		self.capacity_at(config, T::now())
36	}
37
38	/// Attempt to acquire `count` tokens from the rate limiter at the current time
39	///
40	/// Equivalent to [`Tracker::acquire_at(config, count, T::now())`](Tracker::acquire_at()).
41	pub fn acquire(&mut self,
42		config: &crate::Config<T>,
43		count: u32,
44	) -> Result<(), crate::Denied<T>> {
45		self.acquire_at(config, count, T::now())
46	}
47
48	/// Acquire tokens from the tracker.
49	///
50	/// Equivalent to [`Tracker::acquire_range_at(config, request, T::now())`](Tracker::acquire_range_at()).
51	pub fn acquire_range(&mut self,
52		config: &crate::Config<T>,
53		request: std::ops::RangeInclusive<u32>,
54	) -> Result<u32, crate::Denied<T>> {
55		self.acquire_range_at(config, request, T::now())
56	}
57
58	/// Acquire up to `count` tokens from the rate limiter at the current time.
59	///
60	/// Equivalent to [`Tracker::acquire_up_to_at(config, count, T::now())`](Tracker::acquire_up_to_at()).
61	#[deprecated(note="Use acquire_up_to_2.")]
62	pub fn acquire_up_to(&mut self,
63		config: &crate::Config<T>,
64		count: u32,
65	) -> Result<u32, crate::TooEarly<T>> {
66		#[allow(deprecated)]
67		self.acquire_up_to_at(config, count, T::now())
68	}
69
70	/// Acquire up to `count` tokens from the rate limiter at the current time.
71	///
72	/// Equivalent to [`Tracker::acquire_up_to_at_2(config, count, T::now())`](Tracker::acquire_up_to_at_2()).
73	pub fn acquire_up_to_2(&mut self,
74		config: &crate::Config<T>,
75		count: u32,
76	) -> Result<u32, crate::Denied<T>> {
77		self.acquire_up_to_at_2(config, count, T::now())
78	}
79
80	/// Force acquire tokens at the current time.
81	///
82	/// Equivalent to [`Tracker::force_acquire_at(config, count, T::now())`](Tracker::force_acquire_at()).
83	pub fn force_acquire(
84		&mut self,
85		config: &crate::Config<T>,
86		count: u32,
87	) {
88		self.force_acquire_at(config, count, T::now())
89	}
90
91	/// Add capacity.
92	///
93	/// Equivalent to [`Tracker::add_capacity_at(config, count, tokens, T::now())`](Tracker::add_capacity_at()).
94	pub fn add_capacity(
95		&mut self,
96		config: &crate::Config<T>,
97		tokens: u32,
98	) {
99		self.add_capacity_at(config, tokens, T::now())
100	}
101
102	/// Add capacity without overfilling.
103	///
104	/// Equivalent to [`Tracker::add_limited_capacity_at(config, count, tokens, T::now())`](Tracker::add_limited_capacity_at()).
105	pub fn add_limited_capacity(
106		&mut self,
107		config: &crate::Config<T>,
108		tokens: u32,
109	) {
110		self.add_limited_capacity_at(config, tokens, T::now())
111	}
112
113	/// Attempts to minimize the state at the current time.
114	///
115	/// Equivalent to [`Tracker::simplify_at(config, T::now())`](Tracker::simplify_at()).
116	pub fn simplify(&mut self,
117		config: &crate::Config<T>,
118	) -> bool {
119		self.simplify_at(config, T::now())
120	}
121}
122
123impl<T: crate::Time> Default for crate::Tracker<T> {
124	/// Create a full tracker.
125	///
126	/// See [`Tracker::full()`] for more details.
127	fn default() -> Self {
128		Self::full()
129	}
130}