Struct ThrottleTimer

Source
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);
Source

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}
Source

pub const fn event_name(&self) -> &str

Source

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}
Source

pub const fn max_frequency(&self) -> &Duration

Source

pub const fn created_date(&self) -> SystemTime

Source

pub fn wait_time(&self) -> Duration

Source

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}
Source

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

Source

pub fn run_throttle_cb( &mut self, success: &mut dyn FnMut(), throttled: &mut dyn FnMut(), ) -> bool

Source

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}
Source

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

Source

pub fn run_with_msg(&mut self, success: &mut dyn FnMut()) -> bool

Trait Implementations§

Source§

impl Debug for ThrottleTimer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.