Add from_secs, from_millis.

Uses a hardcoded tick rate for now.
This commit is contained in:
Dario Nieuwenhuis 2020-09-28 21:46:03 +02:00
parent 68eac3a57c
commit 3d64a8abef

View File

@ -25,6 +25,9 @@ pub struct Instant {
ticks: u64, ticks: u64,
} }
// TODO allow customizing, probably via Cargo features `tick-hz-32768` or something.
pub const TICKS_PER_SECOND: u32 = 32768;
impl Instant { impl Instant {
pub fn now() -> Instant { pub fn now() -> Instant {
Instant { ticks: now() } Instant { ticks: now() }
@ -128,6 +131,18 @@ impl Duration {
Duration { ticks } Duration { ticks }
} }
pub const fn from_secs(secs: u32) -> Duration {
Duration {
ticks: secs * TICKS_PER_SECOND,
}
}
pub const fn from_millis(millis: u32) -> Duration {
Duration {
ticks: millis * TICKS_PER_SECOND / 1000,
}
}
pub fn checked_add(self, rhs: Duration) -> Option<Duration> { pub fn checked_add(self, rhs: Duration) -> Option<Duration> {
self.ticks self.ticks
.checked_add(rhs.ticks) .checked_add(rhs.ticks)