Timer

Struct Timer 

Source
pub struct Timer<S: TimerState> { /* private fields */ }
Expand description

Timer which can either be in a paused state or a running state. To instantiate the timer run Timer::new(). To actually start it call Timer::init()

§Example

use zentime_rs_timer::timer::{Timer, TimerEndHandler, TimerTickHandler, TimerStatus, Paused};
use zentime_rs_timer::TimerAction;
use std::thread;

// Handler which is passed to our timer implementation
pub struct OnEndHandler { }

impl TimerEndHandler for OnEndHandler {
    fn call(&mut self) {
        println!("Hi from timer");
    }
}

pub struct OnTickHandler {}

impl TimerTickHandler for OnTickHandler {
    fn call(&mut self, status: TimerStatus) -> Option<TimerAction> {
        println!("{}", status.current_time);
        None
    }
}

// Run timer in its own thread so it does not block the current one
thread::spawn(move || {
    Timer::<Paused>::new(
        10,
        Some(OnEndHandler {}),
        Some(OnTickHandler {})
    )
    .init();
});

Implementations§

Source§

impl Timer<Paused>

Implementation of the Paused state for Timer

Source

pub fn new<E, T>(time: u64, on_timer_end: Option<E>, on_tick: Option<T>) -> Self
where E: TimerEndHandler + 'static, T: TimerTickHandler + 'static,

Creates a new timer in paused state. You have to call Self::init() to start the timer

Source

pub fn init(self)

Puts the paused timer into a waiting state waiting for input (e.g. to unpause the timer and transition it into a running state).

Source§

impl Timer<Running>

Source

pub fn new<E, T>(time: u64, on_timer_end: Option<E>, on_tick: Option<T>) -> Self
where E: TimerEndHandler + 'static, T: TimerTickHandler + 'static,

Creates a new timer in paused state. You have to call Self::init() to start the timer

Source

pub fn init(self)

Runs the timer and awaits input. Depending on the input [TimerInputAction] the timer might transition into a paused state or skip to the next interval.

Trait Implementations§

Source§

impl<S: TimerState + Debug> Debug for Timer<S>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S> Freeze for Timer<S>
where S: Freeze,

§

impl<S> !RefUnwindSafe for Timer<S>

§

impl<S> !Send for Timer<S>

§

impl<S> !Sync for Timer<S>

§

impl<S> Unpin for Timer<S>
where S: Unpin,

§

impl<S> !UnwindSafe for Timer<S>

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.