pub struct ThrottleTimer { /* private fields */ }
Implementations§
Source§impl ThrottleTimer
§Example
use std::time::Duration;
use throttle_timer::ThrottleTimer;
let mut break_timer: ThrottleTimer = ThrottleTimer::new(Duration::from_secs(1_u64), &"Break");
let do_break_flag = break_timer.run(&mut || {});
// Timers always run when no previous runs
assert!(do_break_flag == true);
if do_break_flag {
println!("timer do run flag is set to true")
}
// Run flag false as no time has passed
assert!(break_timer.run(&mut || {}) == false);
impl ThrottleTimer
§Example
use std::time::Duration;
use throttle_timer::ThrottleTimer;
let mut break_timer: ThrottleTimer = ThrottleTimer::new(Duration::from_secs(1_u64), &"Break");
let do_break_flag = break_timer.run(&mut || {});
// Timers always run when no previous runs
assert!(do_break_flag == true);
if do_break_flag {
println!("timer do run flag is set to true")
}
// Run flag false as no time has passed
assert!(break_timer.run(&mut || {}) == false);
Sourcepub fn new(max_frequency: Duration, event_name: &'static str) -> Self
pub fn new(max_frequency: Duration, event_name: &'static str) -> Self
Examples found in repository?
examples/simple.rs (line 5)
4fn main() {
5 let mut break_timer = ThrottleTimer::new(Duration::from_secs(10_u64), &"Break");
6 let mut val = 0_u8;
7 // timers always run when no previous runs
8 assert!(break_timer.run(&mut || val += 1));
9
10 for _ in 0..100 {
11 // timer will not run as 10 secs has not passed
12 // do run will return false
13 assert!(!break_timer.run(&mut || val += 1));
14 }
15 break_timer.print_stats();
16 assert_eq!(break_timer.total_calls(), &1);
17 assert_eq!(val, 1_u8);
18}
pub const fn event_name(&self) -> &str
Sourcepub const fn total_calls(&self) -> &usize
pub const fn total_calls(&self) -> &usize
Examples found in repository?
examples/simple.rs (line 16)
4fn main() {
5 let mut break_timer = ThrottleTimer::new(Duration::from_secs(10_u64), &"Break");
6 let mut val = 0_u8;
7 // timers always run when no previous runs
8 assert!(break_timer.run(&mut || val += 1));
9
10 for _ in 0..100 {
11 // timer will not run as 10 secs has not passed
12 // do run will return false
13 assert!(!break_timer.run(&mut || val += 1));
14 }
15 break_timer.print_stats();
16 assert_eq!(break_timer.total_calls(), &1);
17 assert_eq!(val, 1_u8);
18}
pub const fn max_frequency(&self) -> &Duration
pub const fn created_date(&self) -> SystemTime
pub fn wait_time(&self) -> Duration
Sourcepub fn print_stats(&self)
pub fn print_stats(&self)
Prints total calls and calls/sec
Examples found in repository?
examples/simple.rs (line 15)
4fn main() {
5 let mut break_timer = ThrottleTimer::new(Duration::from_secs(10_u64), &"Break");
6 let mut val = 0_u8;
7 // timers always run when no previous runs
8 assert!(break_timer.run(&mut || val += 1));
9
10 for _ in 0..100 {
11 // timer will not run as 10 secs has not passed
12 // do run will return false
13 assert!(!break_timer.run(&mut || val += 1));
14 }
15 break_timer.print_stats();
16 assert_eq!(break_timer.total_calls(), &1);
17 assert_eq!(val, 1_u8);
18}
Sourcepub fn can_run(&mut self) -> bool
pub fn can_run(&mut self) -> bool
Calling run()
will check the last call time. If max frequency time has not passed the fn will return false.
If max_frequency duration has passed since the last call then the fn will return true
pub fn run_throttle_cb( &mut self, success: &mut dyn FnMut(), throttled: &mut dyn FnMut(), ) -> bool
Sourcepub fn run(&mut self, success: &mut dyn FnMut()) -> bool
pub fn run(&mut self, success: &mut dyn FnMut()) -> bool
Calling run()
will check the last call time. If max frequency time has not passed the fn will return false.
If max_frequency duration has passed since the last call then the fn will return true
Examples found in repository?
examples/simple.rs (line 8)
4fn main() {
5 let mut break_timer = ThrottleTimer::new(Duration::from_secs(10_u64), &"Break");
6 let mut val = 0_u8;
7 // timers always run when no previous runs
8 assert!(break_timer.run(&mut || val += 1));
9
10 for _ in 0..100 {
11 // timer will not run as 10 secs has not passed
12 // do run will return false
13 assert!(!break_timer.run(&mut || val += 1));
14 }
15 break_timer.print_stats();
16 assert_eq!(break_timer.total_calls(), &1);
17 assert_eq!(val, 1_u8);
18}
Sourcepub fn run_wait(&mut self, success: &mut dyn FnMut())
pub fn run_wait(&mut self, success: &mut dyn FnMut())
Calling run()
will check the last call time. If max frequency time has not passed the fn will return false.
If max_frequency duration has passed since the last call then the fn will return true
pub fn run_with_msg(&mut self, success: &mut dyn FnMut()) -> bool
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ThrottleTimer
impl RefUnwindSafe for ThrottleTimer
impl Send for ThrottleTimer
impl Sync for ThrottleTimer
impl Unpin for ThrottleTimer
impl UnwindSafe for ThrottleTimer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more