@@ -13,6 +13,7 @@ use libc;
13
13
use time:: Duration ;
14
14
15
15
pub use self :: inner:: { Instant , SystemTime , UNIX_EPOCH } ;
16
+ use convert:: TryInto ;
16
17
17
18
const NSEC_PER_SEC : u64 = 1_000_000_000 ;
18
19
@@ -41,8 +42,12 @@ impl Timespec {
41
42
}
42
43
43
44
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" ) ;
46
51
47
52
// Nano calculations can't overflow because nanos are <1B which fit
48
53
// in a u32.
@@ -54,16 +59,19 @@ impl Timespec {
54
59
}
55
60
Timespec {
56
61
t : libc:: timespec {
57
- tv_sec : secs as libc :: time_t ,
62
+ tv_sec : secs,
58
63
tv_nsec : nsec as libc:: c_long ,
59
64
} ,
60
65
}
61
66
}
62
67
63
68
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" ) ;
67
75
68
76
// Similar to above, nanos can't overflow.
69
77
let mut nsec = self . t . tv_nsec as i32 - other. subsec_nanos ( ) as i32 ;
@@ -74,7 +82,7 @@ impl Timespec {
74
82
}
75
83
Timespec {
76
84
t : libc:: timespec {
77
- tv_sec : secs as libc :: time_t ,
85
+ tv_sec : secs,
78
86
tv_nsec : nsec as libc:: c_long ,
79
87
} ,
80
88
}
0 commit comments