Skip to main content

PeriodicTimer

Struct PeriodicTimer 

Source
pub struct PeriodicTimer { /* private fields */ }
Expand description

A timer that periodically ticks.

A periodic timer can be created using the PeriodicTimer::new() constructor, which requires a reference to a Clock.

§Precision

The timer uses the current thread’s scheduler to schedule its ticks. The precision of the timer is affected by the load on this thread. There are no guarantees about the precision of the timer other than that it will eventually tick. When the thread is healthy, the timer’s period should be close to the specified one.

Note: The periodic timer is not affected by adjustments to the system clock.

§Stream Behavior

PeriodicTimer implements Stream and will never complete. The stream produces a tick every period indefinitely. Use stream combinators like StreamExt::take to limit the number of ticks.

§Examples

§Create a periodic timer

use std::time::Duration;

use futures::StreamExt;
use tick::{Clock, PeriodicTimer, Stopwatch};

let timer = PeriodicTimer::new(clock, Duration::from_millis(1));

timer
    .take(3)
    .for_each(async |()| {
        // Do something every 1ms
    })
    .await;

§Create a periodic timer with initial delay

use std::time::Duration;

use futures::StreamExt;
use tick::{Clock, PeriodicTimer};

// Delay for 10ms before the timer starts ticking
clock.delay(Duration::from_millis(10)).await;

let timer = PeriodicTimer::new(clock, Duration::from_millis(1));

timer
    .take(3)
    .for_each(async |()| {
        // Do something every 1ms
    })
    .await;

Implementations§

Source§

impl PeriodicTimer

Source

pub fn new(clock: &Clock, period: Duration) -> Self

Creates a timer that fires periodically.

Note: The minimum precision of the timer is 1ms. If a smaller period is specified, it will be adjusted to 1ms.

Trait Implementations§

Source§

impl Debug for PeriodicTimer

Source§

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

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

impl Drop for PeriodicTimer

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Stream for PeriodicTimer

Source§

type Item = ()

Values yielded by the stream.
Source§

fn poll_next( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Option<Self::Item>>

Attempt to pull out the next value of this stream, registering the current task for wakeup if the value is not yet available, and returning None if the stream is exhausted. Read more
Source§

fn size_hint(&self) -> (usize, Option<usize>)

Returns the bounds on the remaining length of the stream. 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.