-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor to add an interval type (#252)
Adds a new interval type so we can reduce code repitition across the samplers.
- Loading branch information
Showing
25 changed files
with
243 additions
and
754 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,43 @@ | ||
use crate::Instant; | ||
use core::time::Duration; | ||
|
||
pub struct Interval { | ||
prev: Instant, | ||
next: Instant, | ||
period: Duration, | ||
} | ||
|
||
impl Interval { | ||
pub fn new(start: Instant, period: Duration) -> Self { | ||
Self { | ||
prev: start, | ||
next: start, | ||
period, | ||
} | ||
} | ||
|
||
/// Try to tick the interval forward to the provided instant. Returns true | ||
/// if the interval has fired and returns false otherwise. | ||
pub fn try_wait(&mut self, now: Instant) -> Result<Duration, ()> { | ||
if now < self.next { | ||
return Err(()); | ||
} | ||
|
||
let next = self.next + self.period; | ||
|
||
// check if we have fallen behind | ||
if next > now { | ||
self.next = next; | ||
} else { | ||
// if we fell behind, don't sample again until the interval has | ||
// elapsed | ||
self.next = now + self.period; | ||
} | ||
|
||
let elapsed = now - self.prev; | ||
|
||
self.prev = now; | ||
|
||
Ok(elapsed) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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.