From fceab0a2ac4b1248857c802c968126b3b500c51d Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 15 Apr 2022 10:56:27 +0100 Subject: [PATCH] Provide FutureTimeInstant, for use by dice This loads and saves as a Duration. Signed-off-by: Ian Jackson --- src/utils.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/utils.rs b/src/utils.rs index cfaafb37..5f852b3f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -443,6 +443,33 @@ entry_define_insert_remove!{ key } +#[derive(Debug,Copy,Clone,Eq,PartialEq,Ord,PartialOrd)] +#[derive(From,Into)] +#[derive(Serialize, Deserialize)] +#[serde(into="Duration", try_from="Duration")] +pub struct FutureInstant(pub Instant); + +impl Into for FutureInstant { + fn into(self) -> Duration { + let now = Instant::now(); + Instant::from(self).checked_duration_since(now).unwrap_or_default() + } +} + +#[derive(Error,Debug)] +#[error("Duration (eg during load) implies out-of-range FutureInstant")] +pub struct FutureInstantOutOfRange; + +impl TryFrom for FutureInstant { + type Error = FutureInstantOutOfRange; + #[throws(FutureInstantOutOfRange)] + fn try_from(duration: Duration) -> FutureInstant { + let now = Instant::now(); + now.checked_add(duration).ok_or(FutureInstantOutOfRange)?.into() + } +} + + #[derive(Debug,Copy,Clone)] pub struct DigestRead { d: D, -- 2.30.2