@@ -13,6 +13,7 @@ use libc;
1313use time:: Duration ;
1414
1515pub use self :: inner:: { Instant , SystemTime , UNIX_EPOCH } ;
16+ use convert:: TryInto ;
1617
1718const NSEC_PER_SEC : u64 = 1_000_000_000 ;
1819
@@ -41,8 +42,12 @@ impl Timespec {
4142 }
4243
4344 fn add_duration ( & self , other : & Duration ) -> Timespec {
44- let secs = ( self . t . tv_sec as i64 ) . checked_add ( other. as_secs ( ) as i64 ) ;
45- let mut secs = secs. expect ( "overflow when adding duration to time" ) ;
45+ let mut secs = other
46+ . as_secs ( )
47+ . try_into ( ) // <- target type would be `libc::time_t`
48+ . ok ( )
49+ . and_then ( |secs| self . t . tv_sec . checked_add ( secs) )
50+ . expect ( "overflow when adding duration to time" ) ;
4651
4752 // Nano calculations can't overflow because nanos are <1B which fit
4853 // in a u32.
@@ -54,16 +59,19 @@ impl Timespec {
5459 }
5560 Timespec {
5661 t : libc:: timespec {
57- tv_sec : secs as libc :: time_t ,
62+ tv_sec : secs,
5863 tv_nsec : nsec as libc:: c_long ,
5964 } ,
6065 }
6166 }
6267
6368 fn sub_duration ( & self , other : & Duration ) -> Timespec {
64- let secs = ( self . t . tv_sec as i64 ) . checked_sub ( other. as_secs ( ) as i64 ) ;
65- let mut secs = secs. expect ( "overflow when subtracting duration \
66- from time") ;
69+ let mut secs = other
70+ . as_secs ( )
71+ . try_into ( ) // <- target type would be `libc::time_t`
72+ . ok ( )
73+ . and_then ( |secs| self . t . tv_sec . checked_sub ( secs) )
74+ . expect ( "overflow when subtracting duration from time" ) ;
6775
6876 // Similar to above, nanos can't overflow.
6977 let mut nsec = self . t . tv_nsec as i32 - other. subsec_nanos ( ) as i32 ;
@@ -74,7 +82,7 @@ impl Timespec {
7482 }
7583 Timespec {
7684 t : libc:: timespec {
77- tv_sec : secs as libc :: time_t ,
85+ tv_sec : secs,
7886 tv_nsec : nsec as libc:: c_long ,
7987 } ,
8088 }
0 commit comments