pub struct Interval { /* private fields */ }Expand description
A structure that ticks at regular intervals.
Implementations§
Source§impl Interval
impl Interval
Sourcepub fn new(period: Duration, time_fn: fn() -> Duration) -> Self
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}Sourcepub fn tick(&mut self) -> IntervalTick<'_> ⓘ
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}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Interval
impl RefUnwindSafe for Interval
impl Send for Interval
impl Sync for Interval
impl Unpin for Interval
impl UnsafeUnpin for Interval
impl UnwindSafe for Interval
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more