-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.rs
More file actions
88 lines (73 loc) · 2.4 KB
/
lib.rs
File metadata and controls
88 lines (73 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! # Standard `embedded-time`
//!
//! This library provides an [embedded_time::Clock] that can be used for host-side testing.
//!
//! The provided [embedded_time::Clock] implementation is based on [std::time].
//!
//! # Usage
//!
//! ```rust
//! use std_embedded_time::StandardClock;
//! use embedded_time::Clock;
//!
//! fn main() {
//! let clock = StandardClock::default();
//!
//! let now = clock.try_now().unwrap();
//! println!("Current time: {:?}", now);
//! }
//! ```
pub use embedded_time;
use embedded_time::{Instant, fraction::Fraction};
/// A clock with nanosecond precision.
///
/// To construct a clock, use [StandardClock::default()].
///
/// The clock is "started" when it is constructed.
///
/// # Limitations
/// The clock represents up to ~584 years worth of time, after which it will roll over.
#[derive(Copy, Clone, Debug)]
pub struct StandardClock {
start: std::time::Instant,
}
impl Default for StandardClock {
fn default() -> Self {
Self {
start: std::time::Instant::now(),
}
}
}
impl embedded_time::Clock for StandardClock {
/// With a 64-bit tick register, the clock can represent times up to approximately 594 years in
/// duration, after which the clock will roll over.
type T = u64;
/// Each tick of the clock is equivalent to 1 nanosecond.
const SCALING_FACTOR: Fraction = Fraction::new(1, 1_000_000_000);
/// Get the current time from the clock.
fn try_now(&self) -> Result<Instant<Self>, embedded_time::clock::Error> {
let now = std::time::Instant::now();
let elapsed = now.duration_since(self.start);
// Note: We are discarding the upper 64 bits of nanoseconds. However, even while doing so,
// we can represent ~594 years of time, so this should not be relevant.
Ok(Instant::new(elapsed.as_nanos() as u64))
}
}
#[cfg(test)]
mod test {
use super::StandardClock;
use embedded_time::{Clock, duration::*};
use std::time::Duration;
#[test]
fn test_measurement() {
let clock = StandardClock::default();
let start = clock.try_now().unwrap();
std::thread::sleep(Duration::from_secs(1));
let end = clock.try_now().unwrap();
let elapsed = end - start;
let lower_bound = Milliseconds(999u32);
assert!(elapsed > lower_bound);
let upper_bound = 2000u32.milliseconds();
assert!(elapsed < upper_bound);
}
}