Skip to main content

Interval

Struct Interval 

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

A structure that ticks at regular intervals.

Implementations§

Source§

impl Interval

Source

pub fn new(period: Duration, time_fn: fn() -> Duration) -> Self

Creates a new Interval that ticks at the specified period.

The first tick completes immediately (burst strategy), similar to Tokio.

Examples found in repository?
examples/interval.rs (line 18)
11fn main() {
12    let spawner: ato::Spawner<2> = ato::Spawner::default();
13    let start_time = get_platform_time();
14    // Run interval task for 3 iterations (500 ms), with sleep in between, which
15    // should still be 1500 ms total.
16    ato::spawn_task!(spawner, res, {
17        let mut interval =
18            ato::interval::Interval::new(Duration::from_millis(500), get_platform_time);
19        for _ in 0..3 {
20            interval.tick().await;
21            // sleep for random duration less than the interval
22            ato::sleep(Duration::from_millis(200), get_platform_time).await;
23        }
24    });
25    assert!(res.is_ok());
26    assert!(spawner.run_until_all_done().is_ok());
27
28    let elapsed = get_platform_time() - start_time;
29    assert!(elapsed <= Duration::from_millis(1550)); // with some margin
30}
Source

pub fn tick(&mut self) -> IntervalTick<'_>

Creates a future that completes when the next interval is reached.

The returned future borrows the interval, allowing the Interval to track state across multiple calls.

Examples found in repository?
examples/interval.rs (line 20)
11fn main() {
12    let spawner: ato::Spawner<2> = ato::Spawner::default();
13    let start_time = get_platform_time();
14    // Run interval task for 3 iterations (500 ms), with sleep in between, which
15    // should still be 1500 ms total.
16    ato::spawn_task!(spawner, res, {
17        let mut interval =
18            ato::interval::Interval::new(Duration::from_millis(500), get_platform_time);
19        for _ in 0..3 {
20            interval.tick().await;
21            // sleep for random duration less than the interval
22            ato::sleep(Duration::from_millis(200), get_platform_time).await;
23        }
24    });
25    assert!(res.is_ok());
26    assert!(spawner.run_until_all_done().is_ok());
27
28    let elapsed = get_platform_time() - start_time;
29    assert!(elapsed <= Duration::from_millis(1550)); // with some margin
30}
Source

pub fn reset(&mut self)

Resets the interval to start ticking from the current instant.

This is useful if the system was paused or lagged significantly and you want to skip the “burst” of missed ticks.

Trait Implementations§

Source§

impl Debug for Interval

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.