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