diff --git a/src/shims/time.rs b/src/shims/time.rs index 77f46ac6c3d4..fa01b7b1caee 100644 --- a/src/shims/time.rs +++ b/src/shims/time.rs @@ -11,14 +11,24 @@ use crate::*; const NANOSECOND_PER_BASIC_BLOCK: u64 = 10; #[derive(Debug)] -pub enum Instant { +pub struct Instant { + kind: InstantKind, +} + +#[derive(Debug)] +enum InstantKind { Host(StdInstant), Virtual { nanoseconds: u64 }, } /// A monotone clock used for `Instant` simulation. #[derive(Debug)] -pub enum Clock { +pub struct Clock { + kind: ClockKind, +} + +#[derive(Debug)] +enum ClockKind { Host { /// The "time anchor" for this machine's monotone clock. time_anchor: StdInstant, @@ -32,29 +42,32 @@ pub enum Clock { impl Clock { /// Create a new clock based on the availability of communication with the host. pub fn new(communicate: bool) -> Self { - if communicate { - Self::Host { time_anchor: StdInstant::now() } + let kind = if communicate { + ClockKind::Host { time_anchor: StdInstant::now() } } else { - Self::Virtual { nanoseconds: 0.into() } - } + ClockKind::Virtual { nanoseconds: 0.into() } + }; + + Self { kind } } /// Get the current time relative to this clock. pub fn get(&self) -> Duration { - match self { - Self::Host { time_anchor } => StdInstant::now().saturating_duration_since(*time_anchor), - Self::Virtual { nanoseconds } => + match &self.kind { + ClockKind::Host { time_anchor } => + StdInstant::now().saturating_duration_since(*time_anchor), + ClockKind::Virtual { nanoseconds } => Duration::from_nanos(nanoseconds.load(Ordering::Relaxed)), } } /// Let the time pass for a small interval. pub fn tick(&self) { - match self { - Self::Host { .. } => { + match &self.kind { + ClockKind::Host { .. } => { // Time will pass without us doing anything. } - Self::Virtual { nanoseconds } => { + ClockKind::Virtual { nanoseconds } => { nanoseconds.fetch_add(NANOSECOND_PER_BASIC_BLOCK, Ordering::Relaxed); } } @@ -62,9 +75,9 @@ impl Clock { /// Sleep for the desired duration. pub fn sleep(&self, duration: Duration) { - match self { - Self::Host { .. } => std::thread::sleep(duration), - Self::Virtual { nanoseconds } => { + match &self.kind { + ClockKind::Host { .. } => std::thread::sleep(duration), + ClockKind::Virtual { nanoseconds } => { // Just pretend that we have slept for some time. nanoseconds.fetch_add(duration.as_nanos().try_into().unwrap(), Ordering::Relaxed); } @@ -73,30 +86,34 @@ impl Clock { /// Compute `now + duration` relative to this clock. pub fn get_time_relative(&self, duration: Duration) -> Option