From 1666dabcbc2628fb4a5e4b9c43a13fd1179112b2 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 28 Jul 2014 16:04:57 -0700 Subject: [PATCH] std: Remove ms-taking methods from timers --- src/libstd/io/timer.rs | 221 +++++++++-------------------------------- 1 file changed, 49 insertions(+), 172 deletions(-) diff --git a/src/libstd/io/timer.rs b/src/libstd/io/timer.rs index 41a0271fff2..d9d6bb0f1b3 100644 --- a/src/libstd/io/timer.rs +++ b/src/libstd/io/timer.rs @@ -79,15 +79,10 @@ fn in_ms(d: Duration) -> u64 { /// Sleep the current task for the specified duration. pub fn sleep(duration: Duration) { - sleep_ms(in_ms(duration)) -} - -/// Sleep the current task for `msecs` milliseconds. -pub fn sleep_ms(msecs: u64) { let timer = Timer::new(); let mut timer = timer.ok().expect("timer::sleep: could not create a Timer"); - timer.sleep_ms(msecs) + timer.sleep(duration) } impl Timer { @@ -108,14 +103,6 @@ impl Timer { self.obj.sleep(in_ms(duration)); } - /// Blocks the current task for `msecs` milliseconds. - /// - /// Note that this function will cause any other receivers for this timer to - /// be invalidated (the other end will be closed). - pub fn sleep_ms(&mut self, msecs: u64) { - self.obj.sleep(msecs); - } - /// Creates a oneshot receiver which will have a notification sent when /// the specified duration has elapsed. /// @@ -133,46 +120,6 @@ impl Timer { return rx } - /// Creates a oneshot receiver which will have a notification sent when - /// `msecs` milliseconds has elapsed. - /// - /// This does *not* block the current task, but instead returns immediately. - /// - /// Note that this invalidates any previous receiver which has been created - /// by this timer, and that the returned receiver will be invalidated once - /// the timer is destroyed (when it falls out of scope). In particular, if - /// this is called in method-chaining style, the receiver will be - /// invalidated at the end of that statement, and all `recv` calls will - /// fail. - /// - /// # Example - /// - /// ```rust - /// use std::io::Timer; - /// - /// let mut timer = Timer::new().unwrap(); - /// let ten_milliseconds = timer.oneshot(10); - /// - /// for _ in range(0u, 100) { /* do work */ } - /// - /// // blocks until 10 ms after the `oneshot` call - /// ten_milliseconds.recv(); - /// ``` - /// - /// ```rust - /// use std::io::Timer; - /// - /// // Incorrect, method chaining-style: - /// let mut five_ms = Timer::new().unwrap().oneshot(5); - /// // The timer object was destroyed, so this will always fail: - /// // five_ms.recv() - /// ``` - pub fn oneshot_ms(&mut self, msecs: u64) -> Receiver<()> { - let (tx, rx) = channel(); - self.obj.oneshot(msecs, box TimerCallback { tx: tx }); - return rx - } - /// Creates a receiver which will have a continuous stream of notifications /// being sent each time the specified duration has elapsed. /// @@ -191,54 +138,6 @@ impl Timer { self.obj.period(in_ms(duration), box TimerCallback { tx: tx }); return rx } - - /// Creates a receiver which will have a continuous stream of notifications - /// being sent every `msecs` milliseconds. - /// - /// This does *not* block the current task, but instead returns - /// immediately. The first notification will not be received immediately, - /// but rather after `msec` milliseconds have passed. - /// - /// Note that this invalidates any previous receiver which has been created - /// by this timer, and that the returned receiver will be invalidated once - /// the timer is destroyed (when it falls out of scope). In particular, if - /// this is called in method-chaining style, the receiver will be - /// invalidated at the end of that statement, and all `recv` calls will - /// fail. - /// - /// # Example - /// - /// ```rust - /// use std::io::Timer; - /// - /// let mut timer = Timer::new().unwrap(); - /// let ten_milliseconds = timer.periodic(10); - /// - /// for _ in range(0u, 100) { /* do work */ } - /// - /// // blocks until 10 ms after the `periodic` call - /// ten_milliseconds.recv(); - /// - /// for _ in range(0u, 100) { /* do work */ } - /// - /// // blocks until 20 ms after the `periodic` call (*not* 10ms after the - /// // previous `recv`) - /// ten_milliseconds.recv(); - /// ``` - /// - /// ```rust - /// use std::io::Timer; - /// - /// // Incorrect, method chaining-style. - /// let mut five_ms = Timer::new().unwrap().periodic(5); - /// // The timer object was destroyed, so this will always fail: - /// // five_ms.recv() - /// ``` - pub fn periodic_ms(&mut self, msecs: u64) -> Receiver<()> { - let (tx, rx) = channel(); - self.obj.period(msecs, box TimerCallback { tx: tx }); - return rx - } } impl Callback for TimerCallback { @@ -249,101 +148,103 @@ impl Callback for TimerCallback { #[cfg(test)] mod test { - iotest!(fn test_io_timer_sleep_ms_simple() { + use time::Duration; + + iotest!(fn test_io_timer_sleep_simple() { let mut timer = Timer::new().unwrap(); - timer.sleep_ms(1); + timer.sleep(Duration::milliseconds(1)); }) - iotest!(fn test_io_timer_sleep_oneshot_ms() { + iotest!(fn test_io_timer_sleep_oneshot() { let mut timer = Timer::new().unwrap(); - timer.oneshot_ms(1).recv(); + timer.oneshot(Duration::milliseconds(1)).recv(); }) - iotest!(fn test_io_timer_sleep_oneshot_ms_forget() { + iotest!(fn test_io_timer_sleep_oneshot_forget() { let mut timer = Timer::new().unwrap(); - timer.oneshot_ms(100000000000); + timer.oneshot(Duration::milliseconds(100000000000)); }) - iotest!(fn oneshot_ms_twice() { + iotest!(fn oneshot_twice() { let mut timer = Timer::new().unwrap(); - let rx1 = timer.oneshot_ms(10000); - let rx = timer.oneshot_ms(1); + let rx1 = timer.oneshot(Duration::milliseconds(10000)); + let rx = timer.oneshot(1); rx.recv(); assert_eq!(rx1.recv_opt(), Err(())); }) - iotest!(fn test_io_timer_oneshot_ms_then_sleep() { + iotest!(fn test_io_timer_oneshot_then_sleep() { let mut timer = Timer::new().unwrap(); - let rx = timer.oneshot_ms(100000000000); - timer.sleep_ms(1); // this should invalidate rx + let rx = timer.oneshot(Duration::milliseconds(100000000000)); + timer.sleep(Duration::milliseconds(1)); // this should invalidate rx assert_eq!(rx.recv_opt(), Err(())); }) - iotest!(fn test_io_timer_sleep_periodic_ms() { + iotest!(fn test_io_timer_sleep_periodic() { let mut timer = Timer::new().unwrap(); - let rx = timer.periodic_ms(1); + let rx = timer.periodic(Duration::milliseconds(1)); rx.recv(); rx.recv(); rx.recv(); }) - iotest!(fn test_io_timer_sleep_periodic_ms_forget() { + iotest!(fn test_io_timer_sleep_periodic_forget() { let mut timer = Timer::new().unwrap(); - timer.periodic_ms(100000000000); + timer.periodic(Duration::milliseconds(100000000000)); }) - iotest!(fn test_io_timer_sleep_ms_standalone() { - sleep_ms(1) + iotest!(fn test_io_timer_sleep_standalone() { + sleep(Duration::milliseconds(1)) }) - iotest!(fn oneshot_ms() { + iotest!(fn oneshot() { let mut timer = Timer::new().unwrap(); - let rx = timer.oneshot_ms(1); + let rx = timer.oneshot(Duration::milliseconds(1)); rx.recv(); assert!(rx.recv_opt().is_err()); - let rx = timer.oneshot_ms(1); + let rx = timer.oneshot(Duration::milliseconds(1)); rx.recv(); assert!(rx.recv_opt().is_err()); }) iotest!(fn override() { let mut timer = Timer::new().unwrap(); - let orx = timer.oneshot_ms(100); - let prx = timer.periodic_ms(100); - timer.sleep_ms(1); + let orx = timer.oneshot(Duration::milliseconds(100)); + let prx = timer.periodic(Duration::milliseconds(100)); + timer.sleep(Duration::milliseconds(1)); assert_eq!(orx.recv_opt(), Err(())); assert_eq!(prx.recv_opt(), Err(())); - timer.oneshot_ms(1).recv(); + timer.oneshot(Duration::milliseconds(1)).recv(); }) - iotest!(fn period_ms() { + iotest!(fn period() { let mut timer = Timer::new().unwrap(); - let rx = timer.periodic_ms(1); + let rx = timer.periodic(Duration::milliseconds(1)); rx.recv(); rx.recv(); - let rx2 = timer.periodic_ms(1); + let rx2 = timer.periodic(Durtion::milliseconds(1)); rx2.recv(); rx2.recv(); }) - iotest!(fn sleep_ms() { + iotest!(fn sleep() { let mut timer = Timer::new().unwrap(); - timer.sleep_ms(1); - timer.sleep_ms(1); + timer.sleep(Duration::milliseconds(1)); + timer.sleep(Duration::milliseconds(1)); }) - iotest!(fn oneshot_ms_fail() { + iotest!(fn oneshot_fail() { let mut timer = Timer::new().unwrap(); - let _rx = timer.oneshot_ms(1); + let _rx = timer.oneshot(Duration::milliseconds(1)); fail!(); } #[should_fail]) - iotest!(fn period_ms_fail() { + iotest!(fn period_fail() { let mut timer = Timer::new().unwrap(); - let _rx = timer.periodic_ms(1); + let _rx = timer.periodic(Duration::milliseconds(1)); fail!(); } #[should_fail]) @@ -355,7 +256,7 @@ mod test { iotest!(fn closing_channel_during_drop_doesnt_kill_everything() { // see issue #10375 let mut timer = Timer::new().unwrap(); - let timer_rx = timer.periodic_ms(1000); + let timer_rx = timer.periodic(Duration::milliseconds(1000)); spawn(proc() { let _ = timer_rx.recv_opt(); @@ -368,31 +269,31 @@ mod test { iotest!(fn reset_doesnt_switch_tasks() { // similar test to the one above. let mut timer = Timer::new().unwrap(); - let timer_rx = timer.periodic_ms(1000); + let timer_rx = timer.periodic(Duration::milliseconds(1000)); spawn(proc() { let _ = timer_rx.recv_opt(); }); - timer.oneshot_ms(1); + timer.oneshot(Duration::milliseconds(1)); }) iotest!(fn reset_doesnt_switch_tasks2() { // similar test to the one above. let mut timer = Timer::new().unwrap(); - let timer_rx = timer.periodic_ms(1000); + let timer_rx = timer.periodic(Duration::milliseconds(1000)); spawn(proc() { let _ = timer_rx.recv_opt(); }); - timer.sleep_ms(1); + timer.sleep(Duration::milliseconds(1)); }) iotest!(fn sender_goes_away_oneshot() { let rx = { let mut timer = Timer::new().unwrap(); - timer.oneshot_ms(1000) + timer.oneshot(Duration::milliseconds(1000)) }; assert_eq!(rx.recv_opt(), Err(())); }) @@ -400,50 +301,26 @@ mod test { iotest!(fn sender_goes_away_period() { let rx = { let mut timer = Timer::new().unwrap(); - timer.periodic_ms(1000) + timer.periodic(Duration::milliseconds(1000)) }; assert_eq!(rx.recv_opt(), Err(())); }) iotest!(fn receiver_goes_away_oneshot() { let mut timer1 = Timer::new().unwrap(); - timer1.oneshot_ms(1); + timer1.oneshot(Duration::milliseconds(1)); let mut timer2 = Timer::new().unwrap(); // while sleeping, the previous timer should fire and not have its // callback do something terrible. - timer2.sleep_ms(2); + timer2.sleep(Duration::milliseconds(2)); }) iotest!(fn receiver_goes_away_period() { let mut timer1 = Timer::new().unwrap(); - timer1.periodic_ms(1); + timer1.periodic(Duration::milliseconds(1)); let mut timer2 = Timer::new().unwrap(); // while sleeping, the previous timer should fire and not have its // callback do something terrible. - timer2.sleep_ms(2); + timer2.sleep(Duration::milliseconds(2)); }) - - - iotest!(fn test_io_timer_sleep_duration_simple() { - use time::Duration; - let mut timer = Timer::new().unwrap(); - timer.sleep(Duration::seconds(1)); - }) - - iotest!(fn test_io_timer_sleep_oneshot_duration() { - use time::Duration; - let mut timer = Timer::new().unwrap(); - timer.oneshot(Duration::seconds(1)).recv(); - }) - - iotest!(fn test_io_timer_sleep_periodic_duration() { - use time::Duration; - let mut timer = Timer::new().unwrap(); - let rx = timer.periodic(Duration::seconds(1)); - rx.recv(); - rx.recv(); - rx.recv(); - }) - - }