From dd6e821cc91b07500851f65131422cc81b1cf165 Mon Sep 17 00:00:00 2001 From: Leonhard Markert Date: Wed, 22 Jan 2020 20:26:49 +0100 Subject: [PATCH 1/5] Rename src/wheel_time.rs -> src/lib.rs --- src/{wheel_timer.rs => lib.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{wheel_timer.rs => lib.rs} (100%) diff --git a/src/wheel_timer.rs b/src/lib.rs similarity index 100% rename from src/wheel_timer.rs rename to src/lib.rs From 6ede35fb5e50dcf36aae9cac14c16e2364265d0b Mon Sep 17 00:00:00 2001 From: Leonhard Markert Date: Wed, 22 Jan 2020 20:27:07 +0100 Subject: [PATCH 2/5] cargo fmt --- benches/bench_wheel_timer.rs | 62 +++++------ src/lib.rs | 193 +++++++++++++++++------------------ tests/test_wheel_timer.rs | 56 +++++----- 3 files changed, 153 insertions(+), 158 deletions(-) diff --git a/benches/bench_wheel_timer.rs b/benches/bench_wheel_timer.rs index 6797be7..20410f9 100644 --- a/benches/bench_wheel_timer.rs +++ b/benches/bench_wheel_timer.rs @@ -9,43 +9,43 @@ use wheel_timer::WheelTimer; #[bench] fn bench_wheel_timer_drain(b: &mut Bencher) { - let max_interval = 20; - let mut timer = WheelTimer::new(max_interval); - - b.iter(|| { - // Fill - for j in 0..100 { - timer.schedule(j%max_interval, j%max_interval); - } - - // Drain - for _ in 0..100 { - timer.tick(); - } - }); + let max_interval = 20; + let mut timer = WheelTimer::new(max_interval); + + b.iter(|| { + // Fill + for j in 0..100 { + timer.schedule(j % max_interval, j % max_interval); + } + + // Drain + for _ in 0..100 { + timer.tick(); + } + }); } #[bench] fn bench_wheel_timer_fill(b: &mut Bencher) { - let max_interval = 20; - let mut timer = WheelTimer::new(max_interval); - let mut i = 0; - - b.iter(|| { - timer.schedule(i%max_interval, i%max_interval); - i = i + 1; - }); + let max_interval = 20; + let mut timer = WheelTimer::new(max_interval); + let mut i = 0; + + b.iter(|| { + timer.schedule(i % max_interval, i % max_interval); + i = i + 1; + }); } #[bench] fn bench_wheel_timer_fast(b: &mut Bencher) { - let max_interval = 2; - let mut timer = WheelTimer::new(max_interval); - let mut i = 0; - - b.iter(|| { - timer.schedule(i%max_interval, i%max_interval); - timer.tick(); - i = i + 1; - }); + let max_interval = 2; + let mut timer = WheelTimer::new(max_interval); + let mut i = 0; + + b.iter(|| { + timer.schedule(i % max_interval, i % max_interval); + timer.tick(); + i = i + 1; + }); } diff --git a/src/lib.rs b/src/lib.rs index b200b58..7925e3c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,11 +8,11 @@ use std::ops::IndexMut; /// A simple wheel timer implementation with a fixed ring size. pub struct WheelTimer { - max_interval: usize, - current_tick: usize, - size: usize, + max_interval: usize, + current_tick: usize, + size: usize, - ring: Vec> + ring: Vec>, } /// Iterator implementation allows for using the wheel timer in a for loop. @@ -28,108 +28,103 @@ pub struct WheelTimer { /// } /// ``` impl Iterator for WheelTimer { - type Item = Vec; + type Item = Vec; - fn next(&mut self) -> Option> { - let size = self.size(); - return if size > 0 { - Some(self.tick()) - } else { - None - }; - } + fn next(&mut self) -> Option> { + let size = self.size(); + return if size > 0 { Some(self.tick()) } else { None }; + } } impl WheelTimer { - - /// Creates a new timer with the specified max interval. - /// - /// # Example - /// - /// ``` - /// use wheel_timer::WheelTimer; - /// - /// let mut timer: WheelTimer = WheelTimer::new(20); - /// ``` - pub fn new(max_interval: usize) -> WheelTimer { - // Initialize the ring with Nil values - let mut ring = Vec::with_capacity(max_interval); - for _ in 0..max_interval { - ring.push(Vec::new()) + /// Creates a new timer with the specified max interval. + /// + /// # Example + /// + /// ``` + /// use wheel_timer::WheelTimer; + /// + /// let mut timer: WheelTimer = WheelTimer::new(20); + /// ``` + pub fn new(max_interval: usize) -> WheelTimer { + // Initialize the ring with Nil values + let mut ring = Vec::with_capacity(max_interval); + for _ in 0..max_interval { + ring.push(Vec::new()) + } + + return WheelTimer { + max_interval: max_interval, + current_tick: 0, + ring: ring, + size: 0, + }; } - return WheelTimer{ - max_interval: max_interval, - current_tick: 0, - ring: ring, - size: 0, + /// Returns the number of items currently scheduled. + /// + /// # Example + /// + /// ``` + /// use wheel_timer::WheelTimer; + /// + /// let mut timer: WheelTimer = WheelTimer::new(20); + /// timer.schedule(4, 1); + /// timer.schedule(7, 1); + /// timer.schedule(1, 1); + /// assert_eq!(timer.size(), 3); + /// ``` + pub fn size(&self) -> usize { + self.size } - } - - /// Returns the number of items currently scheduled. - /// - /// # Example - /// - /// ``` - /// use wheel_timer::WheelTimer; - /// - /// let mut timer: WheelTimer = WheelTimer::new(20); - /// timer.schedule(4, 1); - /// timer.schedule(7, 1); - /// timer.schedule(1, 1); - /// assert_eq!(timer.size(), 3); - /// ``` - pub fn size(&self) -> usize { - self.size - } - - /// Schedules a new value, available after `ticks` have passed. - /// - /// # Example - /// - /// ``` - /// use wheel_timer::WheelTimer; - /// - /// let mut timer: WheelTimer = WheelTimer::new(20); - /// timer.schedule(4, 7); // schedule value 7 for 4 ticks - /// ``` - pub fn schedule(&mut self, ticks: usize, value: T) { - // Compute the scheduled position in the wheel - let index = (self.current_tick + ticks) % self.max_interval; - - // Get the current node at `index` in the wheel and append the new node - self.ring.index_mut(index).push(value); - // Increment the size counter - self.size = self.size + 1; - } - - /// Tick the timer, returning the node at the current tick. - /// - /// # Example - /// - /// ``` - /// use wheel_timer::WheelTimer; - /// - /// let mut timer: WheelTimer = WheelTimer::new(20); - /// timer.schedule(3, 4); // schedule value 4 for 3 ticks - /// timer.tick(); - /// timer.tick(); - /// timer.tick(); - /// let result = timer.tick(); // vec![4] - /// assert_eq!(result.len(), 1); - /// ``` - pub fn tick(&mut self) -> Vec { - // Get the node at the current tick in the wheel - let node = mem::replace(self.ring.index_mut(self.current_tick), Vec::new()); - - // Increment the timer - self.current_tick = (self.current_tick + 1) % self.max_interval; - - // Reduce the size by the length of the removed node - self.size = self.size - node.len(); + /// Schedules a new value, available after `ticks` have passed. + /// + /// # Example + /// + /// ``` + /// use wheel_timer::WheelTimer; + /// + /// let mut timer: WheelTimer = WheelTimer::new(20); + /// timer.schedule(4, 7); // schedule value 7 for 4 ticks + /// ``` + pub fn schedule(&mut self, ticks: usize, value: T) { + // Compute the scheduled position in the wheel + let index = (self.current_tick + ticks) % self.max_interval; + + // Get the current node at `index` in the wheel and append the new node + self.ring.index_mut(index).push(value); + + // Increment the size counter + self.size = self.size + 1; + } - // Return the node that was in that spot - return node - } + /// Tick the timer, returning the node at the current tick. + /// + /// # Example + /// + /// ``` + /// use wheel_timer::WheelTimer; + /// + /// let mut timer: WheelTimer = WheelTimer::new(20); + /// timer.schedule(3, 4); // schedule value 4 for 3 ticks + /// timer.tick(); + /// timer.tick(); + /// timer.tick(); + /// let result = timer.tick(); // vec![4] + /// assert_eq!(result.len(), 1); + /// ``` + pub fn tick(&mut self) -> Vec { + // Get the node at the current tick in the wheel + let node = mem::replace(self.ring.index_mut(self.current_tick), Vec::new()); + + // Increment the timer + self.current_tick = (self.current_tick + 1) % self.max_interval; + + // Reduce the size by the length of the removed node + self.size = self.size - node.len(); + + // Return the node that was in that spot + return node; + } } diff --git a/tests/test_wheel_timer.rs b/tests/test_wheel_timer.rs index 719d969..4cba766 100644 --- a/tests/test_wheel_timer.rs +++ b/tests/test_wheel_timer.rs @@ -4,50 +4,50 @@ use wheel_timer::WheelTimer; #[test] fn wheel_timer_schedule_test() { - let mut timer = WheelTimer::new(10); - timer.schedule(3, "tick"); + let mut timer = WheelTimer::new(10); + timer.schedule(3, "tick"); - timer.tick(); - timer.tick(); - timer.tick(); + timer.tick(); + timer.tick(); + timer.tick(); - let list = timer.tick(); - assert_eq!(list.len(), 1); + let list = timer.tick(); + assert_eq!(list.len(), 1); - let val = list[0]; - assert_eq!(val, "tick"); + let val = list[0]; + assert_eq!(val, "tick"); } #[test] fn wheel_timer_tick_test() { - let mut timer = WheelTimer::new(10); + let mut timer = WheelTimer::new(10); - for i in 0..10 { - timer.schedule(i, i) - } + for i in 0..10 { + timer.schedule(i, i) + } - for i in 0..10 { - let list = timer.tick(); - assert_eq!(list.len(), 1); + for i in 0..10 { + let list = timer.tick(); + assert_eq!(list.len(), 1); - let val = list[0]; - assert_eq!(val, i); - } + let val = list[0]; + assert_eq!(val, i); + } } #[test] fn wheel_timer_size_test() { - let mut timer = Box::new(WheelTimer::new(10)); + let mut timer = Box::new(WheelTimer::new(10)); - for i in 0..10 { - timer.schedule(i, i) - } + for i in 0..10 { + timer.schedule(i, i) + } - assert_eq!(timer.size(), 10); + assert_eq!(timer.size(), 10); - for _ in 0..10 { - timer.tick(); - } + for _ in 0..10 { + timer.tick(); + } - assert_eq!(timer.size(), 0); + assert_eq!(timer.size(), 0); } From dd0df136d2532bbf7c49914e0d05fb5b2099ed5a Mon Sep 17 00:00:00 2001 From: Leonhard Markert Date: Wed, 22 Jan 2020 20:28:33 +0100 Subject: [PATCH 3/5] clippy --- benches/bench_wheel_timer.rs | 4 ++-- src/lib.rs | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/benches/bench_wheel_timer.rs b/benches/bench_wheel_timer.rs index 20410f9..85ff717 100644 --- a/benches/bench_wheel_timer.rs +++ b/benches/bench_wheel_timer.rs @@ -33,7 +33,7 @@ fn bench_wheel_timer_fill(b: &mut Bencher) { b.iter(|| { timer.schedule(i % max_interval, i % max_interval); - i = i + 1; + i += 1; }); } @@ -46,6 +46,6 @@ fn bench_wheel_timer_fast(b: &mut Bencher) { b.iter(|| { timer.schedule(i % max_interval, i % max_interval); timer.tick(); - i = i + 1; + i += 1; }); } diff --git a/src/lib.rs b/src/lib.rs index 7925e3c..3ead130 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,7 @@ impl Iterator for WheelTimer { fn next(&mut self) -> Option> { let size = self.size(); - return if size > 0 { Some(self.tick()) } else { None }; + if size > 0 { Some(self.tick()) } else { None } } } @@ -53,12 +53,12 @@ impl WheelTimer { ring.push(Vec::new()) } - return WheelTimer { - max_interval: max_interval, + WheelTimer { + max_interval, current_tick: 0, - ring: ring, + ring, size: 0, - }; + } } /// Returns the number of items currently scheduled. @@ -96,7 +96,7 @@ impl WheelTimer { self.ring.index_mut(index).push(value); // Increment the size counter - self.size = self.size + 1; + self.size += 1; } /// Tick the timer, returning the node at the current tick. @@ -122,9 +122,9 @@ impl WheelTimer { self.current_tick = (self.current_tick + 1) % self.max_interval; // Reduce the size by the length of the removed node - self.size = self.size - node.len(); + self.size -= node.len(); // Return the node that was in that spot - return node; + node } } From eacc354fe0b1589cca5ea53de3bd88dbb1209d22 Mon Sep 17 00:00:00 2001 From: Leonhard Markert Date: Wed, 22 Jan 2020 20:32:04 +0100 Subject: [PATCH 4/5] Rust 2018 --- Cargo.toml | 1 + benches/bench_wheel_timer.rs | 1 - src/lib.rs | 6 +++++- tests/test_wheel_timer.rs | 2 -- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fe55dad..be138df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ name = "wheel_timer" version = "0.3.1" description = "A simple hashed wheel timer." authors = [ "jim@barkingmousestudio.com" ] +edition = "2018" license = "MIT" homepage = "https://crates.io/crates/wheel_timer" documentation = "http://barkingmousestudio.com/wheel-timer-rs/wheel_timer/" diff --git a/benches/bench_wheel_timer.rs b/benches/bench_wheel_timer.rs index 85ff717..a912bc1 100644 --- a/benches/bench_wheel_timer.rs +++ b/benches/bench_wheel_timer.rs @@ -1,7 +1,6 @@ #![feature(test)] extern crate test; -extern crate wheel_timer; use test::Bencher; diff --git a/src/lib.rs b/src/lib.rs index 3ead130..f45baf4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,11 @@ impl Iterator for WheelTimer { fn next(&mut self) -> Option> { let size = self.size(); - if size > 0 { Some(self.tick()) } else { None } + if size > 0 { + Some(self.tick()) + } else { + None + } } } diff --git a/tests/test_wheel_timer.rs b/tests/test_wheel_timer.rs index 4cba766..a4e7596 100644 --- a/tests/test_wheel_timer.rs +++ b/tests/test_wheel_timer.rs @@ -1,5 +1,3 @@ -extern crate wheel_timer; - use wheel_timer::WheelTimer; #[test] From 0819f0fc6c1d12490f7bba5d4db6d4524d77c271 Mon Sep 17 00:00:00 2001 From: Leonhard Markert Date: Wed, 22 Jan 2020 20:45:38 +0100 Subject: [PATCH 5/5] Simplify --- src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f45baf4..792ff5f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,8 +31,7 @@ impl Iterator for WheelTimer { type Item = Vec; fn next(&mut self) -> Option> { - let size = self.size(); - if size > 0 { + if self.size() > 0 { Some(self.tick()) } else { None