@@ -11,14 +11,24 @@ use crate::*;
11
11
const NANOSECOND_PER_BASIC_BLOCK : u64 = 10 ;
12
12
13
13
#[ derive( Debug ) ]
14
- pub enum Instant {
14
+ pub struct Instant {
15
+ kind : InstantKind ,
16
+ }
17
+
18
+ #[ derive( Debug ) ]
19
+ enum InstantKind {
15
20
Host ( StdInstant ) ,
16
21
Virtual { nanoseconds : u64 } ,
17
22
}
18
23
19
24
/// A monotone clock used for `Instant` simulation.
20
25
#[ derive( Debug ) ]
21
- pub enum Clock {
26
+ pub struct Clock {
27
+ kind : ClockKind ,
28
+ }
29
+
30
+ #[ derive( Debug ) ]
31
+ enum ClockKind {
22
32
Host {
23
33
/// The "time anchor" for this machine's monotone clock.
24
34
time_anchor : StdInstant ,
@@ -32,39 +42,42 @@ pub enum Clock {
32
42
impl Clock {
33
43
/// Create a new clock based on the availability of communication with the host.
34
44
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 ( ) }
37
47
} else {
38
- Self :: Virtual { nanoseconds : 0 . into ( ) }
39
- }
48
+ ClockKind :: Virtual { nanoseconds : 0 . into ( ) }
49
+ } ;
50
+
51
+ Self { kind }
40
52
}
41
53
42
54
/// Get the current time relative to this clock.
43
55
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 } =>
47
60
Duration :: from_nanos ( nanoseconds. load ( Ordering :: Relaxed ) ) ,
48
61
}
49
62
}
50
63
51
64
/// Let the time pass for a small interval.
52
65
pub fn tick ( & self ) {
53
- match self {
54
- Self :: Host { .. } => {
66
+ match & self . kind {
67
+ ClockKind :: Host { .. } => {
55
68
// Time will pass without us doing anything.
56
69
}
57
- Self :: Virtual { nanoseconds } => {
70
+ ClockKind :: Virtual { nanoseconds } => {
58
71
nanoseconds. fetch_add ( NANOSECOND_PER_BASIC_BLOCK , Ordering :: Relaxed ) ;
59
72
}
60
73
}
61
74
}
62
75
63
76
/// Sleep for the desired duration.
64
77
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 } => {
68
81
// Just pretend that we have slept for some time.
69
82
nanoseconds. fetch_add ( duration. as_nanos ( ) . try_into ( ) . unwrap ( ) , Ordering :: Relaxed ) ;
70
83
}
@@ -73,30 +86,34 @@ impl Clock {
73
86
74
87
/// Compute `now + duration` relative to this clock.
75
88
pub fn get_time_relative ( & self , duration : Duration ) -> Option < Time > {
76
- match self {
77
- Self :: Host { .. } =>
89
+ match & self . kind {
90
+ ClockKind :: Host { .. } =>
78
91
StdInstant :: now ( )
79
92
. 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 } =>
82
95
nanoseconds
83
96
. load ( Ordering :: Relaxed )
84
97
. 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
+ } ) ,
86
101
}
87
102
}
88
103
89
104
/// Compute `start + duration` relative to this clock where `start` is the instant of time when
90
105
/// this clock was created.
91
106
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 } =>
94
109
time_anchor
95
110
. 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
+ } ,
100
117
} ) ) ,
101
118
}
102
119
}
@@ -105,12 +122,12 @@ impl Clock {
105
122
pub fn get_wait_time ( & self , time : & Time ) -> Duration {
106
123
match time {
107
124
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 { .. } ) =>
110
127
instant. saturating_duration_since ( StdInstant :: now ( ) ) ,
111
128
(
112
- Instant :: Virtual { nanoseconds } ,
113
- Clock :: Virtual { nanoseconds : current_ns } ,
129
+ InstantKind :: Virtual { nanoseconds } ,
130
+ ClockKind :: Virtual { nanoseconds : current_ns } ,
114
131
) =>
115
132
Duration :: from_nanos (
116
133
nanoseconds. saturating_sub ( current_ns. load ( Ordering :: Relaxed ) ) ,
0 commit comments