1
1
use std:: { fmt, ops} ;
2
- use libc:: { time_t, suseconds_t} ;
3
-
4
- #[ repr( C ) ]
5
- #[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Debug ) ]
6
- pub struct TimeVal {
7
- pub tv_sec : time_t ,
8
- pub tv_usec : suseconds_t ,
9
- }
2
+ use libc:: { time_t, suseconds_t, timeval} ;
10
3
11
4
const MICROS_PER_SEC : i64 = 1_000_000 ;
12
5
const SECS_PER_MINUTE : i64 = 60 ;
13
6
const SECS_PER_HOUR : i64 = 3600 ;
14
7
15
- #[ cfg ( target_pointer_width = "64" ) ]
16
- const MAX_SECONDS : i64 = ( :: std :: i64 :: MAX / MICROS_PER_SEC ) - 1 ;
8
+ #[ derive ( Clone , Copy ) ]
9
+ pub struct TimeVal ( timeval ) ;
17
10
18
- #[ cfg( target_pointer_width = "32" ) ]
19
- const MAX_SECONDS : i64 = :: std:: isize:: MAX as i64 ;
11
+ impl AsRef < timeval > for TimeVal {
12
+ fn as_ref ( & self ) -> & timeval {
13
+ & self . 0
14
+ }
15
+ }
20
16
21
- const MIN_SECONDS : i64 = -MAX_SECONDS ;
17
+ impl AsMut < timeval > for TimeVal {
18
+ fn as_mut ( & mut self ) -> & mut timeval {
19
+ & mut self . 0
20
+ }
21
+ }
22
22
23
23
impl TimeVal {
24
24
#[ inline]
@@ -44,8 +44,8 @@ impl TimeVal {
44
44
45
45
#[ inline]
46
46
pub fn seconds ( seconds : i64 ) -> TimeVal {
47
- assert ! ( seconds >= MIN_SECONDS && seconds <= MAX_SECONDS , "TimeVal out of bounds; seconds={}" , seconds) ;
48
- TimeVal { tv_sec : seconds as time_t , tv_usec : 0 }
47
+ assert ! ( seconds >= time_t :: min_value ( ) && seconds <= time_t :: max_value ( ) , "TimeVal out of bounds; seconds={}" , seconds) ;
48
+ TimeVal ( timeval { tv_sec : seconds as time_t , tv_usec : 0 } )
49
49
}
50
50
51
51
#[ inline]
@@ -60,8 +60,8 @@ impl TimeVal {
60
60
#[ inline]
61
61
pub fn microseconds ( microseconds : i64 ) -> TimeVal {
62
62
let ( secs, micros) = div_mod_floor_64 ( microseconds, MICROS_PER_SEC ) ;
63
- assert ! ( secs >= MIN_SECONDS && secs <= MAX_SECONDS , "TimeVal out of bounds" ) ;
64
- TimeVal { tv_sec : secs as time_t , tv_usec : micros as suseconds_t }
63
+ assert ! ( secs >= time_t :: min_value ( ) && secs <= time_t :: max_value ( ) , "TimeVal out of bounds; seconds={}" , secs ) ;
64
+ TimeVal ( timeval { tv_sec : secs as time_t , tv_usec : micros as suseconds_t } )
65
65
}
66
66
67
67
pub fn num_hours ( & self ) -> i64 {
@@ -73,10 +73,10 @@ impl TimeVal {
73
73
}
74
74
75
75
pub fn num_seconds ( & self ) -> i64 {
76
- if self . tv_sec < 0 && self . tv_usec > 0 {
77
- ( self . tv_sec + 1 ) as i64
76
+ if self . 0 . tv_sec < 0 && self . 0 . tv_usec > 0 {
77
+ ( self . 0 . tv_sec + 1 ) as i64
78
78
} else {
79
- self . tv_sec as i64
79
+ self . 0 . tv_sec as i64
80
80
}
81
81
}
82
82
@@ -91,10 +91,10 @@ impl TimeVal {
91
91
}
92
92
93
93
fn micros_mod_sec ( & self ) -> suseconds_t {
94
- if self . tv_sec < 0 && self . tv_usec > 0 {
95
- self . tv_usec - MICROS_PER_SEC as suseconds_t
94
+ if self . 0 . tv_sec < 0 && self . 0 . tv_usec > 0 {
95
+ self . 0 . tv_usec - MICROS_PER_SEC as suseconds_t
96
96
} else {
97
- self . tv_usec
97
+ self . 0 . tv_usec
98
98
}
99
99
}
100
100
}
@@ -147,32 +147,40 @@ impl ops::Div<i32> for TimeVal {
147
147
148
148
impl fmt:: Display for TimeVal {
149
149
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
150
- let ( abs, sign) = if self . tv_sec < 0 {
150
+ let ( abs, sign) = if self . 0 . tv_sec < 0 {
151
151
( -* self , "-" )
152
152
} else {
153
153
( * self , "" )
154
154
} ;
155
155
156
- let sec = abs. tv_sec ;
156
+ let sec = abs. 0 . tv_sec ;
157
157
158
158
try!( write ! ( f, "{}" , sign) ) ;
159
159
160
- if abs. tv_usec == 0 {
161
- if abs. tv_sec == 1 {
160
+ if abs. 0 . tv_usec == 0 {
161
+ if abs. 0 . tv_sec == 1 {
162
162
try!( write ! ( f, "{} second" , sec) ) ;
163
163
} else {
164
164
try!( write ! ( f, "{} seconds" , sec) ) ;
165
165
}
166
- } else if abs. tv_usec % 1000 == 0 {
167
- try!( write ! ( f, "{}.{:03} seconds" , sec, abs. tv_usec / 1000 ) ) ;
166
+ } else if abs. 0 . tv_usec % 1_000 == 0 {
167
+ try!( write ! ( f, "{}.{:03} seconds" , sec, abs. 0 . tv_usec / 1_000 ) ) ;
168
168
} else {
169
- try!( write ! ( f, "{}.{:06} seconds" , sec, abs. tv_usec) ) ;
169
+ try!( write ! ( f, "{}.{:06} seconds" , sec, abs. 0 . tv_usec) ) ;
170
170
}
171
171
172
172
Ok ( ( ) )
173
173
}
174
174
}
175
175
176
+ impl PartialEq for TimeVal {
177
+ fn eq ( & self , rhs : & TimeVal ) -> bool {
178
+ self . 0 . tv_sec == rhs. 0 . tv_sec && self . 0 . tv_usec == rhs. 0 . tv_usec
179
+ }
180
+ }
181
+
182
+ impl Eq for TimeVal { }
183
+
176
184
#[ inline]
177
185
fn div_mod_floor_64 ( this : i64 , other : i64 ) -> ( i64 , i64 ) {
178
186
( div_floor_64 ( this, other) , mod_floor_64 ( this, other) )
@@ -208,17 +216,16 @@ mod test {
208
216
#[ test]
209
217
pub fn test_time_val ( ) {
210
218
assert ! ( TimeVal :: seconds( 1 ) != TimeVal :: zero( ) ) ;
211
- assert_eq ! ( TimeVal :: seconds( 1 ) + TimeVal :: seconds( 2 ) , TimeVal :: seconds( 3 ) ) ;
212
- assert_eq ! ( TimeVal :: minutes( 3 ) + TimeVal :: seconds( 2 ) ,
213
- TimeVal :: seconds( 182 ) ) ;
219
+ assert ! ( TimeVal :: seconds( 1 ) + TimeVal :: seconds( 2 ) == TimeVal :: seconds( 3 ) ) ;
220
+ assert ! ( TimeVal :: minutes( 3 ) + TimeVal :: seconds( 2 ) == TimeVal :: seconds( 182 ) ) ;
214
221
}
215
222
216
223
#[ test]
217
224
pub fn test_time_val_neg ( ) {
218
225
let a = TimeVal :: seconds ( 1 ) + TimeVal :: microseconds ( 123 ) ;
219
226
let b = TimeVal :: seconds ( -1 ) + TimeVal :: microseconds ( -123 ) ;
220
227
221
- assert_eq ! ( a, -b) ;
228
+ assert ! ( a == -b) ;
222
229
}
223
230
224
231
#[ test]
0 commit comments