@@ -79,15 +79,10 @@ fn in_ms(d: Duration) -> u64 {
79
79
80
80
/// Sleep the current task for the specified duration.
81
81
pub fn sleep ( duration : Duration ) {
82
- sleep_ms ( in_ms ( duration) )
83
- }
84
-
85
- /// Sleep the current task for `msecs` milliseconds.
86
- pub fn sleep_ms ( msecs : u64 ) {
87
82
let timer = Timer :: new ( ) ;
88
83
let mut timer = timer. ok ( ) . expect ( "timer::sleep: could not create a Timer" ) ;
89
84
90
- timer. sleep_ms ( msecs )
85
+ timer. sleep ( duration )
91
86
}
92
87
93
88
impl Timer {
@@ -108,14 +103,6 @@ impl Timer {
108
103
self . obj . sleep ( in_ms ( duration) ) ;
109
104
}
110
105
111
- /// Blocks the current task for `msecs` milliseconds.
112
- ///
113
- /// Note that this function will cause any other receivers for this timer to
114
- /// be invalidated (the other end will be closed).
115
- pub fn sleep_ms ( & mut self , msecs : u64 ) {
116
- self . obj . sleep ( msecs) ;
117
- }
118
-
119
106
/// Creates a oneshot receiver which will have a notification sent when
120
107
/// the specified duration has elapsed.
121
108
///
@@ -133,46 +120,6 @@ impl Timer {
133
120
return rx
134
121
}
135
122
136
- /// Creates a oneshot receiver which will have a notification sent when
137
- /// `msecs` milliseconds has elapsed.
138
- ///
139
- /// This does *not* block the current task, but instead returns immediately.
140
- ///
141
- /// Note that this invalidates any previous receiver which has been created
142
- /// by this timer, and that the returned receiver will be invalidated once
143
- /// the timer is destroyed (when it falls out of scope). In particular, if
144
- /// this is called in method-chaining style, the receiver will be
145
- /// invalidated at the end of that statement, and all `recv` calls will
146
- /// fail.
147
- ///
148
- /// # Example
149
- ///
150
- /// ```rust
151
- /// use std::io::Timer;
152
- ///
153
- /// let mut timer = Timer::new().unwrap();
154
- /// let ten_milliseconds = timer.oneshot(10);
155
- ///
156
- /// for _ in range(0u, 100) { /* do work */ }
157
- ///
158
- /// // blocks until 10 ms after the `oneshot` call
159
- /// ten_milliseconds.recv();
160
- /// ```
161
- ///
162
- /// ```rust
163
- /// use std::io::Timer;
164
- ///
165
- /// // Incorrect, method chaining-style:
166
- /// let mut five_ms = Timer::new().unwrap().oneshot(5);
167
- /// // The timer object was destroyed, so this will always fail:
168
- /// // five_ms.recv()
169
- /// ```
170
- pub fn oneshot_ms ( & mut self , msecs : u64 ) -> Receiver < ( ) > {
171
- let ( tx, rx) = channel ( ) ;
172
- self . obj . oneshot ( msecs, box TimerCallback { tx : tx } ) ;
173
- return rx
174
- }
175
-
176
123
/// Creates a receiver which will have a continuous stream of notifications
177
124
/// being sent each time the specified duration has elapsed.
178
125
///
@@ -191,54 +138,6 @@ impl Timer {
191
138
self . obj . period ( in_ms ( duration) , box TimerCallback { tx : tx } ) ;
192
139
return rx
193
140
}
194
-
195
- /// Creates a receiver which will have a continuous stream of notifications
196
- /// being sent every `msecs` milliseconds.
197
- ///
198
- /// This does *not* block the current task, but instead returns
199
- /// immediately. The first notification will not be received immediately,
200
- /// but rather after `msec` milliseconds have passed.
201
- ///
202
- /// Note that this invalidates any previous receiver which has been created
203
- /// by this timer, and that the returned receiver will be invalidated once
204
- /// the timer is destroyed (when it falls out of scope). In particular, if
205
- /// this is called in method-chaining style, the receiver will be
206
- /// invalidated at the end of that statement, and all `recv` calls will
207
- /// fail.
208
- ///
209
- /// # Example
210
- ///
211
- /// ```rust
212
- /// use std::io::Timer;
213
- ///
214
- /// let mut timer = Timer::new().unwrap();
215
- /// let ten_milliseconds = timer.periodic(10);
216
- ///
217
- /// for _ in range(0u, 100) { /* do work */ }
218
- ///
219
- /// // blocks until 10 ms after the `periodic` call
220
- /// ten_milliseconds.recv();
221
- ///
222
- /// for _ in range(0u, 100) { /* do work */ }
223
- ///
224
- /// // blocks until 20 ms after the `periodic` call (*not* 10ms after the
225
- /// // previous `recv`)
226
- /// ten_milliseconds.recv();
227
- /// ```
228
- ///
229
- /// ```rust
230
- /// use std::io::Timer;
231
- ///
232
- /// // Incorrect, method chaining-style.
233
- /// let mut five_ms = Timer::new().unwrap().periodic(5);
234
- /// // The timer object was destroyed, so this will always fail:
235
- /// // five_ms.recv()
236
- /// ```
237
- pub fn periodic_ms ( & mut self , msecs : u64 ) -> Receiver < ( ) > {
238
- let ( tx, rx) = channel ( ) ;
239
- self . obj . period ( msecs, box TimerCallback { tx : tx } ) ;
240
- return rx
241
- }
242
141
}
243
142
244
143
impl Callback for TimerCallback {
@@ -249,101 +148,103 @@ impl Callback for TimerCallback {
249
148
250
149
#[ cfg( test) ]
251
150
mod test {
252
- iotest ! ( fn test_io_timer_sleep_ms_simple( ) {
151
+ use time:: Duration ;
152
+
153
+ iotest ! ( fn test_io_timer_sleep_simple( ) {
253
154
let mut timer = Timer :: new( ) . unwrap( ) ;
254
- timer. sleep_ms ( 1 ) ;
155
+ timer. sleep ( Duration :: milliseconds ( 1 ) ) ;
255
156
} )
256
157
257
- iotest ! ( fn test_io_timer_sleep_oneshot_ms ( ) {
158
+ iotest ! ( fn test_io_timer_sleep_oneshot ( ) {
258
159
let mut timer = Timer :: new( ) . unwrap( ) ;
259
- timer. oneshot_ms ( 1 ) . recv( ) ;
160
+ timer. oneshot ( Duration :: milliseconds ( 1 ) ) . recv( ) ;
260
161
} )
261
162
262
- iotest ! ( fn test_io_timer_sleep_oneshot_ms_forget ( ) {
163
+ iotest ! ( fn test_io_timer_sleep_oneshot_forget ( ) {
263
164
let mut timer = Timer :: new( ) . unwrap( ) ;
264
- timer. oneshot_ms ( 100000000000 ) ;
165
+ timer. oneshot ( Duration :: milliseconds ( 100000000000 ) ) ;
265
166
} )
266
167
267
- iotest ! ( fn oneshot_ms_twice ( ) {
168
+ iotest ! ( fn oneshot_twice ( ) {
268
169
let mut timer = Timer :: new( ) . unwrap( ) ;
269
- let rx1 = timer. oneshot_ms ( 10000 ) ;
270
- let rx = timer. oneshot_ms ( 1 ) ;
170
+ let rx1 = timer. oneshot ( Duration :: milliseconds ( 10000 ) ) ;
171
+ let rx = timer. oneshot ( 1 ) ;
271
172
rx. recv( ) ;
272
173
assert_eq!( rx1. recv_opt( ) , Err ( ( ) ) ) ;
273
174
} )
274
175
275
- iotest ! ( fn test_io_timer_oneshot_ms_then_sleep ( ) {
176
+ iotest ! ( fn test_io_timer_oneshot_then_sleep ( ) {
276
177
let mut timer = Timer :: new( ) . unwrap( ) ;
277
- let rx = timer. oneshot_ms ( 100000000000 ) ;
278
- timer. sleep_ms ( 1 ) ; // this should invalidate rx
178
+ let rx = timer. oneshot ( Duration :: milliseconds ( 100000000000 ) ) ;
179
+ timer. sleep ( Duration :: milliseconds ( 1 ) ) ; // this should invalidate rx
279
180
280
181
assert_eq!( rx. recv_opt( ) , Err ( ( ) ) ) ;
281
182
} )
282
183
283
- iotest ! ( fn test_io_timer_sleep_periodic_ms ( ) {
184
+ iotest ! ( fn test_io_timer_sleep_periodic ( ) {
284
185
let mut timer = Timer :: new( ) . unwrap( ) ;
285
- let rx = timer. periodic_ms ( 1 ) ;
186
+ let rx = timer. periodic ( Duration :: milliseconds ( 1 ) ) ;
286
187
rx. recv( ) ;
287
188
rx. recv( ) ;
288
189
rx. recv( ) ;
289
190
} )
290
191
291
- iotest ! ( fn test_io_timer_sleep_periodic_ms_forget ( ) {
192
+ iotest ! ( fn test_io_timer_sleep_periodic_forget ( ) {
292
193
let mut timer = Timer :: new( ) . unwrap( ) ;
293
- timer. periodic_ms ( 100000000000 ) ;
194
+ timer. periodic ( Duration :: milliseconds ( 100000000000 ) ) ;
294
195
} )
295
196
296
- iotest ! ( fn test_io_timer_sleep_ms_standalone ( ) {
297
- sleep_ms ( 1 )
197
+ iotest ! ( fn test_io_timer_sleep_standalone ( ) {
198
+ sleep ( Duration :: milliseconds ( 1 ) )
298
199
} )
299
200
300
- iotest ! ( fn oneshot_ms ( ) {
201
+ iotest ! ( fn oneshot ( ) {
301
202
let mut timer = Timer :: new( ) . unwrap( ) ;
302
203
303
- let rx = timer. oneshot_ms ( 1 ) ;
204
+ let rx = timer. oneshot ( Duration :: milliseconds ( 1 ) ) ;
304
205
rx. recv( ) ;
305
206
assert!( rx. recv_opt( ) . is_err( ) ) ;
306
207
307
- let rx = timer. oneshot_ms ( 1 ) ;
208
+ let rx = timer. oneshot ( Duration :: milliseconds ( 1 ) ) ;
308
209
rx. recv( ) ;
309
210
assert!( rx. recv_opt( ) . is_err( ) ) ;
310
211
} )
311
212
312
213
iotest ! ( fn override( ) {
313
214
let mut timer = Timer :: new( ) . unwrap( ) ;
314
- let orx = timer. oneshot_ms ( 100 ) ;
315
- let prx = timer. periodic_ms ( 100 ) ;
316
- timer. sleep_ms ( 1 ) ;
215
+ let orx = timer. oneshot ( Duration :: milliseconds ( 100 ) ) ;
216
+ let prx = timer. periodic ( Duration :: milliseconds ( 100 ) ) ;
217
+ timer. sleep ( Duration :: milliseconds ( 1 ) ) ;
317
218
assert_eq!( orx. recv_opt( ) , Err ( ( ) ) ) ;
318
219
assert_eq!( prx. recv_opt( ) , Err ( ( ) ) ) ;
319
- timer. oneshot_ms ( 1 ) . recv( ) ;
220
+ timer. oneshot ( Duration :: milliseconds ( 1 ) ) . recv( ) ;
320
221
} )
321
222
322
- iotest ! ( fn period_ms ( ) {
223
+ iotest ! ( fn period ( ) {
323
224
let mut timer = Timer :: new( ) . unwrap( ) ;
324
- let rx = timer. periodic_ms ( 1 ) ;
225
+ let rx = timer. periodic ( Duration :: milliseconds ( 1 ) ) ;
325
226
rx. recv( ) ;
326
227
rx. recv( ) ;
327
- let rx2 = timer. periodic_ms ( 1 ) ;
228
+ let rx2 = timer. periodic ( Durtion :: milliseconds ( 1 ) ) ;
328
229
rx2. recv( ) ;
329
230
rx2. recv( ) ;
330
231
} )
331
232
332
- iotest ! ( fn sleep_ms ( ) {
233
+ iotest ! ( fn sleep ( ) {
333
234
let mut timer = Timer :: new( ) . unwrap( ) ;
334
- timer. sleep_ms ( 1 ) ;
335
- timer. sleep_ms ( 1 ) ;
235
+ timer. sleep ( Duration :: milliseconds ( 1 ) ) ;
236
+ timer. sleep ( Duration :: milliseconds ( 1 ) ) ;
336
237
} )
337
238
338
- iotest ! ( fn oneshot_ms_fail ( ) {
239
+ iotest ! ( fn oneshot_fail ( ) {
339
240
let mut timer = Timer :: new( ) . unwrap( ) ;
340
- let _rx = timer. oneshot_ms ( 1 ) ;
241
+ let _rx = timer. oneshot ( Duration :: milliseconds ( 1 ) ) ;
341
242
fail!( ) ;
342
243
} #[ should_fail] )
343
244
344
- iotest ! ( fn period_ms_fail ( ) {
245
+ iotest ! ( fn period_fail ( ) {
345
246
let mut timer = Timer :: new( ) . unwrap( ) ;
346
- let _rx = timer. periodic_ms ( 1 ) ;
247
+ let _rx = timer. periodic ( Duration :: milliseconds ( 1 ) ) ;
347
248
fail!( ) ;
348
249
} #[ should_fail] )
349
250
@@ -355,7 +256,7 @@ mod test {
355
256
iotest ! ( fn closing_channel_during_drop_doesnt_kill_everything( ) {
356
257
// see issue #10375
357
258
let mut timer = Timer :: new( ) . unwrap( ) ;
358
- let timer_rx = timer. periodic_ms ( 1000 ) ;
259
+ let timer_rx = timer. periodic ( Duration :: milliseconds ( 1000 ) ) ;
359
260
360
261
spawn( proc( ) {
361
262
let _ = timer_rx. recv_opt( ) ;
@@ -368,82 +269,58 @@ mod test {
368
269
iotest ! ( fn reset_doesnt_switch_tasks( ) {
369
270
// similar test to the one above.
370
271
let mut timer = Timer :: new( ) . unwrap( ) ;
371
- let timer_rx = timer. periodic_ms ( 1000 ) ;
272
+ let timer_rx = timer. periodic ( Duration :: milliseconds ( 1000 ) ) ;
372
273
373
274
spawn( proc( ) {
374
275
let _ = timer_rx. recv_opt( ) ;
375
276
} ) ;
376
277
377
- timer. oneshot_ms ( 1 ) ;
278
+ timer. oneshot ( Duration :: milliseconds ( 1 ) ) ;
378
279
} )
379
280
380
281
iotest ! ( fn reset_doesnt_switch_tasks2( ) {
381
282
// similar test to the one above.
382
283
let mut timer = Timer :: new( ) . unwrap( ) ;
383
- let timer_rx = timer. periodic_ms ( 1000 ) ;
284
+ let timer_rx = timer. periodic ( Duration :: milliseconds ( 1000 ) ) ;
384
285
385
286
spawn( proc( ) {
386
287
let _ = timer_rx. recv_opt( ) ;
387
288
} ) ;
388
289
389
- timer. sleep_ms ( 1 ) ;
290
+ timer. sleep ( Duration :: milliseconds ( 1 ) ) ;
390
291
} )
391
292
392
293
iotest ! ( fn sender_goes_away_oneshot( ) {
393
294
let rx = {
394
295
let mut timer = Timer :: new( ) . unwrap( ) ;
395
- timer. oneshot_ms ( 1000 )
296
+ timer. oneshot ( Duration :: milliseconds ( 1000 ) )
396
297
} ;
397
298
assert_eq!( rx. recv_opt( ) , Err ( ( ) ) ) ;
398
299
} )
399
300
400
301
iotest ! ( fn sender_goes_away_period( ) {
401
302
let rx = {
402
303
let mut timer = Timer :: new( ) . unwrap( ) ;
403
- timer. periodic_ms ( 1000 )
304
+ timer. periodic ( Duration :: milliseconds ( 1000 ) )
404
305
} ;
405
306
assert_eq!( rx. recv_opt( ) , Err ( ( ) ) ) ;
406
307
} )
407
308
408
309
iotest ! ( fn receiver_goes_away_oneshot( ) {
409
310
let mut timer1 = Timer :: new( ) . unwrap( ) ;
410
- timer1. oneshot_ms ( 1 ) ;
311
+ timer1. oneshot ( Duration :: milliseconds ( 1 ) ) ;
411
312
let mut timer2 = Timer :: new( ) . unwrap( ) ;
412
313
// while sleeping, the previous timer should fire and not have its
413
314
// callback do something terrible.
414
- timer2. sleep_ms ( 2 ) ;
315
+ timer2. sleep ( Duration :: milliseconds ( 2 ) ) ;
415
316
} )
416
317
417
318
iotest ! ( fn receiver_goes_away_period( ) {
418
319
let mut timer1 = Timer :: new( ) . unwrap( ) ;
419
- timer1. periodic_ms ( 1 ) ;
320
+ timer1. periodic ( Duration :: milliseconds ( 1 ) ) ;
420
321
let mut timer2 = Timer :: new( ) . unwrap( ) ;
421
322
// while sleeping, the previous timer should fire and not have its
422
323
// callback do something terrible.
423
- timer2. sleep_ms( 2 ) ;
424
- } )
425
-
426
-
427
- iotest ! ( fn test_io_timer_sleep_duration_simple( ) {
428
- use time:: Duration ;
429
- let mut timer = Timer :: new( ) . unwrap( ) ;
430
- timer. sleep( Duration :: seconds( 1 ) ) ;
431
- } )
432
-
433
- iotest ! ( fn test_io_timer_sleep_oneshot_duration( ) {
434
- use time:: Duration ;
435
- let mut timer = Timer :: new( ) . unwrap( ) ;
436
- timer. oneshot( Duration :: seconds( 1 ) ) . recv( ) ;
324
+ timer2. sleep( Duration :: milliseconds( 2 ) ) ;
437
325
} )
438
-
439
- iotest ! ( fn test_io_timer_sleep_periodic_duration( ) {
440
- use time:: Duration ;
441
- let mut timer = Timer :: new( ) . unwrap( ) ;
442
- let rx = timer. periodic( Duration :: seconds( 1 ) ) ;
443
- rx. recv( ) ;
444
- rx. recv( ) ;
445
- rx. recv( ) ;
446
- } )
447
-
448
-
449
326
}
0 commit comments