1
- use crate :: fmt;
2
1
use crate :: time:: Duration ;
2
+ use crate :: { fmt, io} ;
3
3
4
4
pub use self :: inner:: Instant ;
5
5
@@ -36,8 +36,8 @@ pub(in crate::sys::unix) struct Timespec {
36
36
37
37
impl SystemTime {
38
38
#[ cfg_attr( target_os = "horizon" , allow( unused) ) ]
39
- pub fn new ( tv_sec : i64 , tv_nsec : i64 ) -> SystemTime {
40
- SystemTime { t : Timespec :: new ( tv_sec, tv_nsec) }
39
+ pub fn new ( tv_sec : i64 , tv_nsec : i64 ) -> Result < SystemTime , io :: Error > {
40
+ Ok ( SystemTime { t : Timespec :: new ( tv_sec, tv_nsec) ? } )
41
41
}
42
42
43
43
pub fn sub_time ( & self , other : & SystemTime ) -> Result < Duration , Duration > {
@@ -53,12 +53,6 @@ impl SystemTime {
53
53
}
54
54
}
55
55
56
- impl From < libc:: timespec > for SystemTime {
57
- fn from ( t : libc:: timespec ) -> SystemTime {
58
- SystemTime { t : Timespec :: from ( t) }
59
- }
60
- }
61
-
62
56
impl fmt:: Debug for SystemTime {
63
57
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
64
58
f. debug_struct ( "SystemTime" )
@@ -70,12 +64,24 @@ impl fmt::Debug for SystemTime {
70
64
71
65
impl Timespec {
72
66
pub const fn zero ( ) -> Timespec {
73
- Timespec :: new ( 0 , 0 )
67
+ unsafe { Self :: new_unchecked ( 0 , 0 ) }
74
68
}
75
69
76
- const fn new ( tv_sec : i64 , tv_nsec : i64 ) -> Timespec {
70
+ const fn new_assert ( tv_sec : i64 , tv_nsec : i64 ) -> Self {
77
71
assert ! ( tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC as i64 ) ;
78
72
// SAFETY: The assert above checks tv_nsec is within the valid range
73
+ unsafe { Timespec :: new_unchecked ( tv_sec as i64 , tv_nsec as i64 ) }
74
+ }
75
+
76
+ const fn new ( tv_sec : i64 , tv_nsec : i64 ) -> Result < Timespec , io:: Error > {
77
+ if tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC as i64 {
78
+ Ok ( unsafe { Self :: new_unchecked ( tv_sec, tv_nsec) } )
79
+ } else {
80
+ Err ( io:: const_io_error!( io:: ErrorKind :: Other , "Invalid time for nanosecond" ) )
81
+ }
82
+ }
83
+
84
+ const unsafe fn new_unchecked ( tv_sec : i64 , tv_nsec : i64 ) -> Timespec {
79
85
Timespec { tv_sec, tv_nsec : unsafe { Nanoseconds ( tv_nsec as u32 ) } }
80
86
}
81
87
@@ -122,7 +128,7 @@ impl Timespec {
122
128
nsec -= NSEC_PER_SEC as u32 ;
123
129
secs = secs. checked_add ( 1 ) ?;
124
130
}
125
- Some ( Timespec :: new ( secs, nsec. into ( ) ) )
131
+ Some ( unsafe { Timespec :: new_unchecked ( secs, nsec. into ( ) ) } )
126
132
}
127
133
128
134
pub fn checked_sub_duration ( & self , other : & Duration ) -> Option < Timespec > {
@@ -134,7 +140,7 @@ impl Timespec {
134
140
nsec += NSEC_PER_SEC as i32 ;
135
141
secs = secs. checked_sub ( 1 ) ?;
136
142
}
137
- Some ( Timespec :: new ( secs, nsec. into ( ) ) )
143
+ Some ( unsafe { Timespec :: new_unchecked ( secs, nsec. into ( ) ) } )
138
144
}
139
145
140
146
#[ allow( dead_code) ]
@@ -170,12 +176,6 @@ impl Timespec {
170
176
}
171
177
}
172
178
173
- impl From < libc:: timespec > for Timespec {
174
- fn from ( t : libc:: timespec ) -> Timespec {
175
- Timespec :: new ( t. tv_sec as i64 , t. tv_nsec as i64 )
176
- }
177
- }
178
-
179
179
#[ cfg( all(
180
180
target_os = "linux" ,
181
181
target_env = "gnu" ,
@@ -204,18 +204,6 @@ impl __timespec64 {
204
204
}
205
205
}
206
206
207
- #[ cfg( all(
208
- target_os = "linux" ,
209
- target_env = "gnu" ,
210
- target_pointer_width = "32" ,
211
- not( target_arch = "riscv32" )
212
- ) ) ]
213
- impl From < __timespec64 > for Timespec {
214
- fn from ( t : __timespec64 ) -> Timespec {
215
- Timespec :: new ( t. tv_sec , t. tv_nsec . into ( ) )
216
- }
217
- }
218
-
219
207
#[ cfg( any(
220
208
all( target_os = "macos" , any( not( target_arch = "aarch64" ) ) ) ,
221
209
target_os = "ios" ,
@@ -274,19 +262,7 @@ mod inner {
274
262
275
263
let mut s = libc:: timeval { tv_sec : 0 , tv_usec : 0 } ;
276
264
cvt ( unsafe { libc:: gettimeofday ( & mut s, ptr:: null_mut ( ) ) } ) . unwrap ( ) ;
277
- return SystemTime :: from ( s) ;
278
- }
279
- }
280
-
281
- impl From < libc:: timeval > for Timespec {
282
- fn from ( t : libc:: timeval ) -> Timespec {
283
- Timespec :: new ( t. tv_sec as i64 , 1000 * t. tv_usec as i64 )
284
- }
285
- }
286
-
287
- impl From < libc:: timeval > for SystemTime {
288
- fn from ( t : libc:: timeval ) -> SystemTime {
289
- SystemTime { t : Timespec :: from ( t) }
265
+ SystemTime { t : Timespec :: new_assert ( s. tv_sec as i64 , s. tv_usec as i64 * 1000 ) }
290
266
}
291
267
}
292
268
@@ -412,13 +388,15 @@ mod inner {
412
388
if let Some ( clock_gettime64) = __clock_gettime64. get ( ) {
413
389
let mut t = MaybeUninit :: uninit ( ) ;
414
390
cvt ( unsafe { clock_gettime64 ( clock, t. as_mut_ptr ( ) ) } ) . unwrap ( ) ;
415
- return Timespec :: from ( unsafe { t. assume_init ( ) } ) ;
391
+ let t = unsafe { t. assume_init ( ) } ;
392
+ return Timespec :: new_assert ( t. tv_sec as i64 , t. tv_nsec as i64 ) ;
416
393
}
417
394
}
418
395
419
396
let mut t = MaybeUninit :: uninit ( ) ;
420
397
cvt ( unsafe { libc:: clock_gettime ( clock, t. as_mut_ptr ( ) ) } ) . unwrap ( ) ;
421
- Timespec :: from ( unsafe { t. assume_init ( ) } )
398
+ let t = unsafe { t. assume_init ( ) } ;
399
+ Timespec :: new_assert ( t. tv_sec as i64 , t. tv_nsec as i64 )
422
400
}
423
401
}
424
402
}
0 commit comments