From 6ba638fbf5b6a6e688ea84f8ad34769006df0794 Mon Sep 17 00:00:00 2001 From: tickbh Date: Fri, 11 Oct 2024 16:34:02 +0800 Subject: [PATCH] fix timer --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 2 +- src/timer/mod.rs | 39 ++++- src/timer/timer_rbtree.rs | 312 ++++++++++++++++++++++++++++++++++++++ src/timer/timer_wheel.rs | 188 +++++++++++++---------- 6 files changed, 461 insertions(+), 84 deletions(-) create mode 100644 src/timer/timer_rbtree.rs diff --git a/Cargo.lock b/Cargo.lock index 0ad6ce4..f1c7932 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16,7 +16,7 @@ dependencies = [ [[package]] name = "algorithm" -version = "0.1.12" +version = "0.1.13" dependencies = [ "algorithm-macro 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "hashbrown", diff --git a/Cargo.toml b/Cargo.toml index e3ed71d..7b0ec38 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = ["algorithm-macro"] [package] name = "algorithm" -version = "0.1.12" +version = "0.1.13" edition = "2021" authors = ["tickbh "] description = "about algorithm data structure, now has ttl with lru/lru-k/lfu/arc and slab/rbtree/roaring_bitmap/timer_wheelss, 关于算法常用的数据结构" diff --git a/src/lib.rs b/src/lib.rs index d9d02fd..1e4bf1d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,7 @@ pub mod buf; pub use cache::{LruCache, LruKCache, LfuCache, ArcCache, Slab, Reinit}; pub use tree::RBTree; pub use map::{BitMap, RoaringBitMap}; -pub use timer::{TimerWheel, Timer}; +pub use timer::{TimerWheel, Timer, TimerRBTree}; pub use arr::{CircularBuffer, FixedVec}; pub use util::*; diff --git a/src/timer/mod.rs b/src/timer/mod.rs index 816d07b..1018e62 100644 --- a/src/timer/mod.rs +++ b/src/timer/mod.rs @@ -1,4 +1,41 @@ + +pub trait Timer { + /// 当时与现在的间隔,以确定插入确定的槽 + fn when(&self) -> u64; + /// 可能需要修改对象,此处用可变值 + fn when_mut(&mut self) -> u64 { + self.when() + } +} + +macro_rules! impl_primitive_timer { + ($ty:ident) => { + impl Timer for $ty { + #[inline(always)] + fn when(&self) -> u64 { + *self as u64 + } + } + }; +} + +impl_primitive_timer!(u8); +impl_primitive_timer!(u16); +impl_primitive_timer!(u32); +impl_primitive_timer!(u64); +impl_primitive_timer!(u128); +impl_primitive_timer!(i8); +impl_primitive_timer!(i16); +impl_primitive_timer!(i32); +impl_primitive_timer!(i64); +impl_primitive_timer!(i128); +impl_primitive_timer!(f32); +impl_primitive_timer!(f64); +impl_primitive_timer!(usize); + +mod timer_rbtree; mod timer_wheel; -pub use timer_wheel::{TimerWheel, Timer}; \ No newline at end of file +pub use timer_wheel::TimerWheel; +pub use timer_rbtree::TimerRBTree; diff --git a/src/timer/timer_rbtree.rs b/src/timer/timer_rbtree.rs new file mode 100644 index 0000000..b5b9a49 --- /dev/null +++ b/src/timer/timer_rbtree.rs @@ -0,0 +1,312 @@ +use crate::HashMap; + +use crate::RBTree; +use std::cmp::Ordering; +use std::vec; + +use super::Timer; + +#[derive(PartialEq, Eq)] +struct TreeKey(u64, u64); + +impl Ord for TreeKey { + fn cmp(&self, other: &Self) -> Ordering { + if self.0 != other.0 { + return self.0.cmp(&other.0); + } + other.1.cmp(&self.1) + } +} + +impl PartialOrd for TreeKey { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +/// 计时器轮,模拟时钟格式组成的高效计时器 +/// +/// 时间轮是一个环形的数据结构,可以想象成一个时钟的面,被分成多个格子 +/// +/// 每个格子代表一段时间,这段时间越短,定时器的精度就越高。 +/// +/// 每个格子用一个Vec存储放在该格子上的延时任务。 +/// +/// Mark: 在Rust中双向链表中暂未提供元素关键列表的接口,这里改用Vec,删除时会额外移动Vec值 +/// +/// # Examples +/// +/// ``` +/// use algorithm::TimerRBTree; +/// fn main() { +/// let mut timer = TimerRBTree::new(); +/// timer.add_timer(30); +/// assert_eq!(timer.tick_first(), Some(30)); +/// timer.add_timer(149); +/// assert_eq!(timer.tick_first(), Some(30)); +/// let t = timer.add_timer(600); +/// assert_eq!(timer.tick_first(), Some(30)); +/// timer.add_timer(1); +/// assert_eq!(timer.tick_first(), Some(1)); +/// timer.del_timer(t); +/// timer.add_timer(150); +/// assert_eq!(timer.tick_first(), Some(1)); +/// let val = timer.update_deltatime(30).unwrap(); +/// assert_eq!(val, vec![1, 30]); +/// timer.add_timer(2); +/// let val = timer.update_deltatime(119).unwrap(); +/// assert_eq!(val, vec![2, 149]); +/// let val = timer.update_deltatime(1).unwrap(); +/// assert_eq!(val, vec![150]); +/// assert!(timer.is_empty()); +/// } +/// ``` +pub struct TimerRBTree { + tree: RBTree, + + map: HashMap, + + /// 当时记录的时序 + cur_step: u64, + + /// id记录 + next_timer_id: u64, +} + +impl TimerRBTree { + pub fn new() -> Self { + Self { + tree: RBTree::new(), + map: HashMap::new(), + cur_step: 0, + next_timer_id: 0, + } + } + + /// 获取定时器的长度 + /// # Examples + /// + /// ``` + /// use algorithm::TimerRBTree; + /// fn main() { + /// let mut timer = TimerRBTree::::new(); + /// assert!(timer.is_empty()); + /// timer.add_timer(1); + /// assert_eq!(timer.len(), 1); + /// let t = timer.add_timer(2); + /// assert_eq!(timer.len(), 2); + /// timer.del_timer(t); + /// assert_eq!(timer.len(), 1); + /// } + /// ``` + pub fn len(&self) -> usize { + self.tree.len() + } + + pub fn is_empty(&self) -> bool { + self.tree.is_empty() + } + + /// 清除所有的槽位 + /// # Examples + /// + /// ``` + /// use algorithm::TimerRBTree; + /// fn main() { + /// let mut timer = TimerRBTree::::new(); + /// assert!(timer.is_empty()); + /// timer.add_timer(1); + /// timer.add_timer(2); + /// assert_eq!(timer.len(), 2); + /// timer.clear(); + /// assert_eq!(timer.len(), 0); + /// } + /// ``` + pub fn clear(&mut self) { + self.tree.clear(); + self.map.clear(); + self.cur_step = 0; + self.next_timer_id = 0; + } + + /// 添加定时器元素 + /// # Examples + /// + /// ``` + /// use algorithm::TimerRBTree; + /// fn main() { + /// let mut timer = TimerRBTree::new(); + /// timer.add_timer(30); + /// assert_eq!(timer.len(), 1); + /// } + pub fn add_timer(&mut self, val: T) -> u64 { + let timer_id = self.next_timer_id; + self.next_timer_id = self.next_timer_id.wrapping_add(1); + let when = val.when(); + self.tree.insert(TreeKey(when, timer_id), val); + self.map.insert(timer_id, when); + timer_id + } + + /// 删除指定的定时器,时间复杂度为O(logn), + /// + /// # Examples + /// + /// ``` + /// use algorithm::TimerRBTree; + /// fn main() { + /// let mut timer = TimerRBTree::new(); + /// let t = timer.add_timer(30); + /// timer.del_timer(t); + /// assert_eq!(timer.len(), 0); + /// } + pub fn del_timer(&mut self, timer_id: u64) -> Option { + if let Some(when) = self.map.remove(&timer_id) { + let tree = TreeKey(when, timer_id); + self.tree.remove(&tree).map(|e| e) + } else { + None + } + } + + /// 获取指定的定时器,时间复杂度为O(log(n)) + /// + /// # Examples + /// + /// ``` + /// use algorithm::TimerRBTree; + /// fn main() { + /// let mut timer = TimerRBTree::new(); + /// let t = timer.add_timer(30); + /// assert_eq!(timer.get_timer(&t), Some(&30)); + /// } + pub fn get_timer(&self, timer_id: &u64) -> Option<&T> { + if let Some(when) = self.map.get(timer_id) { + let tree = TreeKey(*when, *timer_id); + self.tree.get(&tree).map(|e| e) + } else { + None + } + } + + /// 获取指定的定时器,时间复杂度为O(log(n)) + /// + /// # Examples + /// + /// ``` + /// use algorithm::TimerRBTree; + /// fn main() { + /// let mut timer = TimerRBTree::new(); + /// let t = timer.add_timer(30); + /// *timer.get_mut_timer(&t).unwrap() = 33; + /// let val = timer.update_deltatime(30).unwrap(); + /// assert_eq!(val, vec![33]); + /// } + pub fn get_mut_timer(&mut self, timer_id: &u64) -> Option<&mut T> { + if let Some(when) = self.map.get(timer_id) { + let tree = TreeKey(*when, *timer_id); + self.tree.get_mut(&tree).map(|e| e) + } else { + None + } + } + + /// 取出时间轴最小的一个值 + pub fn tick_first(&self) -> Option { + self.tree + .get_first() + .map(|(key, _)| Some(key.0)) + .unwrap_or(None) + } + + /// 判断到指定时间是否有小于该指定值的实例 + pub fn tick_time(&mut self, tm: u64) -> Option { + if tm < self.tick_first().unwrap_or(tm + 1) { + return None; + } + self.tree.pop_first().map(|(_, e)| e) + } + + /// 计时器轮的递进时间 + /// + /// # Examples + /// + /// ``` + /// use algorithm::TimerRBTree; + /// fn main() { + /// let mut timer = TimerRBTree::new(); + /// timer.add_timer(30); + /// let val = timer.update_deltatime(30).unwrap(); + /// assert_eq!(val, vec![30]); + /// } + pub fn update_now(&mut self, now: u64) -> Option> { + self.cur_step = now; + let mut result = vec![]; + loop { + if let Some(val) = self.tick_first() { + if self.cur_step < val { + break; + } + result.push(self.tree.pop_first().map(|(_, e)| e).unwrap()); + } else { + break; + } + } + Some(result) + } + /// 计时器轮的递进时间 + /// + /// # Examples + /// + /// ``` + /// use algorithm::TimerRBTree; + /// fn main() { + /// let mut timer = TimerRBTree::new(); + /// timer.add_timer(30); + /// let val = timer.update_deltatime(30).unwrap(); + /// assert_eq!(val, vec![30]); + /// } + pub fn update_deltatime(&mut self, delta: u64) -> Option> { + self.update_now(self.cur_step.wrapping_add(delta)) + } + + /// 计时器轮的递进时间 + /// + /// # Examples + /// + /// ``` + /// use algorithm::TimerRBTree; + /// fn main() { + /// let mut timer = TimerRBTree::new(); + /// timer.add_timer(30); + /// let mut idx = 0; + /// timer.update_deltatime_with_callback(30, &mut |_, v| { + /// idx = v; + /// None + /// }); + /// assert_eq!(idx, 30); + /// } + pub fn update_deltatime_with_callback(&mut self, delta: u64, f: &mut F) + where + F: FnMut(&mut Self, T) -> Option, + { + self.update_now_with_callback(self.cur_step.wrapping_add(delta), f) + } + + pub fn update_now_with_callback(&mut self, now: u64, f: &mut F) + where + F: FnMut(&mut Self, T) -> Option, + { + if let Some(result) = self.update_now(now) { + let mut collect_result = vec![]; + for r in result.into_iter() { + if let Some(v) = (*f)(self, r) { + collect_result.push(v); + } + } + for v in collect_result.drain(..) { + self.add_timer(v); + } + } + } +} diff --git a/src/timer/timer_wheel.rs b/src/timer/timer_wheel.rs index fe9f38f..890e642 100644 --- a/src/timer/timer_wheel.rs +++ b/src/timer/timer_wheel.rs @@ -1,53 +1,23 @@ -use std::{fmt::{self, Display}, ptr}; +use std::{ + fmt::{self, Display}, + ptr, +}; + +use super::Timer; struct Entry { val: T, - when: usize, - id: usize, -} - -pub trait Timer { - /// 当时与现在的间隔,以确定插入确定的槽 - fn when(&self) -> usize; - /// 可能需要修改对象,此处用可变值 - fn when_mut(&mut self) -> usize { - self.when() - } + when: u64, + id: u64, } - -macro_rules! impl_primitive_timer { - ($ty:ident) => { - impl Timer for $ty { - #[inline(always)] - fn when(&self) -> usize { - *self as usize - } - } - }; -} - -impl_primitive_timer!(u8); -impl_primitive_timer!(u16); -impl_primitive_timer!(u32); -impl_primitive_timer!(u64); -impl_primitive_timer!(u128); -impl_primitive_timer!(i8); -impl_primitive_timer!(i16); -impl_primitive_timer!(i32); -impl_primitive_timer!(i64); -impl_primitive_timer!(i128); -impl_primitive_timer!(f32); -impl_primitive_timer!(f64); -impl_primitive_timer!(usize); - /// 单轮结构 pub struct OneTimerWheel { /// 当时指针指向的位置,如秒针指向3点钟方向 - index: usize, + index: u64, /// 当前结构的容量,如表示秒的为60的容量 - capation: usize, + capation: u64, /// 当前结构步长,如分钟就表示60s的 - step: usize, + step: u64, /// 当前槽位容纳的元素 slots: Vec>>, /// 当前轮结构的父轮,如当前是分的,那父轮为时轮 @@ -59,7 +29,7 @@ pub struct OneTimerWheel { } impl OneTimerWheel { - pub fn new(capation: usize, step: usize, name: &'static str) -> Self { + pub fn new(capation: u64, step: u64, name: &'static str) -> Self { let mut slots = vec![]; for _ in 0..capation { slots.push(Vec::new()); @@ -76,7 +46,7 @@ impl OneTimerWheel { } pub fn clear(&mut self) { - for idx in 0..self.capation { + for idx in 0..self.capation as usize { self.slots[idx].clear(); } } @@ -99,8 +69,8 @@ impl OneTimerWheel { self.add_timer_with_offset(entry, offset); } - fn del_timer(&mut self, timer_id: usize) -> Option { - for i in 0..self.capation { + fn del_timer(&mut self, timer_id: u64) -> Option { + for i in 0..self.capation as usize { let mut found_idx = None; for (idx, val) in self.slots[i].iter().enumerate() { if val.id == timer_id { @@ -115,8 +85,8 @@ impl OneTimerWheel { None } - fn get_timer(&self, timer_id: &usize) -> Option<&T> { - for i in 0..self.capation { + fn get_timer(&self, timer_id: &u64) -> Option<&T> { + for i in 0..self.capation as usize { for val in self.slots[i].iter() { if &val.id == timer_id { return Some(&val.val); @@ -126,8 +96,8 @@ impl OneTimerWheel { None } - fn get_mut_timer(&mut self, timer_id: &usize) -> Option<&mut T> { - for i in 0..self.capation { + fn get_mut_timer(&mut self, timer_id: &u64) -> Option<&mut T> { + for i in 0..self.capation as usize { let mut found_idx = None; let v = &mut self.slots[i]; for (idx, val) in v.iter().enumerate() { @@ -148,10 +118,10 @@ impl OneTimerWheel { self.add_timer_with_offset(entry, offset); } - fn add_timer_with_offset(&mut self, entry: Entry, offset: usize) { + fn add_timer_with_offset(&mut self, entry: Entry, offset: u64) { if offset > self.capation * self.step { let index = (self.index + self.capation - 1) % self.capation; - self.slots[index].push(entry); + self.slots[index as usize].push(entry); } else if offset < self.step && !self.child.is_null() { unsafe { (*self.child).add_timer_with_offset(entry, offset); @@ -160,11 +130,11 @@ impl OneTimerWheel { // 当前偏差值还在自己的容纳范围之前,做容错,排在最后处理位 let index = (offset - 1) / self.step; let index = (index + self.index) % self.capation; - self.slots[index].push(entry); + self.slots[index as usize].push(entry); } } - pub fn update_index(&mut self, offset: usize, remainder: usize, result: &mut Vec) -> (usize, usize) { + pub fn update_index(&mut self, offset: u64, remainder: u64, result: &mut Vec) -> (u64, u64) { let next = self.index + offset; let mut all = 0; for idx in self.index..next { @@ -173,7 +143,7 @@ impl OneTimerWheel { } all += 1; let idx = idx % self.capation; - let list = &mut self.slots[idx]; + let list = &mut self.slots[idx as usize]; for val in list.drain(..) { result.push(val.val); } @@ -181,7 +151,7 @@ impl OneTimerWheel { self.index = next % self.capation; if !self.child.is_null() { unsafe { - let list = &mut self.slots[self.index]; + let list = &mut self.slots[self.index as usize]; for mut val in list.drain(..) { val.when = (val.when % self.step).saturating_sub(remainder); if val.when <= 0 { @@ -242,26 +212,25 @@ pub struct TimerWheel { /// 时轮的最小轮,以时钟为例就是秒针 lessest: *mut OneTimerWheel, /// 时轮的最小间隔,以时间为例就是秒 - min_step: usize, + min_step: u64, /// 维护定时器id - next_timer_id: usize, + next_timer_id: u64, /// 离的最近的id - delay_id: usize, + delay_id: u64, /// 总共的递进步长,缓存优化触发 - all_deltatime: usize, + all_deltatime: u64, /// 当时时轮里的元素个数 len: usize, } impl TimerWheel { - /// 创建一个计时器轮 /// # Examples /// /// ``` /// use algorithm::TimerWheel; /// fn main() { - /// let mut timer = TimerWheel::::new(); + /// let mut timer = TimerWheel::::new(); /// assert!(timer.is_empty()); /// } /// ``` @@ -283,7 +252,7 @@ impl TimerWheel { /// ``` /// use algorithm::TimerWheel; /// fn main() { - /// let mut timer = TimerWheel::::new(); + /// let mut timer = TimerWheel::::new(); /// timer.append_timer_wheel(60, 1, "SecondWheel"); /// assert!(timer.is_empty()); /// timer.add_timer(1); @@ -304,7 +273,7 @@ impl TimerWheel { /// ``` /// use algorithm::TimerWheel; /// fn main() { - /// let mut timer = TimerWheel::::new(); + /// let mut timer = TimerWheel::::new(); /// assert!(timer.is_empty()); /// } /// ``` @@ -318,7 +287,7 @@ impl TimerWheel { /// ``` /// use algorithm::TimerWheel; /// fn main() { - /// let mut timer = TimerWheel::::new(); + /// let mut timer = TimerWheel::::new(); /// timer.append_timer_wheel(60, 1, "SecondWheel"); /// assert!(timer.is_empty()); /// timer.add_timer(1); @@ -352,7 +321,7 @@ impl TimerWheel { /// timer.append_timer_wheel(60, 1, "SecondWheel"); /// timer.add_timer(30); /// } - pub fn append_timer_wheel(&mut self, slots: usize, step: usize, name: &'static str) { + pub fn append_timer_wheel(&mut self, slots: u64, step: u64, name: &'static str) { debug_assert!(self.len == 0, "必须时轮为空才可改变时轮"); let one = Box::into_raw(Box::new(OneTimerWheel::new(slots, step, name))); self.delay_id = self.delay_id.max(slots * step); @@ -380,9 +349,27 @@ impl TimerWheel { /// let val = timer.update_deltatime(30).unwrap(); /// assert_eq!(val, vec![30]); /// } - pub fn update_deltatime(&mut self, delta: usize) -> Option> { + pub fn update_deltatime(&mut self, delta: u64) -> Option> { debug_assert!(self.min_step > 0); - self.all_deltatime += delta; + self.update_now(self.all_deltatime.wrapping_add(delta)) + } + + /// 计时器轮的递进时间 + /// + /// # Examples + /// + /// ``` + /// use algorithm::TimerWheel; + /// fn main() { + /// let mut timer = TimerWheel::new(); + /// timer.append_timer_wheel(60, 1, "SecondWheel"); + /// timer.add_timer(30); + /// let val = timer.update_deltatime(30).unwrap(); + /// assert_eq!(val, vec![30]); + /// } + pub fn update_now(&mut self, now: u64) -> Option> { + debug_assert!(self.min_step > 0); + self.all_deltatime = now; let mut offset = self.all_deltatime / self.min_step; if offset < self.delay_id { return None; @@ -419,17 +406,49 @@ impl TimerWheel { /// let mut idx = 0; /// timer.update_deltatime_with_callback(30, &mut |_, v| { /// idx = v; + /// None /// }); /// assert_eq!(idx, 30); /// } - pub fn update_deltatime_with_callback(&mut self, delta: usize, f: &mut F) + pub fn update_deltatime_with_callback(&mut self, delta: u64, f: &mut F) where - F: FnMut(&mut Self, T), + F: FnMut(&mut Self, T) -> Option, { debug_assert!(self.min_step > 0); - if let Some(result) = self.update_deltatime(delta) { + self.update_now_with_callback(self.all_deltatime.wrapping_add(delta), f); + } + + /// 计时器轮的递进时间 + /// + /// # Examples + /// + /// ``` + /// use algorithm::TimerWheel; + /// fn main() { + /// let mut timer = TimerWheel::new(); + /// timer.append_timer_wheel(60, 1, "SecondWheel"); + /// timer.add_timer(30); + /// let mut idx = 0; + /// timer.update_deltatime_with_callback(30, &mut |_, v| { + /// idx = v; + /// None + /// }); + /// assert_eq!(idx, 30); + /// } + pub fn update_now_with_callback(&mut self, now: u64, f: &mut F) + where + F: FnMut(&mut Self, T) -> Option, + { + debug_assert!(self.min_step > 0); + if let Some(result) = self.update_now(now) { + let mut collect_result = vec![]; for r in result.into_iter() { - (*f)(self, r); + if let Some(v) = (*f)(self, r) { + collect_result.push(v); + } + } + for v in collect_result.drain(..) { + self.add_timer(v); } } } @@ -456,7 +475,7 @@ impl TimerWheel { let (step, index, cap) = ((*wheel).step, (*wheel).index, (*wheel).capation); for i in 0..cap { let index = (index + i) % cap; - if !(*wheel).slots[index].is_empty() { + if !(*wheel).slots[index as usize].is_empty() { next_delay_id = (i + 1) * step; break 'outer; } @@ -482,7 +501,7 @@ impl TimerWheel { /// timer.del_timer(t); /// assert_eq!(timer.len(), 0); /// } - pub fn del_timer(&mut self, timer_id: usize) -> Option { + pub fn del_timer(&mut self, timer_id: u64) -> Option { let mut wheel = self.lessest; while !wheel.is_null() { unsafe { @@ -509,7 +528,7 @@ impl TimerWheel { /// let t = timer.add_timer(30); /// assert_eq!(timer.get_timer(&t), Some(&30)); /// } - pub fn get_timer(&self, timer_id: &usize) -> Option<&T> { + pub fn get_timer(&self, timer_id: &u64) -> Option<&T> { let mut wheel = self.lessest; while !wheel.is_null() { unsafe { @@ -537,7 +556,7 @@ impl TimerWheel { /// let val = timer.update_deltatime(30).unwrap(); /// assert_eq!(val, vec![33]); /// } - pub fn get_mut_timer(&mut self, timer_id: &usize) -> Option<&mut T> { + pub fn get_mut_timer(&mut self, timer_id: &u64) -> Option<&mut T> { let mut wheel = self.lessest; while !wheel.is_null() { unsafe { @@ -560,11 +579,15 @@ impl TimerWheel { /// timer.append_timer_wheel(60, 1, "SecondWheel"); /// timer.add_timer(30); /// } - pub fn add_timer(&mut self, mut val: T) -> usize { + pub fn add_timer(&mut self, mut val: T) -> u64 { debug_assert!(!self.greatest.is_null(), "必须设置时轮才能添加元素"); let timer_id = self.next_timer_id; self.next_timer_id += 1; - let entry = Entry { when: val.when_mut().max(1), val, id: timer_id }; + let entry = Entry { + when: val.when_mut().max(1), + val, + id: timer_id, + }; self.delay_id = self.delay_id.min(entry.when); unsafe { (*self.greatest).add_timer(entry); @@ -584,7 +607,7 @@ impl TimerWheel { /// timer.add_timer(30); /// assert_eq!(timer.get_delay_id(), 30); /// } - pub fn get_delay_id(&self) -> usize { + pub fn get_delay_id(&self) -> u64 { self.delay_id } } @@ -595,7 +618,12 @@ impl Display for TimerWheel { let mut wheel = self.greatest; while !wheel.is_null() { unsafe { - f.write_fmt(format_args!("{}, slots: {}, step: {}", (*wheel).name, (*wheel).slots.len(), (*wheel).step))?; + f.write_fmt(format_args!( + "{}, slots: {}, step: {}", + (*wheel).name, + (*wheel).slots.len(), + (*wheel).step + ))?; wheel = (*wheel).child; } } @@ -613,4 +641,4 @@ impl Drop for TimerWheel { } } } -} \ No newline at end of file +}