Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/cache/src/worker/tests/system_time_stub.rs
1693 views
1
use std::sync::LazyLock;
2
use std::time::{Duration, SystemTime, SystemTimeError};
3
4
pub static NOW: LazyLock<SystemTime> = LazyLock::new(SystemTime::now);
5
6
#[derive(PartialOrd, PartialEq, Ord, Eq)]
7
pub struct SystemTimeStub(SystemTime);
8
9
impl SystemTimeStub {
10
pub fn now() -> Self {
11
Self(*NOW)
12
}
13
14
pub fn checked_add(&self, duration: Duration) -> Option<Self> {
15
self.0.checked_add(duration).map(|t| t.into())
16
}
17
18
pub fn duration_since(&self, earlier: SystemTime) -> Result<Duration, SystemTimeError> {
19
self.0.duration_since(earlier)
20
}
21
}
22
23
impl From<SystemTime> for SystemTimeStub {
24
fn from(time: SystemTime) -> Self {
25
Self(time)
26
}
27
}
28
29