about summary refs log tree commit diff
path: root/src/libstd/io
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2014-07-28 15:54:05 -0700
committerBrian Anderson <banderson@mozilla.com>2014-08-13 11:31:47 -0700
commit6cb2093f7496a2539e343677b6f7f5dd2fa5f091 (patch)
tree4f559bb3d4a02f63b4f2365543bbfd0a3203791b /src/libstd/io
parent18f75a9197a5b535f9804901bfefbaffe373d689 (diff)
downloadrust-6cb2093f7496a2539e343677b6f7f5dd2fa5f091.tar.gz
rust-6cb2093f7496a2539e343677b6f7f5dd2fa5f091.zip
std: Update Duration from upstream
From rust-chrono 4f34003e03e259bd5cbda0cb4d35325861307cc6
Diffstat (limited to 'src/libstd/io')
-rw-r--r--src/libstd/io/timer.rs34
1 files changed, 9 insertions, 25 deletions
diff --git a/src/libstd/io/timer.rs b/src/libstd/io/timer.rs
index ff6ae05bb49..41a0271fff2 100644
--- a/src/libstd/io/timer.rs
+++ b/src/libstd/io/timer.rs
@@ -22,7 +22,6 @@ use time::Duration;
 use io::{IoResult, IoError};
 use kinds::Send;
 use boxed::Box;
-use num::{CheckedMul, CheckedAdd};
 use rt::rtio::{IoFactory, LocalIo, RtioTimer, Callback};
 
 /// A synchronous timer object
@@ -71,31 +70,16 @@ pub struct Timer {
 
 struct TimerCallback { tx: Sender<()> }
 
-#[allow(missing_doc)]
-trait DurationExtension {
-    fn in_ms(&self) -> u64;
-}
-
-impl DurationExtension for Duration {
-    fn in_ms(&self) -> u64 {
-        if self.ndays() < 0 { fail!("negative duration") }
-        let nanos = self.nnanoseconds() as u64;
-        let secs = self.nseconds() as u64;
-        let days = self.ndays() as u64;
-        let nanos_in_ms = nanos / 1000;
-        let secs_in_ms = secs.checked_mul(&1000).expect("overflow");
-        let ms_per_day = 24 * 60 * 60 * 1000; // hours/day * min/hour * sec/min * ms/sec
-        let days_in_ms = days.checked_mul(&ms_per_day).expect("overflow");
-        let result = nanos_in_ms;
-        let result = result.checked_add(&secs_in_ms).expect("overflow");
-        let result = result.checked_add(&(days_in_ms as u64)).expect("overflow");
-        return result;
-    }
+fn in_ms(d: Duration) -> u64 {
+    // FIXME: Do we really want to fail on negative duration?
+    let ms = d.num_milliseconds();
+    if ms < 0 { fail!("negative duration") }
+    return ms as u64;
 }
 
 /// Sleep the current task for the specified duration.
 pub fn sleep(duration: Duration) {
-    sleep_ms(duration.in_ms())
+    sleep_ms(in_ms(duration))
 }
 
 /// Sleep the current task for `msecs` milliseconds.
@@ -121,7 +105,7 @@ impl Timer {
     /// Note that this function will cause any other receivers for this timer to
     /// be invalidated (the other end will be closed).
     pub fn sleep(&mut self, duration: Duration) {
-        self.obj.sleep(duration.in_ms());
+        self.obj.sleep(in_ms(duration));
     }
 
     /// Blocks the current task for `msecs` milliseconds.
@@ -145,7 +129,7 @@ impl Timer {
     /// fail.
     pub fn oneshot(&mut self, duration: Duration) -> Receiver<()> {
         let (tx, rx) = channel();
-        self.obj.oneshot(duration.in_ms(), box TimerCallback { tx: tx });
+        self.obj.oneshot(in_ms(duration), box TimerCallback { tx: tx });
         return rx
     }
 
@@ -204,7 +188,7 @@ impl Timer {
     /// fail.
     pub fn periodic(&mut self, duration: Duration) -> Receiver<()> {
         let (tx, rx) = channel();
-        self.obj.period(duration.in_ms(), box TimerCallback { tx: tx });
+        self.obj.period(in_ms(duration), box TimerCallback { tx: tx });
         return rx
     }