1- use crate :: fmt;
21use crate :: time:: Duration ;
2+ use crate :: { fmt, io} ;
33
44pub use self :: inner:: Instant ;
55
@@ -36,8 +36,8 @@ pub(in crate::sys::unix) struct Timespec {
3636
3737impl SystemTime {
3838 #[ cfg_attr( any( target_os = "horizon" , target_os = "hurd" ) , 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) ? } )
4141 }
4242
4343 pub fn sub_time ( & self , other : & SystemTime ) -> Result < Duration , Duration > {
@@ -53,12 +53,6 @@ impl SystemTime {
5353 }
5454}
5555
56- impl From < libc:: timespec > for SystemTime {
57- fn from ( t : libc:: timespec ) -> SystemTime {
58- SystemTime { t : Timespec :: from ( t) }
59- }
60- }
61-
6256impl fmt:: Debug for SystemTime {
6357 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
6458 f. debug_struct ( "SystemTime" )
@@ -70,12 +64,24 @@ impl fmt::Debug for SystemTime {
7064
7165impl Timespec {
7266 pub const fn zero ( ) -> Timespec {
73- Timespec :: new ( 0 , 0 )
67+ unsafe { Self :: new_unchecked ( 0 , 0 ) }
7468 }
7569
76- const fn new ( tv_sec : i64 , tv_nsec : i64 ) -> Timespec {
70+ const fn new_assert ( tv_sec : i64 , tv_nsec : i64 ) -> Self {
7771 assert ! ( tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC as i64 ) ;
7872 // 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 {
7985 Timespec { tv_sec, tv_nsec : unsafe { Nanoseconds ( tv_nsec as u32 ) } }
8086 }
8187
@@ -122,7 +128,7 @@ impl Timespec {
122128 nsec -= NSEC_PER_SEC as u32 ;
123129 secs = secs. checked_add ( 1 ) ?;
124130 }
125- Some ( Timespec :: new ( secs, nsec. into ( ) ) )
131+ Some ( unsafe { Timespec :: new_unchecked ( secs, nsec. into ( ) ) } )
126132 }
127133
128134 pub fn checked_sub_duration ( & self , other : & Duration ) -> Option < Timespec > {
@@ -134,7 +140,7 @@ impl Timespec {
134140 nsec += NSEC_PER_SEC as i32 ;
135141 secs = secs. checked_sub ( 1 ) ?;
136142 }
137- Some ( Timespec :: new ( secs, nsec. into ( ) ) )
143+ Some ( unsafe { Timespec :: new_unchecked ( secs, nsec. into ( ) ) } )
138144 }
139145
140146 #[ allow( dead_code) ]
@@ -170,12 +176,6 @@ impl Timespec {
170176 }
171177}
172178
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-
179179#[ cfg( all(
180180 target_os = "linux" ,
181181 target_env = "gnu" ,
@@ -204,18 +204,6 @@ impl __timespec64 {
204204 }
205205}
206206
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-
219207#[ cfg( any(
220208 all( target_os = "macos" , any( not( target_arch = "aarch64" ) ) ) ,
221209 target_os = "ios" ,
@@ -274,19 +262,7 @@ mod inner {
274262
275263 let mut s = libc:: timeval { tv_sec : 0 , tv_usec : 0 } ;
276264 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 ) }
290266 }
291267 }
292268
@@ -412,13 +388,15 @@ mod inner {
412388 if let Some ( clock_gettime64) = __clock_gettime64. get ( ) {
413389 let mut t = MaybeUninit :: uninit ( ) ;
414390 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 ) ;
416393 }
417394 }
418395
419396 let mut t = MaybeUninit :: uninit ( ) ;
420397 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 )
422400 }
423401 }
424402}
0 commit comments