@@ -11,14 +11,24 @@ use crate::*;
1111const NANOSECOND_PER_BASIC_BLOCK : u64 = 10 ;
1212
1313#[ derive( Debug ) ]
14- pub enum Instant {
14+ pub struct Instant {
15+ kind : InstantKind ,
16+ }
17+
18+ #[ derive( Debug ) ]
19+ enum InstantKind {
1520 Host ( StdInstant ) ,
1621 Virtual { nanoseconds : u64 } ,
1722}
1823
1924/// A monotone clock used for `Instant` simulation.
2025#[ derive( Debug ) ]
21- pub enum Clock {
26+ pub struct Clock {
27+ kind : ClockKind ,
28+ }
29+
30+ #[ derive( Debug ) ]
31+ enum ClockKind {
2232 Host {
2333 /// The "time anchor" for this machine's monotone clock.
2434 time_anchor : StdInstant ,
@@ -32,39 +42,42 @@ pub enum Clock {
3242impl Clock {
3343 /// Create a new clock based on the availability of communication with the host.
3444 pub fn new ( communicate : bool ) -> Self {
35- if communicate {
36- Self :: Host { time_anchor : StdInstant :: now ( ) }
45+ let kind = if communicate {
46+ ClockKind :: Host { time_anchor : StdInstant :: now ( ) }
3747 } else {
38- Self :: Virtual { nanoseconds : 0 . into ( ) }
39- }
48+ ClockKind :: Virtual { nanoseconds : 0 . into ( ) }
49+ } ;
50+
51+ Self { kind }
4052 }
4153
4254 /// Get the current time relative to this clock.
4355 pub fn get ( & self ) -> Duration {
44- match self {
45- Self :: Host { time_anchor } => StdInstant :: now ( ) . saturating_duration_since ( * time_anchor) ,
46- Self :: Virtual { nanoseconds } =>
56+ match & self . kind {
57+ ClockKind :: Host { time_anchor } =>
58+ StdInstant :: now ( ) . saturating_duration_since ( * time_anchor) ,
59+ ClockKind :: Virtual { nanoseconds } =>
4760 Duration :: from_nanos ( nanoseconds. load ( Ordering :: Relaxed ) ) ,
4861 }
4962 }
5063
5164 /// Let the time pass for a small interval.
5265 pub fn tick ( & self ) {
53- match self {
54- Self :: Host { .. } => {
66+ match & self . kind {
67+ ClockKind :: Host { .. } => {
5568 // Time will pass without us doing anything.
5669 }
57- Self :: Virtual { nanoseconds } => {
70+ ClockKind :: Virtual { nanoseconds } => {
5871 nanoseconds. fetch_add ( NANOSECOND_PER_BASIC_BLOCK , Ordering :: Relaxed ) ;
5972 }
6073 }
6174 }
6275
6376 /// Sleep for the desired duration.
6477 pub fn sleep ( & self , duration : Duration ) {
65- match self {
66- Self :: Host { .. } => std:: thread:: sleep ( duration) ,
67- Self :: Virtual { nanoseconds } => {
78+ match & self . kind {
79+ ClockKind :: Host { .. } => std:: thread:: sleep ( duration) ,
80+ ClockKind :: Virtual { nanoseconds } => {
6881 // Just pretend that we have slept for some time.
6982 nanoseconds. fetch_add ( duration. as_nanos ( ) . try_into ( ) . unwrap ( ) , Ordering :: Relaxed ) ;
7083 }
@@ -73,30 +86,34 @@ impl Clock {
7386
7487 /// Compute `now + duration` relative to this clock.
7588 pub fn get_time_relative ( & self , duration : Duration ) -> Option < Time > {
76- match self {
77- Self :: Host { .. } =>
89+ match & self . kind {
90+ ClockKind :: Host { .. } =>
7891 StdInstant :: now ( )
7992 . checked_add ( duration)
80- . map ( |instant| Time :: Monotonic ( Instant :: Host ( instant) ) ) ,
81- Self :: Virtual { nanoseconds } =>
93+ . map ( |instant| Time :: Monotonic ( Instant { kind : InstantKind :: Host ( instant) } ) ) ,
94+ ClockKind :: Virtual { nanoseconds } =>
8295 nanoseconds
8396 . load ( Ordering :: Relaxed )
8497 . checked_add ( duration. as_nanos ( ) . try_into ( ) . unwrap ( ) )
85- . map ( |nanoseconds| Time :: Monotonic ( Instant :: Virtual { nanoseconds } ) ) ,
98+ . map ( |nanoseconds| {
99+ Time :: Monotonic ( Instant { kind : InstantKind :: Virtual { nanoseconds } } )
100+ } ) ,
86101 }
87102 }
88103
89104 /// Compute `start + duration` relative to this clock where `start` is the instant of time when
90105 /// this clock was created.
91106 pub fn get_time_absolute ( & self , duration : Duration ) -> Option < Time > {
92- match self {
93- Self :: Host { time_anchor } =>
107+ match & self . kind {
108+ ClockKind :: Host { time_anchor } =>
94109 time_anchor
95110 . checked_add ( duration)
96- . map ( |instant| Time :: Monotonic ( Instant :: Host ( instant) ) ) ,
97- Self :: Virtual { .. } =>
98- Some ( Time :: Monotonic ( Instant :: Virtual {
99- nanoseconds : duration. as_nanos ( ) . try_into ( ) . unwrap ( ) ,
111+ . map ( |instant| Time :: Monotonic ( Instant { kind : InstantKind :: Host ( instant) } ) ) ,
112+ ClockKind :: Virtual { .. } =>
113+ Some ( Time :: Monotonic ( Instant {
114+ kind : InstantKind :: Virtual {
115+ nanoseconds : duration. as_nanos ( ) . try_into ( ) . unwrap ( ) ,
116+ } ,
100117 } ) ) ,
101118 }
102119 }
@@ -105,12 +122,12 @@ impl Clock {
105122 pub fn get_wait_time ( & self , time : & Time ) -> Duration {
106123 match time {
107124 Time :: Monotonic ( instant) =>
108- match ( instant, self ) {
109- ( Instant :: Host ( instant) , Clock :: Host { .. } ) =>
125+ match ( & instant. kind , & self . kind ) {
126+ ( InstantKind :: Host ( instant) , ClockKind :: Host { .. } ) =>
110127 instant. saturating_duration_since ( StdInstant :: now ( ) ) ,
111128 (
112- Instant :: Virtual { nanoseconds } ,
113- Clock :: Virtual { nanoseconds : current_ns } ,
129+ InstantKind :: Virtual { nanoseconds } ,
130+ ClockKind :: Virtual { nanoseconds : current_ns } ,
114131 ) =>
115132 Duration :: from_nanos (
116133 nanoseconds. saturating_sub ( current_ns. load ( Ordering :: Relaxed ) ) ,
0 commit comments