Skip to content

Commit efdf705

Browse files
authored
Replace seconds argument by Duration object (#4)
Let set the duration with a `Duration` object instead of just seconds.
1 parent 3a3c5cb commit efdf705

File tree

2 files changed

+123
-61
lines changed

2 files changed

+123
-61
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ of the values on it at any time.
99
## Examples
1010

1111
```rust
12+
use std::time::Duration;
13+
use std::thread;
1214
use sum_queue::SumQueue;
13-
use std::{time, thread};
1415

1516
// creates a queue where elements expire after 2 seconds
16-
let mut queue: SumQueue<i32> = SumQueue::new(2);
17+
let mut queue: SumQueue<i32> = SumQueue::new(Duration::from_secs(2));
1718
queue.push(1);
1819
queue.push(10);
1920
queue.push(3);
@@ -42,20 +43,21 @@ println!("Stats - length of queue: {}", stats.len); // 3
4243

4344
assert_eq!(queue.pop(), Some(1));
4445
assert_eq!(queue.iter().collect::<Vec<_>>(), vec![&5, &2]);
46+
println!("Elements after pop: {:?}", queue.iter().collect::<Vec<_>>()); // [5, 2]
4547

4648
// After a second the elements are still the same
47-
thread::sleep(time::Duration::from_secs(1));
48-
println!("Same elements: {:?}", queue.iter().collect::<Vec<_>>()); // [5, 2]
49+
thread::sleep(Duration::from_secs(1));
50+
println!("Same after 1 sec: {:?}", queue.iter().collect::<Vec<_>>()); // [5, 2]
4951

5052
queue.push(50); // Add an element 1 second younger than the rest of elements
5153
println!("Same elements + 50: {:?}", queue.iter().collect::<Vec<_>>()); // [5, 2, 50]
5254

53-
// Now let sleep 2 secs so the first elements expire
54-
thread::sleep(time::Duration::from_secs(2));
55+
// Now let sleep 1 sec so the first elements expire
56+
thread::sleep(Duration::from_secs(1));
5557
println!("Just 50: {:?}", queue.iter().collect::<Vec<_>>()); // [50]
5658

57-
// 2 seconds later the last element also expires
58-
thread::sleep(time::Duration::from_secs(2));
59+
// 1 second more later the last element also expires
60+
thread::sleep(Duration::from_secs(1));
5961
println!("No elements: {:?}", queue.iter().collect::<Vec<_>>()); // []
6062
```
6163

0 commit comments

Comments
 (0)