Skip to content

Commit 3a3c5cb

Browse files
jyellozmrsarm
authored andcommitted
Converted to Instant/Duration.
This should improve precision of summary information when working with small time windows.
1 parent f704f6f commit 3a3c5cb

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/lib.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ use std::cmp::Ordering;
7979
use std::collections::BinaryHeap;
8080
use std::collections::binary_heap;
8181
use std::ops::Add;
82-
use std::time::{SystemTime, UNIX_EPOCH};
82+
use std::time::{Instant, Duration};
8383

8484
/// Internal element used by `SumQueue` to hold the values.
8585
struct QueueElement<T> {
86-
time: u64, // "Unix" Time, or seconds since EPOCH when the value was added
86+
time: Instant,
8787
value: T
8888
}
8989

@@ -160,9 +160,8 @@ impl<T> PartialOrd for QueueElement<T> {
160160
}
161161
}
162162

163-
fn now() -> u64 {
164-
SystemTime::now().duration_since(UNIX_EPOCH)
165-
.expect("<-- Time went backwards").as_secs()
163+
fn now() -> Instant {
164+
Instant::now()
166165
}
167166

168167
/// Main struct that holds the queue of elements.
@@ -183,7 +182,7 @@ pub struct SumQueue<T> {
183182
heap: BinaryHeap<QueueElement<T>>,
184183
/// max time in seconds the elements will
185184
/// live in the queue.
186-
max_age: u64
185+
max_age: Duration,
187186
}
188187

189188
impl<T> SumQueue<T> {
@@ -192,7 +191,7 @@ impl<T> SumQueue<T> {
192191
pub fn new(max_age_secs: u64) -> SumQueue<T> {
193192
SumQueue {
194193
heap: BinaryHeap::<QueueElement<T>>::new(),
195-
max_age: max_age_secs
194+
max_age: Duration::from_secs(max_age_secs),
196195
}
197196
}
198197

@@ -204,7 +203,7 @@ impl<T> SumQueue<T> {
204203
pub fn with_capacity(max_age_secs: u64, capacity: usize) -> SumQueue<T> {
205204
SumQueue {
206205
heap: BinaryHeap::<QueueElement<T>>::with_capacity(capacity),
207-
max_age: max_age_secs
206+
max_age: Duration::from_secs(max_age_secs),
208207
}
209208
}
210209

@@ -233,7 +232,7 @@ impl<T> SumQueue<T> {
233232
self.heap.len()
234233
}
235234

236-
fn clear_oldest(&mut self, now: u64) {
235+
fn clear_oldest(&mut self, now: Instant) {
237236
while let Some(el) = self.heap.peek() {
238237
let peek_age = now - el.time;
239238
if peek_age > self.max_age {
@@ -304,7 +303,7 @@ impl<T> SumQueue<T> {
304303
/// assert_eq!(queue.max_age(), 60);
305304
/// ```
306305
pub fn max_age(&self) -> u64 {
307-
self.max_age
306+
self.max_age.as_secs()
308307
}
309308

310309
/// Returns the first item in the heap, or `None` if it is empty.
@@ -556,7 +555,7 @@ mod tests {
556555
assert_eq!(queue.iter().collect::<Vec<_>>(), vec![&1, &5, &2, &50]);
557556
println!("Same elements + 50: {:?}", queue.iter().collect::<Vec<_>>());
558557

559-
sleep_secs(2);
558+
sleep_secs(1);
560559
assert_eq!(queue.iter().collect::<Vec<_>>(), vec![&50]);
561560
println!("Expired original list, only 50 in the list: {:?}",
562561
queue.iter().collect::<Vec<_>>());

0 commit comments

Comments
 (0)