#![forbid(unsafe_code)]
use std::time::Duration;
#[cfg(target_os = "windows")]
mod windows;
#[cfg(target_os = "windows")]
use windows as inner;
#[cfg(unix)]
mod unix;
#[cfg(unix)]
use unix as inner;
#[derive(Clone, Copy, Debug)]
pub struct Instant {
t: inner::Instant,
}
impl Instant {
pub fn now() -> Self {
Self {
t: inner::Instant::now(),
}
}
pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> {
self.t.checked_duration_since(earlier.t)
}
pub fn duration_since(&self, earlier: Instant) -> Duration {
self.checked_duration_since(earlier)
.unwrap_or(Duration::ZERO)
}
pub fn elapsed(&self) -> Duration {
Self::now().duration_since(*self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn time_increments_after_sleep() {
let sleep_time = Duration::from_millis(10);
let start = Instant::now();
std::thread::sleep(sleep_time);
assert!(start.elapsed() >= sleep_time);
}
}