@@ -31,6 +31,7 @@ extern crate libc;
31
31
use std:: io:: BufReader ;
32
32
use std:: num;
33
33
use std:: string:: String ;
34
+ use std:: time:: Duration ;
34
35
35
36
static NSEC_PER_SEC : i32 = 1_000_000_000_i32 ;
36
37
@@ -90,10 +91,15 @@ impl Timespec {
90
91
}
91
92
}
92
93
93
- impl Add < Timespec , Timespec > for Timespec {
94
- fn add ( & self , other : & Timespec ) -> Timespec {
95
- let mut sec = self . sec + other. sec ;
96
- let mut nsec = self . nsec + other. nsec ;
94
+ impl Add < Duration , Timespec > for Timespec {
95
+ fn add ( & self , other : & Duration ) -> Timespec {
96
+ let d_sec = other. num_seconds ( ) ;
97
+ // It is safe to unwrap the nanoseconds, because there cannot be
98
+ // more than one second left, which fits in i64 and in i32.
99
+ let d_nsec = ( other - Duration :: seconds ( d_sec) )
100
+ . num_nanoseconds ( ) . unwrap ( ) as i32 ;
101
+ let mut sec = self . sec + d_sec;
102
+ let mut nsec = self . nsec + d_nsec;
97
103
if nsec >= NSEC_PER_SEC {
98
104
nsec -= NSEC_PER_SEC ;
99
105
sec += 1 ;
@@ -102,15 +108,11 @@ impl Add<Timespec, Timespec> for Timespec {
102
108
}
103
109
}
104
110
105
- impl Sub < Timespec , Timespec > for Timespec {
106
- fn sub ( & self , other : & Timespec ) -> Timespec {
107
- let mut sec = self . sec - other. sec ;
108
- let mut nsec = self . nsec - other. nsec ;
109
- if nsec < 0 {
110
- nsec += NSEC_PER_SEC ;
111
- sec -= 1 ;
112
- }
113
- Timespec :: new ( sec, nsec)
111
+ impl Sub < Timespec , Duration > for Timespec {
112
+ fn sub ( & self , other : & Timespec ) -> Duration {
113
+ let sec = self . sec - other. sec ;
114
+ let nsec = self . nsec - other. nsec ;
115
+ Duration :: seconds ( sec) + Duration :: nanoseconds ( nsec as i64 )
114
116
}
115
117
}
116
118
@@ -1103,6 +1105,7 @@ mod tests {
1103
1105
1104
1106
use std:: f64;
1105
1107
use std:: result:: { Err , Ok } ;
1108
+ use std:: time:: Duration ;
1106
1109
use self :: test:: Bencher ;
1107
1110
1108
1111
#[ cfg( windows) ]
@@ -1514,19 +1517,19 @@ mod tests {
1514
1517
1515
1518
fn test_timespec_add ( ) {
1516
1519
let a = Timespec :: new ( 1 , 2 ) ;
1517
- let b = Timespec :: new ( 2 , 3 ) ;
1520
+ let b = Duration :: seconds ( 2 ) + Duration :: nanoseconds ( 3 ) ;
1518
1521
let c = a + b;
1519
1522
assert_eq ! ( c. sec, 3 ) ;
1520
1523
assert_eq ! ( c. nsec, 5 ) ;
1521
1524
1522
1525
let p = Timespec :: new ( 1 , super :: NSEC_PER_SEC - 2 ) ;
1523
- let q = Timespec :: new ( 2 , 2 ) ;
1526
+ let q = Duration :: seconds ( 2 ) + Duration :: nanoseconds ( 2 ) ;
1524
1527
let r = p + q;
1525
1528
assert_eq ! ( r. sec, 4 ) ;
1526
1529
assert_eq ! ( r. nsec, 0 ) ;
1527
1530
1528
1531
let u = Timespec :: new ( 1 , super :: NSEC_PER_SEC - 2 ) ;
1529
- let v = Timespec :: new ( 2 , 3 ) ;
1532
+ let v = Duration :: seconds ( 2 ) + Duration :: nanoseconds ( 3 ) ;
1530
1533
let w = u + v;
1531
1534
assert_eq ! ( w. sec, 4 ) ;
1532
1535
assert_eq ! ( w. nsec, 1 ) ;
@@ -1536,20 +1539,17 @@ mod tests {
1536
1539
let a = Timespec :: new ( 2 , 3 ) ;
1537
1540
let b = Timespec :: new ( 1 , 2 ) ;
1538
1541
let c = a - b;
1539
- assert_eq ! ( c. sec, 1 ) ;
1540
- assert_eq ! ( c. nsec, 1 ) ;
1542
+ assert_eq ! ( c. num_nanoseconds( ) , Some ( super :: NSEC_PER_SEC as i64 + 1 ) ) ;
1541
1543
1542
1544
let p = Timespec :: new ( 2 , 0 ) ;
1543
1545
let q = Timespec :: new ( 1 , 2 ) ;
1544
1546
let r = p - q;
1545
- assert_eq ! ( r. sec, 0 ) ;
1546
- assert_eq ! ( r. nsec, super :: NSEC_PER_SEC - 2 ) ;
1547
+ assert_eq ! ( r. num_nanoseconds( ) , Some ( super :: NSEC_PER_SEC as i64 - 2 ) ) ;
1547
1548
1548
1549
let u = Timespec :: new ( 1 , 2 ) ;
1549
1550
let v = Timespec :: new ( 2 , 3 ) ;
1550
1551
let w = u - v;
1551
- assert_eq ! ( w. sec, -2 ) ;
1552
- assert_eq ! ( w. nsec, super :: NSEC_PER_SEC - 1 ) ;
1552
+ assert_eq ! ( w. num_nanoseconds( ) , Some ( -super :: NSEC_PER_SEC as i64 - 1 ) ) ;
1553
1553
}
1554
1554
1555
1555
#[ test]
0 commit comments