summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-08-11 11:57:06 -0700
committerAlex Crichton <alex@alexcrichton.com>2015-08-11 11:57:06 -0700
commit32a5cebfbd80d85bd0f0fe0d344c8386eb4037e2 (patch)
tree582679058f44dfd829e8133e6d58d07ce1df0248 /src/libstd
parentdcab9506f308d3d3be2521558f61e2b21d32df67 (diff)
parent6a42e5de024a82211ed36d580dcb588f8deb8e90 (diff)
downloadrust-32a5cebfbd80d85bd0f0fe0d344c8386eb4037e2.tar.gz
rust-32a5cebfbd80d85bd0f0fe0d344c8386eb4037e2.zip
Merge pull request #27668 from brson/beta-next
Beta next
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/sys/unix/condvar.rs4
-rw-r--r--src/libstd/sys/unix/net.rs8
-rw-r--r--src/libstd/sys/unix/thread.rs4
-rw-r--r--src/libstd/sys/windows/mod.rs6
-rw-r--r--src/libstd/time/duration.rs86
-rw-r--r--src/libstd/time/mod.rs2
6 files changed, 50 insertions, 60 deletions
diff --git a/src/libstd/sys/unix/condvar.rs b/src/libstd/sys/unix/condvar.rs
index beecb445e8d..f0df8d9072c 100644
--- a/src/libstd/sys/unix/condvar.rs
+++ b/src/libstd/sys/unix/condvar.rs
@@ -60,11 +60,11 @@ impl Condvar {
         let r = ffi::gettimeofday(&mut sys_now, ptr::null_mut());
         debug_assert_eq!(r, 0);
 
-        let nsec = dur.extra_nanos() as libc::c_long +
+        let nsec = dur.subsec_nanos() as libc::c_long +
                    (sys_now.tv_usec * 1000) as libc::c_long;
         let extra = (nsec / 1_000_000_000) as libc::time_t;
         let nsec = nsec % 1_000_000_000;
-        let seconds = dur.secs() as libc::time_t;
+        let seconds = dur.as_secs() as libc::time_t;
 
         let timeout = sys_now.tv_sec.checked_add(extra).and_then(|s| {
             s.checked_add(seconds)
diff --git a/src/libstd/sys/unix/net.rs b/src/libstd/sys/unix/net.rs
index 37eb7fd2ac8..e65f64f2029 100644
--- a/src/libstd/sys/unix/net.rs
+++ b/src/libstd/sys/unix/net.rs
@@ -79,19 +79,19 @@ impl Socket {
     pub fn set_timeout(&self, dur: Option<Duration>, kind: libc::c_int) -> io::Result<()> {
         let timeout = match dur {
             Some(dur) => {
-                if dur.secs() == 0 && dur.extra_nanos() == 0 {
+                if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
                     return Err(io::Error::new(io::ErrorKind::InvalidInput,
                                               "cannot set a 0 duration timeout"));
                 }
 
-                let secs = if dur.secs() > libc::time_t::max_value() as u64 {
+                let secs = if dur.as_secs() > libc::time_t::max_value() as u64 {
                     libc::time_t::max_value()
                 } else {
-                    dur.secs() as libc::time_t
+                    dur.as_secs() as libc::time_t
                 };
                 let mut timeout = libc::timeval {
                     tv_sec: secs,
-                    tv_usec: (dur.extra_nanos() / 1000) as libc::suseconds_t,
+                    tv_usec: (dur.subsec_nanos() / 1000) as libc::suseconds_t,
                 };
                 if timeout.tv_sec == 0 && timeout.tv_usec == 0 {
                     timeout.tv_usec = 1;
diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs
index 6be61f06926..80193ee8cfe 100644
--- a/src/libstd/sys/unix/thread.rs
+++ b/src/libstd/sys/unix/thread.rs
@@ -131,8 +131,8 @@ impl Thread {
 
     pub fn sleep(dur: Duration) {
         let mut ts = libc::timespec {
-            tv_sec: dur.secs() as libc::time_t,
-            tv_nsec: dur.extra_nanos() as libc::c_long,
+            tv_sec: dur.as_secs() as libc::time_t,
+            tv_nsec: dur.subsec_nanos() as libc::c_long,
         };
 
         // If we're awoken with a signal then the return value will be -1 and
diff --git a/src/libstd/sys/windows/mod.rs b/src/libstd/sys/windows/mod.rs
index b6d080109df..b38945d8916 100644
--- a/src/libstd/sys/windows/mod.rs
+++ b/src/libstd/sys/windows/mod.rs
@@ -162,10 +162,10 @@ fn dur2timeout(dur: Duration) -> libc::DWORD {
     // * Nanosecond precision is rounded up
     // * Greater than u32::MAX milliseconds (50 days) is rounded up to INFINITE
     //   (never time out).
-    dur.secs().checked_mul(1000).and_then(|ms| {
-        ms.checked_add((dur.extra_nanos() as u64) / 1_000_000)
+    dur.as_secs().checked_mul(1000).and_then(|ms| {
+        ms.checked_add((dur.subsec_nanos() as u64) / 1_000_000)
     }).and_then(|ms| {
-        ms.checked_add(if dur.extra_nanos() % 1_000_000 > 0 {1} else {0})
+        ms.checked_add(if dur.subsec_nanos() % 1_000_000 > 0 {1} else {0})
     }).map(|ms| {
         if ms > <libc::DWORD>::max_value() as u64 {
             libc::INFINITE
diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs
index 8001df29d1f..5270f9e6180 100644
--- a/src/libstd/time/duration.rs
+++ b/src/libstd/time/duration.rs
@@ -8,13 +8,9 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-//! Temporal quantification
-
-#![unstable(feature = "duration", reason = "recently added API per RFC 1040")]
-
+#[cfg(stage0)]
 use prelude::v1::*;
 
-use fmt;
 use ops::{Add, Sub, Mul, Div};
 use sys::time::SteadyTime;
 
@@ -36,17 +32,17 @@ const MILLIS_PER_SEC: u64 = 1_000;
 /// # Examples
 ///
 /// ```
-/// #![feature(duration)]
 /// use std::time::Duration;
 ///
 /// let five_seconds = Duration::new(5, 0);
 /// let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5);
 ///
-/// assert_eq!(five_seconds_and_five_nanos.secs(), 5);
-/// assert_eq!(five_seconds_and_five_nanos.extra_nanos(), 5);
+/// assert_eq!(five_seconds_and_five_nanos.as_secs(), 5);
+/// assert_eq!(five_seconds_and_five_nanos.subsec_nanos(), 5);
 ///
 /// let ten_millis = Duration::from_millis(10);
 /// ```
+#[stable(feature = "duration", since = "1.3.0")]
 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
 pub struct Duration {
     secs: u64,
@@ -59,6 +55,7 @@ impl Duration {
     ///
     /// If the nanoseconds is greater than 1 billion (the number of nanoseconds
     /// in a second), then it will carry over into the seconds provided.
+    #[stable(feature = "duration", since = "1.3.0")]
     pub fn new(secs: u64, nanos: u32) -> Duration {
         let secs = secs + (nanos / NANOS_PER_SEC) as u64;
         let nanos = nanos % NANOS_PER_SEC;
@@ -78,11 +75,13 @@ impl Duration {
     }
 
     /// Creates a new `Duration` from the specified number of seconds.
+    #[stable(feature = "duration", since = "1.3.0")]
     pub fn from_secs(secs: u64) -> Duration {
         Duration { secs: secs, nanos: 0 }
     }
 
     /// Creates a new `Duration` from the specified number of milliseconds.
+    #[stable(feature = "duration", since = "1.3.0")]
     pub fn from_millis(millis: u64) -> Duration {
         let secs = millis / MILLIS_PER_SEC;
         let nanos = ((millis % MILLIS_PER_SEC) as u32) * NANOS_PER_MILLI;
@@ -93,14 +92,33 @@ impl Duration {
     ///
     /// The extra precision represented by this duration is ignored (e.g. extra
     /// nanoseconds are not represented in the returned value).
-    pub fn secs(&self) -> u64 { self.secs }
+    #[stable(feature = "duration", since = "1.3.0")]
+    pub fn as_secs(&self) -> u64 { self.secs }
+
+    #[deprecated(reason = "renamed to `as_secs`", since = "1.3.0")]
+    #[unstable(feature = "duration_deprecated")]
+    /// Returns the number of whole seconds represented by this duration.
+    ///
+    /// The extra precision represented by this duration is ignored (e.g. extra
+    /// nanoseconds are not represented in the returned value).
+    pub fn secs(&self) -> u64 { self.as_secs() }
+
+    /// Returns the nanosecond precision represented by this duration.
+    ///
+    /// This method does **not** return the length of the duration when
+    /// represented by nanoseconds. The returned number always represents a
+    /// fractional portion of a second (e.g. it is less than one billion).
+    #[stable(feature = "duration", since = "1.3.0")]
+    pub fn subsec_nanos(&self) -> u32 { self.nanos }
 
+    #[deprecated(reason = "renamed to `subsec_nanos`", since = "1.3.0")]
+    #[unstable(feature = "duration_deprecated")]
     /// Returns the nanosecond precision represented by this duration.
     ///
     /// This method does **not** return the length of the duration when
     /// represented by nanoseconds. The returned number always represents a
     /// fractional portion of a second (e.g. it is less than one billion).
-    pub fn extra_nanos(&self) -> u32 { self.nanos }
+    pub fn extra_nanos(&self) -> u32 { self.subsec_nanos() }
 }
 
 impl Add for Duration {
@@ -166,20 +184,6 @@ impl Div<u32> for Duration {
     }
 }
 
-impl fmt::Display for Duration {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        match (self.secs, self.nanos) {
-            (s, 0) => write!(f, "{}s", s),
-            (0, n) if n % NANOS_PER_MILLI == 0 => write!(f, "{}ms",
-                                                         n / NANOS_PER_MILLI),
-            (0, n) if n % 1_000 == 0 => write!(f, "{}µs", n / 1_000),
-            (0, n) => write!(f, "{}ns", n),
-            (s, n) => write!(f, "{}.{}s", s,
-                             format!("{:09}", n).trim_right_matches('0'))
-        }
-    }
-}
-
 #[cfg(test)]
 mod tests {
     use prelude::v1::*;
@@ -197,20 +201,20 @@ mod tests {
 
     #[test]
     fn secs() {
-        assert_eq!(Duration::new(0, 0).secs(), 0);
-        assert_eq!(Duration::from_secs(1).secs(), 1);
-        assert_eq!(Duration::from_millis(999).secs(), 0);
-        assert_eq!(Duration::from_millis(1001).secs(), 1);
+        assert_eq!(Duration::new(0, 0).as_secs(), 0);
+        assert_eq!(Duration::from_secs(1).as_secs(), 1);
+        assert_eq!(Duration::from_millis(999).as_secs(), 0);
+        assert_eq!(Duration::from_millis(1001).as_secs(), 1);
     }
 
     #[test]
     fn nanos() {
-        assert_eq!(Duration::new(0, 0).extra_nanos(), 0);
-        assert_eq!(Duration::new(0, 5).extra_nanos(), 5);
-        assert_eq!(Duration::new(0, 1_000_000_001).extra_nanos(), 1);
-        assert_eq!(Duration::from_secs(1).extra_nanos(), 0);
-        assert_eq!(Duration::from_millis(999).extra_nanos(), 999 * 1_000_000);
-        assert_eq!(Duration::from_millis(1001).extra_nanos(), 1 * 1_000_000);
+        assert_eq!(Duration::new(0, 0).subsec_nanos(), 0);
+        assert_eq!(Duration::new(0, 5).subsec_nanos(), 5);
+        assert_eq!(Duration::new(0, 1_000_000_001).subsec_nanos(), 1);
+        assert_eq!(Duration::from_secs(1).subsec_nanos(), 0);
+        assert_eq!(Duration::from_millis(999).subsec_nanos(), 999 * 1_000_000);
+        assert_eq!(Duration::from_millis(1001).subsec_nanos(), 1 * 1_000_000);
     }
 
     #[test]
@@ -257,18 +261,4 @@ mod tests {
         assert_eq!(Duration::new(99, 999_999_000) / 100,
                    Duration::new(0, 999_999_990));
     }
-
-    #[test]
-    fn display() {
-        assert_eq!(Duration::new(0, 2).to_string(), "2ns");
-        assert_eq!(Duration::new(0, 2_000_000).to_string(), "2ms");
-        assert_eq!(Duration::new(2, 0).to_string(), "2s");
-        assert_eq!(Duration::new(2, 2).to_string(), "2.000000002s");
-        assert_eq!(Duration::new(2, 2_000_000).to_string(),
-                   "2.002s");
-        assert_eq!(Duration::new(0, 2_000_002).to_string(),
-                   "2000002ns");
-        assert_eq!(Duration::new(2, 2_000_002).to_string(),
-                   "2.002000002s");
-    }
 }
diff --git a/src/libstd/time/mod.rs b/src/libstd/time/mod.rs
index d535b195519..29df882a042 100644
--- a/src/libstd/time/mod.rs
+++ b/src/libstd/time/mod.rs
@@ -10,7 +10,7 @@
 
 //! Temporal quantification.
 
-#![unstable(feature = "time")]
+#![stable(feature = "time", since = "1.3.0")]
 
 pub use self::duration::Duration;