Skip to content

Commit 2fe8b0d

Browse files
committed
timer
1 parent ef28210 commit 2fe8b0d

File tree

6 files changed

+117
-65
lines changed

6 files changed

+117
-65
lines changed

examples/timer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use algorithm::TimerWheel;
22

33
fn main() {
44
let mut timer = TimerWheel::new();
5-
timer.append_timer_wheel(12, 60 * 60, "HourWheel");
6-
timer.append_timer_wheel(60, 60, "MinuteWheel");
7-
timer.append_timer_wheel(60, 1, "SecondWheel");
5+
timer.append_timer_wheel(60, "SecondWheel");
6+
timer.append_timer_wheel(60, "MinuteWheel");
7+
timer.append_timer_wheel(12, "HourWheel");
88

99
timer.add_timer(30);
1010
assert_eq!(timer.get_delay_id(), 30);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub mod buf;
1313
pub use cache::{LruCache, LruKCache, LfuCache, ArcCache, Slab, Reinit};
1414
pub use tree::RBTree;
1515
pub use map::{BitMap, RoaringBitMap};
16-
pub use timer::{TimerWheel, Timer, TimerRBTree, StampTimer};
16+
pub use timer::{TimerWheel, Timer, TimerRBTree, StampTimer, StepTimer};
1717
pub use arr::{CircularBuffer, FixedVec};
1818
pub use util::*;
1919

src/timer/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ impl_primitive_timer!(usize);
3737
mod timer_rbtree;
3838
mod timer_wheel;
3939
mod stamp_timer;
40+
mod step_timer;
4041

4142
pub use timer_wheel::TimerWheel;
4243
pub use timer_rbtree::TimerRBTree;
4344
pub use stamp_timer::StampTimer;
45+
pub use step_timer::StepTimer;

src/timer/stamp_timer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ impl<T> StampTimer<T> {
1414
Self { val, duration, is_sec }
1515
}
1616

17-
pub fn new_second(val: T, interval: u64) -> Self {
17+
pub fn new_second(val: T, duration: Duration) -> Self {
1818
Self {
1919
val,
20-
duration: Duration::from_secs(interval),
20+
duration,
2121
is_sec: true,
2222
}
2323
}
2424

25-
pub fn new_millis(val: T, interval: u64) -> Self {
25+
pub fn new_millis(val: T, duration: Duration) -> Self {
2626
Self {
2727
val,
28-
duration: Duration::from_millis(interval),
28+
duration,
2929
is_sec: false,
3030
}
3131
}

src/timer/step_timer.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use super::Timer;
2+
3+
pub struct StepTimer<T: Timer, V> {
4+
pub step: T,
5+
pub val: V,
6+
}
7+
8+
impl<T: Timer, V> StepTimer<T, V> {
9+
pub fn new(val: V, step: T) -> Self {
10+
Self { val, step }
11+
}
12+
}
13+
14+
impl<T: Timer, V> Timer for StepTimer<T, V> {
15+
fn when(&self) -> u64 {
16+
self.step.when()
17+
}
18+
}

0 commit comments

Comments
 (0)