From: Ian Jackson Date: Fri, 15 Apr 2022 09:56:27 +0000 (+0100) Subject: Provide FutureTimeInstant, for use by dice X-Git-Tag: otter-1.1.0~574 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=fceab0a2ac4b1248857c802c968126b3b500c51d;p=otter.git Provide FutureTimeInstant, for use by dice This loads and saves as a Duration. Signed-off-by: Ian Jackson --- 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,