mirror of
https://github.com/rosenpass/rosenpass.git
synced 2026-02-04 19:11:52 -08:00
feat(util/time): cleanup, document and add tests
Drop the unused `dur` function, it's nowhere found in the code. Document both Timebase and Timebase::now() Add tests Signed-off-by: Paul Spooren <mail@aparcar.org>
This commit is contained in:
committed by
Karolin Varner
parent
9f78531979
commit
abdbf8f3da
@@ -1,20 +1,53 @@
|
||||
use std::time::{Duration, Instant};
|
||||
use std::time::Instant;
|
||||
|
||||
/// A timebase.
|
||||
///
|
||||
/// This is a simple wrapper around `std::time::Instant` that provides a
|
||||
/// convenient way to get the seconds elapsed since the creation of the
|
||||
/// `Timebase` instance.
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Timebase(Instant);
|
||||
|
||||
impl Default for Timebase {
|
||||
// TODO: Implement new()?
|
||||
fn default() -> Self {
|
||||
Self(Instant::now())
|
||||
}
|
||||
}
|
||||
|
||||
impl Timebase {
|
||||
/// Returns the seconds elapsed since the creation of the `Timebase`
|
||||
pub fn now(&self) -> f64 {
|
||||
self.0.elapsed().as_secs_f64()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dur(&self, t: f64) -> Duration {
|
||||
Duration::from_secs_f64(t)
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
|
||||
#[test]
|
||||
fn test_timebase() {
|
||||
let timebase = Timebase::default();
|
||||
let now = timebase.now();
|
||||
assert!(now > 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_timebase_clone() {
|
||||
let timebase = Timebase::default();
|
||||
let timebase_clone = timebase.clone();
|
||||
assert_eq!(timebase.0, timebase_clone.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_timebase_sleep() {
|
||||
let timebase = Timebase::default();
|
||||
sleep(Duration::from_secs(1));
|
||||
let now = timebase.now();
|
||||
assert!(now > 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user