@@ -10,41 +10,33 @@ pub struct Instant {
10
10
}
11
11
12
12
impl Instant {
13
+ /// Create a new `Instant` from microseconds since the epoch.
13
14
pub const fn from_us ( us : i64 ) -> Self {
14
15
Self { us }
15
16
}
16
17
18
+ /// Returns the point in time as microseconds since the epoch.
17
19
pub const fn as_us ( & self ) -> i64 {
18
20
self . us
19
21
}
20
22
}
21
23
22
- impl core:: fmt:: Display for Instant {
23
- fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
24
- write ! ( f, "{:.2}ms" , self . as_us( ) as f32 / 1000.0 )
25
- }
26
- }
27
-
28
24
#[ derive( Clone , Copy , Debug , PartialEq , PartialOrd , Eq , Ord ) ]
29
25
#[ cfg_attr( feature = "fuzz" , derive( arbitrary:: Arbitrary ) ) ]
30
26
pub struct Duration ( i64 ) ;
31
27
32
28
impl Duration {
29
+ /// Create a new `Duration` from microseconds.
33
30
pub const fn from_us ( us : i64 ) -> Self {
34
31
Self ( us)
35
32
}
36
33
34
+ /// Returns the duration as microseconds.
37
35
pub const fn as_us ( & self ) -> i64 {
38
36
self . 0
39
37
}
40
38
}
41
39
42
- impl core:: fmt:: Display for Duration {
43
- fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
44
- write ! ( f, "{:.2}ms" , self . as_us( ) as f32 / 1000.0 )
45
- }
46
- }
47
-
48
40
impl core:: ops:: Sub for Instant {
49
41
type Output = Self ;
50
42
@@ -100,3 +92,61 @@ impl core::ops::Add<Duration> for Duration {
100
92
Self :: from_us ( self . as_us ( ) + rhs. as_us ( ) )
101
93
}
102
94
}
95
+
96
+ impl core:: fmt:: Display for Instant {
97
+ fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
98
+ write ! ( f, "{:.2}ms" , self . as_us( ) as f32 / 1000.0 )
99
+ }
100
+ }
101
+
102
+ impl core:: fmt:: Display for Duration {
103
+ fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
104
+ write ! ( f, "{:.2}ms" , self . as_us( ) as f32 / 1000.0 )
105
+ }
106
+ }
107
+
108
+ #[ cfg( test) ]
109
+ mod tests {
110
+ use super :: * ;
111
+
112
+ #[ test]
113
+ fn instant ( ) {
114
+ let a = Instant :: from_us ( 100 ) ;
115
+ assert_eq ! ( a. us, 100 ) ;
116
+ assert_eq ! ( a. as_us( ) , 100 ) ;
117
+ }
118
+
119
+ #[ test]
120
+ fn instant_operations ( ) {
121
+ let a = Instant :: from_us ( 100 ) ;
122
+ let b = Instant :: from_us ( 50 ) ;
123
+ assert_eq ! ( ( a - b) . as_us( ) , 50 ) ;
124
+ assert_eq ! ( ( a - Duration :: from_us( 50 ) ) . as_us( ) , 50 ) ;
125
+ assert_eq ! ( ( a + Duration :: from_us( 50 ) ) . as_us( ) , 150 ) ;
126
+ }
127
+
128
+ #[ test]
129
+ fn duration ( ) {
130
+ let a = Duration :: from_us ( 100 ) ;
131
+ assert_eq ! ( a. 0 , 100 ) ;
132
+ assert_eq ! ( a. as_us( ) , 100 ) ;
133
+ }
134
+
135
+ #[ test]
136
+ fn duration_operations ( ) {
137
+ let a = Duration :: from_us ( 100 ) ;
138
+ let b = Duration :: from_us ( 50 ) ;
139
+ assert_eq ! ( ( a - b) . as_us( ) , 50 ) ;
140
+ assert_eq ! ( ( a * 2 ) . as_us( ) , 200 ) ;
141
+ assert_eq ! ( ( a / 2 ) . as_us( ) , 50 ) ;
142
+ assert_eq ! ( ( a + b) . as_us( ) , 150 ) ;
143
+ }
144
+
145
+ #[ test]
146
+ fn formatting ( ) {
147
+ let a = Instant :: from_us ( 100 ) ;
148
+ let b = Duration :: from_us ( 100 ) ;
149
+ assert_eq ! ( format!( "{}" , a) , "0.10ms" ) ;
150
+ assert_eq ! ( format!( "{}" , b) , "0.10ms" ) ;
151
+ }
152
+ }
0 commit comments