Skip to main content

Timer

Struct Timer 

Source
pub struct Timer<E: IsEvent<Event = E>> {
    pub duration: Duration,
    pub on_elapsed: fn(&SenderComponent<E>),
    /* private fields */
}

Fields§

§duration: Duration§on_elapsed: fn(&SenderComponent<E>)

Implementations§

Source§

impl<E: IsEvent<Event = E>> Timer<E>

Source

pub fn new(duration: Duration) -> Self

Source

pub fn start(&mut self)

Schedule the timer to run once.

Examples found in repository?
examples/blinky_without_macro/main.rs (line 41)
30    pub fn on(&mut self, event: &Event) -> Response<Self, Event> {
31        match event {
32
33            Event::OnEntry => {
34                self.light = true;
35                self.counter += 1;
36                if self.counter == 10 {
37                    let id = self.get_id().unwrap();
38                    self.publish(Event::Detach(id));
39                    println!("{}: done", &id);
40                }
41                self.timer.start();
42                Response::Handled
43            }
44
45            Event::TimerElapsed => {
46                Response::Transition(Self::off)
47            }
48
49            _ => Response::Handled
50        }
51    }
52
53    fn off(&mut self, event: &Event) -> Response<Self, Event> {
54        match event {
55
56            Event::OnEntry => {
57                self.light = false;
58                self.timer.start();
59                Response::Handled
60            }
61
62            Event::TimerElapsed => {
63                Response::Transition(Self::on)
64            }
65
66            _ => Response::Handled
67        }
68    }
More examples
Hide additional examples
examples/blinky/main.rs (line 44)
33        pub fn on(&mut self, event: &Event) -> Response<Self, Event> {
34            match event {
35
36                Event::OnEntry => {
37                    self.light = true;
38                    self.counter += 1;
39                    if self.counter == 10 {
40                        let id = self.get_id().unwrap();
41                        self.publish(Event::Detach(id));
42                        println!("{}: done", &id);
43                    }
44                    self.timer.start();
45                    Response::Handled
46                }
47
48                Event::TimerElapsed => {
49                    Response::Transition(Self::off)
50                }
51
52                _ => Response::Handled
53
54            }
55        }
56
57        fn off(&mut self, event: &Event) -> Response<Self, Event> {
58            match event {
59
60                Event::OnEntry => {
61                    self.light = false;
62                    self.timer.start();
63                    Response::Handled
64                }
65
66                Event::TimerElapsed => {
67                    Response::Transition(Self::on)
68                }
69
70                _ => Response::Handled
71
72            }
73        }
Source

pub fn start_interval(&mut self)

Schedule the timer to run repeatedly.

Source

pub fn cancel(&mut self)

Cancel the timer if it was running.

Trait Implementations§

Source§

impl<E: Clone + IsEvent<Event = E>> Clone for Timer<E>

Source§

fn clone(&self) -> Timer<E>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<E: IsEvent<Event = E>> Debug for Timer<E>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<E: IsEvent<Event = E>> Default for Timer<E>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<E: IsEvent<Event = E>> Sender for Timer<E>

Source§

type Event = E

Source§

fn get_sender_component_mut(&mut self) -> &mut SenderComponent<Self::Event>

Get a mutable reference to the event sender component.
Source§

fn get_sender_component(&self) -> &SenderComponent<Self::Event>

Get an immutable reference to the event sender component.
Source§

fn set_sender_component( &mut self, sender_component: SenderComponent<Self::Event>, )

Set the event sender component.
Source§

fn get_sender(&self) -> UnboundedSender<Envelope<Self::Event>>

Get the event sender
Source§

fn set_sender(&mut self, sender: UnboundedSender<Envelope<Self::Event>>)

Set the event sender
Source§

fn clear_sender(&mut self)

Clear the event sender
Source§

fn set_associated_handler_id(&mut self, id: usize)

Get associated event handler id, this is the id of the event handler that owns the event sender.
Source§

fn get_associated_handler_id(&self) -> Option<usize>

Get the associated event handler id.
Source§

fn clear_associated_handler_id(&mut self)

Clear the associated event handler id.
Source§

fn publish(&self, event: Self::Event)

Publish an event to all handlers.
Source§

fn post(&self, event: Self::Event, handler_id: usize)

Post an event to a specific handler.
Source§

fn post_to_self(&self, event: Self::Event)

Post an event to event handler to which the event sender is attached.
Source§

fn send(&self, envelope: Envelope<Self::Event>)

Send an envelope

Auto Trait Implementations§

§

impl<E> Freeze for Timer<E>

§

impl<E> !RefUnwindSafe for Timer<E>

§

impl<E> Send for Timer<E>

§

impl<E> Sync for Timer<E>

§

impl<E> Unpin for Timer<E>

§

impl<E> UnsafeUnpin for Timer<E>

§

impl<E> !UnwindSafe for Timer<E>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.