@@ -22,7 +22,6 @@ use time::Duration;
22
22
use io:: { IoResult , IoError } ;
23
23
use kinds:: Send ;
24
24
use boxed:: Box ;
25
- use num:: { CheckedMul , CheckedAdd } ;
26
25
use rt:: rtio:: { IoFactory , LocalIo , RtioTimer , Callback } ;
27
26
28
27
/// A synchronous timer object
@@ -71,31 +70,16 @@ pub struct Timer {
71
70
72
71
struct TimerCallback { tx : Sender < ( ) > }
73
72
74
- #[ allow( missing_doc) ]
75
- trait DurationExtension {
76
- fn in_ms ( & self ) -> u64 ;
77
- }
78
-
79
- impl DurationExtension for Duration {
80
- fn in_ms ( & self ) -> u64 {
81
- if self . ndays ( ) < 0 { fail ! ( "negative duration" ) }
82
- let nanos = self . nnanoseconds ( ) as u64 ;
83
- let secs = self . nseconds ( ) as u64 ;
84
- let days = self . ndays ( ) as u64 ;
85
- let nanos_in_ms = nanos / 1000 ;
86
- let secs_in_ms = secs. checked_mul ( & 1000 ) . expect ( "overflow" ) ;
87
- let ms_per_day = 24 * 60 * 60 * 1000 ; // hours/day * min/hour * sec/min * ms/sec
88
- let days_in_ms = days. checked_mul ( & ms_per_day) . expect ( "overflow" ) ;
89
- let result = nanos_in_ms;
90
- let result = result. checked_add ( & secs_in_ms) . expect ( "overflow" ) ;
91
- let result = result. checked_add ( & ( days_in_ms as u64 ) ) . expect ( "overflow" ) ;
92
- return result;
93
- }
73
+ fn in_ms ( d : Duration ) -> u64 {
74
+ // FIXME: Do we really want to fail on negative duration?
75
+ let ms = d. num_milliseconds ( ) ;
76
+ if ms < 0 { fail ! ( "negative duration" ) }
77
+ return ms as u64 ;
94
78
}
95
79
96
80
/// Sleep the current task for the specified duration.
97
81
pub fn sleep ( duration : Duration ) {
98
- sleep_ms ( duration . in_ms ( ) )
82
+ sleep_ms ( in_ms ( duration ) )
99
83
}
100
84
101
85
/// Sleep the current task for `msecs` milliseconds.
@@ -121,7 +105,7 @@ impl Timer {
121
105
/// Note that this function will cause any other receivers for this timer to
122
106
/// be invalidated (the other end will be closed).
123
107
pub fn sleep ( & mut self , duration : Duration ) {
124
- self . obj . sleep ( duration . in_ms ( ) ) ;
108
+ self . obj . sleep ( in_ms ( duration ) ) ;
125
109
}
126
110
127
111
/// Blocks the current task for `msecs` milliseconds.
@@ -145,7 +129,7 @@ impl Timer {
145
129
/// fail.
146
130
pub fn oneshot ( & mut self , duration : Duration ) -> Receiver < ( ) > {
147
131
let ( tx, rx) = channel ( ) ;
148
- self . obj . oneshot ( duration . in_ms ( ) , box TimerCallback { tx : tx } ) ;
132
+ self . obj . oneshot ( in_ms ( duration ) , box TimerCallback { tx : tx } ) ;
149
133
return rx
150
134
}
151
135
@@ -204,7 +188,7 @@ impl Timer {
204
188
/// fail.
205
189
pub fn periodic ( & mut self , duration : Duration ) -> Receiver < ( ) > {
206
190
let ( tx, rx) = channel ( ) ;
207
- self . obj . period ( duration . in_ms ( ) , box TimerCallback { tx : tx } ) ;
191
+ self . obj . period ( in_ms ( duration ) , box TimerCallback { tx : tx } ) ;
208
192
return rx
209
193
}
210
194
0 commit comments