-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add intervals #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| use core::time::Duration; | ||
| use std::time::Instant; | ||
|
|
||
| static TEST_EPOCH: std::sync::OnceLock<Instant> = std::sync::OnceLock::new(); | ||
|
|
||
| fn get_platform_time() -> Duration { | ||
| let epoch = TEST_EPOCH.get_or_init(Instant::now); | ||
| Instant::now().duration_since(*epoch) | ||
| } | ||
|
|
||
| fn main() { | ||
| let spawner: ato::Spawner<2> = ato::Spawner::default(); | ||
| let start_time = get_platform_time(); | ||
| // Run interval task for 3 iterations (500 ms), with sleep in between, which | ||
| // should still be 1500 ms total. | ||
| ato::spawn_task!(spawner, res, { | ||
| let mut interval = | ||
| ato::interval::Interval::new(Duration::from_millis(500), get_platform_time); | ||
| for _ in 0..3 { | ||
| interval.tick().await; | ||
| // sleep for random duration less than the interval | ||
| ato::sleep(Duration::from_millis(200), get_platform_time).await; | ||
| } | ||
| }); | ||
| assert!(res.is_ok()); | ||
| assert!(spawner.run_until_all_done().is_ok()); | ||
|
|
||
| let elapsed = get_platform_time() - start_time; | ||
| assert!(elapsed <= Duration::from_millis(1550)); // with some margin | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| use core::{ | ||
| future::Future, | ||
| pin::Pin, | ||
| task::{Context, Poll}, | ||
| time::Duration, | ||
| }; | ||
|
|
||
| /// A structure that ticks at regular intervals. | ||
| #[derive(Debug)] | ||
| pub struct Interval { | ||
| next_tick: Duration, | ||
| period: Duration, | ||
| time_fn: fn() -> Duration, | ||
| } | ||
|
|
||
| impl Interval { | ||
| /// Creates a new `Interval` that ticks at the specified `period`. | ||
| /// | ||
| /// The first tick completes immediately (burst strategy), similar to Tokio. | ||
| pub fn new(period: Duration, time_fn: fn() -> Duration) -> Self { | ||
| let now = time_fn(); | ||
| Self { | ||
| next_tick: now, | ||
| period, | ||
| time_fn, | ||
| } | ||
| } | ||
|
|
||
| /// 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. | ||
| pub fn tick(&mut self) -> IntervalTick<'_> { | ||
| IntervalTick { interval: 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. | ||
| pub fn reset(&mut self) { | ||
| self.next_tick = (self.time_fn)(); | ||
| } | ||
| } | ||
|
|
||
| /// A future returned by `Interval::tick`. | ||
| pub struct IntervalTick<'a> { | ||
| interval: &'a mut Interval, | ||
| } | ||
|
|
||
| impl Future for IntervalTick<'_> { | ||
| type Output = (); | ||
|
|
||
| fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
| let this = self.get_mut(); | ||
| let now = (this.interval.time_fn)(); | ||
|
|
||
| if now >= this.interval.next_tick { | ||
| this.interval.next_tick += this.interval.period; | ||
| Poll::Ready(()) | ||
| } else { | ||
| Poll::Pending | ||
| } | ||
| } | ||
|
SeaRoll marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.