aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src
diff options
context:
space:
mode:
authorFenrirWolf <[email protected]>2018-02-17 13:04:42 -0700
committerGitHub <[email protected]>2018-02-17 13:04:42 -0700
commit81c3c0b63dc5fd9d7504e6add8f3a3f7b7beb0d1 (patch)
treeff21a014ed0623381f5c58aab3eab914d4f50c01 /ctr-std/src
parentFix TcpStream::wait_timeout (diff)
parentUpdate pinned nightly (diff)
downloadctru-rs-81c3c0b63dc5fd9d7504e6add8f3a3f7b7beb0d1.tar.xz
ctru-rs-81c3c0b63dc5fd9d7504e6add8f3a3f7b7beb0d1.zip
Merge pull request #62 from FenrirWolf/update-1.24
Pull in upstream changes for rust 1.24.0
Diffstat (limited to 'ctr-std/src')
-rw-r--r--ctr-std/src/collections/hash/map.rs73
-rw-r--r--ctr-std/src/env.rs12
-rw-r--r--ctr-std/src/f32.rs8
-rw-r--r--ctr-std/src/f64.rs7
-rw-r--r--ctr-std/src/io/cursor.rs2
-rw-r--r--ctr-std/src/path.rs9
-rw-r--r--ctr-std/src/time.rs (renamed from ctr-std/src/time/mod.rs)4
-rw-r--r--ctr-std/src/time/duration.rs587
8 files changed, 74 insertions, 628 deletions
diff --git a/ctr-std/src/collections/hash/map.rs b/ctr-std/src/collections/hash/map.rs
index b01420f..a82ff91 100644
--- a/ctr-std/src/collections/hash/map.rs
+++ b/ctr-std/src/collections/hash/map.rs
@@ -398,8 +398,9 @@ pub struct HashMap<K, V, S = RandomState> {
}
/// Search for a pre-hashed key.
+/// If you don't already know the hash, use search or search_mut instead
#[inline]
-fn search_hashed<K, V, M, F>(table: M, hash: SafeHash, mut is_match: F) -> InternalEntry<K, V, M>
+fn search_hashed<K, V, M, F>(table: M, hash: SafeHash, is_match: F) -> InternalEntry<K, V, M>
where M: Deref<Target = RawTable<K, V>>,
F: FnMut(&K) -> bool
{
@@ -410,6 +411,18 @@ fn search_hashed<K, V, M, F>(table: M, hash: SafeHash, mut is_match: F) -> Inter
return InternalEntry::TableIsEmpty;
}
+ search_hashed_nonempty(table, hash, is_match)
+}
+
+/// Search for a pre-hashed key when the hash map is known to be non-empty.
+#[inline]
+fn search_hashed_nonempty<K, V, M, F>(table: M, hash: SafeHash, mut is_match: F)
+ -> InternalEntry<K, V, M>
+ where M: Deref<Target = RawTable<K, V>>,
+ F: FnMut(&K) -> bool
+{
+ // Do not check the capacity as an extra branch could slow the lookup.
+
let size = table.size();
let mut probe = Bucket::new(table, hash);
let mut displacement = 0;
@@ -543,24 +556,36 @@ impl<K, V, S> HashMap<K, V, S>
}
/// Search for a key, yielding the index if it's found in the hashtable.
- /// If you already have the hash for the key lying around, use
- /// search_hashed.
+ /// If you already have the hash for the key lying around, or if you need an
+ /// InternalEntry, use search_hashed or search_hashed_nonempty.
#[inline]
- fn search<'a, Q: ?Sized>(&'a self, q: &Q) -> InternalEntry<K, V, &'a RawTable<K, V>>
+ fn search<'a, Q: ?Sized>(&'a self, q: &Q)
+ -> Option<FullBucket<K, V, &'a RawTable<K, V>>>
where K: Borrow<Q>,
Q: Eq + Hash
{
+ if self.is_empty() {
+ return None;
+ }
+
let hash = self.make_hash(q);
- search_hashed(&self.table, hash, |k| q.eq(k.borrow()))
+ search_hashed_nonempty(&self.table, hash, |k| q.eq(k.borrow()))
+ .into_occupied_bucket()
}
#[inline]
- fn search_mut<'a, Q: ?Sized>(&'a mut self, q: &Q) -> InternalEntry<K, V, &'a mut RawTable<K, V>>
+ fn search_mut<'a, Q: ?Sized>(&'a mut self, q: &Q)
+ -> Option<FullBucket<K, V, &'a mut RawTable<K, V>>>
where K: Borrow<Q>,
Q: Eq + Hash
{
+ if self.is_empty() {
+ return None;
+ }
+
let hash = self.make_hash(q);
- search_hashed(&mut self.table, hash, |k| q.eq(k.borrow()))
+ search_hashed_nonempty(&mut self.table, hash, |k| q.eq(k.borrow()))
+ .into_occupied_bucket()
}
// The caller should ensure that invariants by Robin Hood Hashing hold
@@ -1118,7 +1143,7 @@ impl<K, V, S> HashMap<K, V, S>
where K: Borrow<Q>,
Q: Hash + Eq
{
- self.search(k).into_occupied_bucket().map(|bucket| bucket.into_refs().1)
+ self.search(k).map(|bucket| bucket.into_refs().1)
}
/// Returns true if the map contains a value for the specified key.
@@ -1145,7 +1170,7 @@ impl<K, V, S> HashMap<K, V, S>
where K: Borrow<Q>,
Q: Hash + Eq
{
- self.search(k).into_occupied_bucket().is_some()
+ self.search(k).is_some()
}
/// Returns a mutable reference to the value corresponding to the key.
@@ -1174,7 +1199,7 @@ impl<K, V, S> HashMap<K, V, S>
where K: Borrow<Q>,
Q: Hash + Eq
{
- self.search_mut(k).into_occupied_bucket().map(|bucket| bucket.into_mut_refs().1)
+ self.search_mut(k).map(|bucket| bucket.into_mut_refs().1)
}
/// Inserts a key-value pair into the map.
@@ -1234,11 +1259,7 @@ impl<K, V, S> HashMap<K, V, S>
where K: Borrow<Q>,
Q: Hash + Eq
{
- if self.table.size() == 0 {
- return None;
- }
-
- self.search_mut(k).into_occupied_bucket().map(|bucket| pop_internal(bucket).1)
+ self.search_mut(k).map(|bucket| pop_internal(bucket).1)
}
/// Removes a key from the map, returning the stored key and value if the
@@ -1269,12 +1290,7 @@ impl<K, V, S> HashMap<K, V, S>
where K: Borrow<Q>,
Q: Hash + Eq
{
- if self.table.size() == 0 {
- return None;
- }
-
self.search_mut(k)
- .into_occupied_bucket()
.map(|bucket| {
let (k, v, _) = pop_internal(bucket);
(k, v)
@@ -1384,9 +1400,14 @@ impl<'a, K, Q: ?Sized, V, S> Index<&'a Q> for HashMap<K, V, S>
{
type Output = V;
+ /// Returns a reference to the value corresponding to the supplied key.
+ ///
+ /// # Panics
+ ///
+ /// Panics if the key is not present in the `HashMap`.
#[inline]
- fn index(&self, index: &Q) -> &V {
- self.get(index).expect("no entry found for key")
+ fn index(&self, key: &Q) -> &V {
+ self.get(key).expect("no entry found for key")
}
}
@@ -2627,15 +2648,11 @@ impl<K, S, Q: ?Sized> super::Recover<Q> for HashMap<K, (), S>
#[inline]
fn get(&self, key: &Q) -> Option<&K> {
- self.search(key).into_occupied_bucket().map(|bucket| bucket.into_refs().0)
+ self.search(key).map(|bucket| bucket.into_refs().0)
}
fn take(&mut self, key: &Q) -> Option<K> {
- if self.table.size() == 0 {
- return None;
- }
-
- self.search_mut(key).into_occupied_bucket().map(|bucket| pop_internal(bucket).0)
+ self.search_mut(key).map(|bucket| pop_internal(bucket).0)
}
#[inline]
diff --git a/ctr-std/src/env.rs b/ctr-std/src/env.rs
index 27bf326..c4946b6 100644
--- a/ctr-std/src/env.rs
+++ b/ctr-std/src/env.rs
@@ -723,6 +723,12 @@ pub fn args_os() -> ArgsOs {
ArgsOs { inner: sys::args::args() }
}
+#[stable(feature = "env_unimpl_send_sync", since = "1.25.0")]
+impl !Send for Args {}
+
+#[stable(feature = "env_unimpl_send_sync", since = "1.25.0")]
+impl !Sync for Args {}
+
#[stable(feature = "env", since = "1.0.0")]
impl Iterator for Args {
type Item = String;
@@ -754,6 +760,12 @@ impl fmt::Debug for Args {
}
}
+#[stable(feature = "env_unimpl_send_sync", since = "1.25.0")]
+impl !Send for ArgsOs {}
+
+#[stable(feature = "env_unimpl_send_sync", since = "1.25.0")]
+impl !Sync for ArgsOs {}
+
#[stable(feature = "env", since = "1.0.0")]
impl Iterator for ArgsOs {
type Item = OsString;
diff --git a/ctr-std/src/f32.rs b/ctr-std/src/f32.rs
index 5e5695f..a760922 100644
--- a/ctr-std/src/f32.rs
+++ b/ctr-std/src/f32.rs
@@ -1015,7 +1015,7 @@ impl f32 {
#[stable(feature = "float_bits_conv", since = "1.20.0")]
#[inline]
pub fn to_bits(self) -> u32 {
- unsafe { ::mem::transmute(self) }
+ num::Float::to_bits(self)
}
/// Raw transmutation from `u32`.
@@ -1023,7 +1023,7 @@ impl f32 {
/// This is currently identical to `transmute::<u32, f32>(v)` on all platforms.
/// It turns out this is incredibly portable, for two reasons:
///
- /// * Floats and Ints have the same endianess on all supported platforms.
+ /// * Floats and Ints have the same endianness on all supported platforms.
/// * IEEE-754 very precisely specifies the bit layout of floats.
///
/// However there is one caveat: prior to the 2008 version of IEEE-754, how
@@ -1059,8 +1059,7 @@ impl f32 {
#[stable(feature = "float_bits_conv", since = "1.20.0")]
#[inline]
pub fn from_bits(v: u32) -> Self {
- // It turns out the safety issues with sNaN were overblown! Hooray!
- unsafe { ::mem::transmute(v) }
+ num::Float::from_bits(v)
}
}
@@ -1532,6 +1531,7 @@ mod tests {
assert!(nan.to_degrees().is_nan());
assert_eq!(inf.to_degrees(), inf);
assert_eq!(neg_inf.to_degrees(), neg_inf);
+ assert_eq!(1_f32.to_degrees(), 57.2957795130823208767981548141051703);
}
#[test]
diff --git a/ctr-std/src/f64.rs b/ctr-std/src/f64.rs
index e4eea74..6f34f17 100644
--- a/ctr-std/src/f64.rs
+++ b/ctr-std/src/f64.rs
@@ -970,7 +970,7 @@ impl f64 {
#[stable(feature = "float_bits_conv", since = "1.20.0")]
#[inline]
pub fn to_bits(self) -> u64 {
- unsafe { ::mem::transmute(self) }
+ num::Float::to_bits(self)
}
/// Raw transmutation from `u64`.
@@ -978,7 +978,7 @@ impl f64 {
/// This is currently identical to `transmute::<u64, f64>(v)` on all platforms.
/// It turns out this is incredibly portable, for two reasons:
///
- /// * Floats and Ints have the same endianess on all supported platforms.
+ /// * Floats and Ints have the same endianness on all supported platforms.
/// * IEEE-754 very precisely specifies the bit layout of floats.
///
/// However there is one caveat: prior to the 2008 version of IEEE-754, how
@@ -1014,8 +1014,7 @@ impl f64 {
#[stable(feature = "float_bits_conv", since = "1.20.0")]
#[inline]
pub fn from_bits(v: u64) -> Self {
- // It turns out the safety issues with sNaN were overblown! Hooray!
- unsafe { ::mem::transmute(v) }
+ num::Float::from_bits(v)
}
}
diff --git a/ctr-std/src/io/cursor.rs b/ctr-std/src/io/cursor.rs
index c844770..76bcb5f 100644
--- a/ctr-std/src/io/cursor.rs
+++ b/ctr-std/src/io/cursor.rs
@@ -296,7 +296,7 @@ impl<'a> Write for Cursor<&'a mut [u8]> {
fn flush(&mut self) -> io::Result<()> { Ok(()) }
}
-#[unstable(feature = "cursor_mut_vec", issue = "30132")]
+#[stable(feature = "cursor_mut_vec", since = "1.25.0")]
impl<'a> Write for Cursor<&'a mut Vec<u8>> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
vec_write(&mut self.pos, self.inner, buf)
diff --git a/ctr-std/src/path.rs b/ctr-std/src/path.rs
index 7631a9a..e03a182 100644
--- a/ctr-std/src/path.rs
+++ b/ctr-std/src/path.rs
@@ -576,7 +576,7 @@ impl<'a> AsRef<OsStr> for Component<'a> {
}
}
-#[stable(feature = "path_component_asref", since = "1.24.0")]
+#[stable(feature = "path_component_asref", since = "1.25.0")]
impl<'a> AsRef<Path> for Component<'a> {
fn as_ref(&self) -> &Path {
self.as_os_str().as_ref()
@@ -1869,7 +1869,11 @@ impl Path {
///
/// let path = Path::new("/test/haha/foo.txt");
///
+ /// assert_eq!(path.strip_prefix("/"), Ok(Path::new("test/haha/foo.txt")));
/// assert_eq!(path.strip_prefix("/test"), Ok(Path::new("haha/foo.txt")));
+ /// assert_eq!(path.strip_prefix("/test/"), Ok(Path::new("haha/foo.txt")));
+ /// assert_eq!(path.strip_prefix("/test/haha/foo.txt"), Ok(Path::new("")));
+ /// assert_eq!(path.strip_prefix("/test/haha/foo.txt/"), Ok(Path::new("")));
/// assert_eq!(path.strip_prefix("test").is_ok(), false);
/// assert_eq!(path.strip_prefix("/haha").is_ok(), false);
/// ```
@@ -1900,6 +1904,9 @@ impl Path {
/// let path = Path::new("/etc/passwd");
///
/// assert!(path.starts_with("/etc"));
+ /// assert!(path.starts_with("/etc/"));
+ /// assert!(path.starts_with("/etc/passwd"));
+ /// assert!(path.starts_with("/etc/passwd/"));
///
/// assert!(!path.starts_with("/e"));
/// ```
diff --git a/ctr-std/src/time/mod.rs b/ctr-std/src/time.rs
index 6ce3b3e..12f2a9b 100644
--- a/ctr-std/src/time/mod.rs
+++ b/ctr-std/src/time.rs
@@ -29,9 +29,7 @@ use sys::time;
use sys_common::FromInner;
#[stable(feature = "time", since = "1.3.0")]
-pub use self::duration::Duration;
-
-mod duration;
+pub use core::time::Duration;
/// A measurement of a monotonically nondecreasing clock.
/// Opaque and useful only with `Duration`.
diff --git a/ctr-std/src/time/duration.rs b/ctr-std/src/time/duration.rs
deleted file mode 100644
index cb5bfb9..0000000
--- a/ctr-std/src/time/duration.rs
+++ /dev/null
@@ -1,587 +0,0 @@
-// Copyright 2012-2017 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-use iter::Sum;
-use ops::{Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign};
-
-const NANOS_PER_SEC: u32 = 1_000_000_000;
-const NANOS_PER_MILLI: u32 = 1_000_000;
-const NANOS_PER_MICRO: u32 = 1_000;
-const MILLIS_PER_SEC: u64 = 1_000;
-const MICROS_PER_SEC: u64 = 1_000_000;
-
-/// A `Duration` type to represent a span of time, typically used for system
-/// timeouts.
-///
-/// Each `Duration` is composed of a whole number of seconds and a fractional part
-/// represented in nanoseconds. If the underlying system does not support
-/// nanosecond-level precision, APIs binding a system timeout will typically round up
-/// the number of nanoseconds.
-///
-/// `Duration`s implement many common traits, including [`Add`], [`Sub`], and other
-/// [`ops`] traits.
-///
-/// [`Add`]: ../../std/ops/trait.Add.html
-/// [`Sub`]: ../../std/ops/trait.Sub.html
-/// [`ops`]: ../../std/ops/index.html
-///
-/// # Examples
-///
-/// ```
-/// 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.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, Hash, Default)]
-pub struct Duration {
- secs: u64,
- nanos: u32, // Always 0 <= nanos < NANOS_PER_SEC
-}
-
-impl Duration {
- /// Creates a new `Duration` from the specified number of whole seconds and
- /// additional nanoseconds.
- ///
- /// If the number of nanoseconds is greater than 1 billion (the number of
- /// nanoseconds in a second), then it will carry over into the seconds provided.
- ///
- /// # Panics
- ///
- /// This constructor will panic if the carry from the nanoseconds overflows
- /// the seconds counter.
- ///
- /// # Examples
- ///
- /// ```
- /// use std::time::Duration;
- ///
- /// let five_seconds = Duration::new(5, 0);
- /// ```
- #[stable(feature = "duration", since = "1.3.0")]
- #[inline]
- pub fn new(secs: u64, nanos: u32) -> Duration {
- let secs = secs.checked_add((nanos / NANOS_PER_SEC) as u64)
- .expect("overflow in Duration::new");
- let nanos = nanos % NANOS_PER_SEC;
- Duration { secs: secs, nanos: nanos }
- }
-
- /// Creates a new `Duration` from the specified number of whole seconds.
- ///
- /// # Examples
- ///
- /// ```
- /// use std::time::Duration;
- ///
- /// let duration = Duration::from_secs(5);
- ///
- /// assert_eq!(5, duration.as_secs());
- /// assert_eq!(0, duration.subsec_nanos());
- /// ```
- #[stable(feature = "duration", since = "1.3.0")]
- #[inline]
- pub fn from_secs(secs: u64) -> Duration {
- Duration { secs: secs, nanos: 0 }
- }
-
- /// Creates a new `Duration` from the specified number of milliseconds.
- ///
- /// # Examples
- ///
- /// ```
- /// use std::time::Duration;
- ///
- /// let duration = Duration::from_millis(2569);
- ///
- /// assert_eq!(2, duration.as_secs());
- /// assert_eq!(569_000_000, duration.subsec_nanos());
- /// ```
- #[stable(feature = "duration", since = "1.3.0")]
- #[inline]
- pub fn from_millis(millis: u64) -> Duration {
- let secs = millis / MILLIS_PER_SEC;
- let nanos = ((millis % MILLIS_PER_SEC) as u32) * NANOS_PER_MILLI;
- Duration { secs: secs, nanos: nanos }
- }
-
- /// Creates a new `Duration` from the specified number of microseconds.
- ///
- /// # Examples
- ///
- /// ```
- /// #![feature(duration_from_micros)]
- /// use std::time::Duration;
- ///
- /// let duration = Duration::from_micros(1_000_002);
- ///
- /// assert_eq!(1, duration.as_secs());
- /// assert_eq!(2000, duration.subsec_nanos());
- /// ```
- #[unstable(feature = "duration_from_micros", issue = "44400")]
- #[inline]
- pub fn from_micros(micros: u64) -> Duration {
- let secs = micros / MICROS_PER_SEC;
- let nanos = ((micros % MICROS_PER_SEC) as u32) * NANOS_PER_MICRO;
- Duration { secs: secs, nanos: nanos }
- }
-
- /// Creates a new `Duration` from the specified number of nanoseconds.
- ///
- /// # Examples
- ///
- /// ```
- /// #![feature(duration_extras)]
- /// use std::time::Duration;
- ///
- /// let duration = Duration::from_nanos(1_000_000_123);
- ///
- /// assert_eq!(1, duration.as_secs());
- /// assert_eq!(123, duration.subsec_nanos());
- /// ```
- #[unstable(feature = "duration_extras", issue = "46507")]
- #[inline]
- pub fn from_nanos(nanos: u64) -> Duration {
- let secs = nanos / (NANOS_PER_SEC as u64);
- let nanos = (nanos % (NANOS_PER_SEC as u64)) as u32;
- Duration { secs: secs, nanos: nanos }
- }
-
- /// Returns the number of _whole_ seconds contained by this `Duration`.
- ///
- /// The returned value does not include the fractional (nanosecond) part of the
- /// duration, which can be obtained using [`subsec_nanos`].
- ///
- /// # Examples
- ///
- /// ```
- /// use std::time::Duration;
- ///
- /// let duration = Duration::new(5, 730023852);
- /// assert_eq!(duration.as_secs(), 5);
- /// ```
- ///
- /// To determine the total number of seconds represented by the `Duration`,
- /// use `as_secs` in combination with [`subsec_nanos`]:
- ///
- /// ```
- /// use std::time::Duration;
- ///
- /// let duration = Duration::new(5, 730023852);
- ///
- /// assert_eq!(5.730023852,
- /// duration.as_secs() as f64
- /// + duration.subsec_nanos() as f64 * 1e-9);
- /// ```
- ///
- /// [`subsec_nanos`]: #method.subsec_nanos
- #[stable(feature = "duration", since = "1.3.0")]
- #[inline]
- pub fn as_secs(&self) -> u64 { self.secs }
-
- /// Returns the fractional part of this `Duration`, in milliseconds.
- ///
- /// This method does **not** return the length of the duration when
- /// represented by milliseconds. The returned number always represents a
- /// fractional portion of a second (i.e. it is less than one thousand).
- ///
- /// # Examples
- ///
- /// ```
- /// #![feature(duration_extras)]
- /// use std::time::Duration;
- ///
- /// let duration = Duration::from_millis(5432);
- /// assert_eq!(duration.as_secs(), 5);
- /// assert_eq!(duration.subsec_millis(), 432);
- /// ```
- #[unstable(feature = "duration_extras", issue = "46507")]
- #[inline]
- pub fn subsec_millis(&self) -> u32 { self.nanos / NANOS_PER_MILLI }
-
- /// Returns the fractional part of this `Duration`, in microseconds.
- ///
- /// This method does **not** return the length of the duration when
- /// represented by microseconds. The returned number always represents a
- /// fractional portion of a second (i.e. it is less than one million).
- ///
- /// # Examples
- ///
- /// ```
- /// #![feature(duration_extras, duration_from_micros)]
- /// use std::time::Duration;
- ///
- /// let duration = Duration::from_micros(1_234_567);
- /// assert_eq!(duration.as_secs(), 1);
- /// assert_eq!(duration.subsec_micros(), 234_567);
- /// ```
- #[unstable(feature = "duration_extras", issue = "46507")]
- #[inline]
- pub fn subsec_micros(&self) -> u32 { self.nanos / NANOS_PER_MICRO }
-
- /// Returns the fractional part of this `Duration`, in nanoseconds.
- ///
- /// 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 (i.e. it is less than one billion).
- ///
- /// # Examples
- ///
- /// ```
- /// use std::time::Duration;
- ///
- /// let duration = Duration::from_millis(5010);
- /// assert_eq!(duration.as_secs(), 5);
- /// assert_eq!(duration.subsec_nanos(), 10_000_000);
- /// ```
- #[stable(feature = "duration", since = "1.3.0")]
- #[inline]
- pub fn subsec_nanos(&self) -> u32 { self.nanos }
-
- /// Checked `Duration` addition. Computes `self + other`, returning [`None`]
- /// if overflow occurred.
- ///
- /// [`None`]: ../../std/option/enum.Option.html#variant.None
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// use std::time::Duration;
- ///
- /// assert_eq!(Duration::new(0, 0).checked_add(Duration::new(0, 1)), Some(Duration::new(0, 1)));
- /// assert_eq!(Duration::new(1, 0).checked_add(Duration::new(std::u64::MAX, 0)), None);
- /// ```
- #[stable(feature = "duration_checked_ops", since = "1.16.0")]
- #[inline]
- pub fn checked_add(self, rhs: Duration) -> Option<Duration> {
- if let Some(mut secs) = self.secs.checked_add(rhs.secs) {
- let mut nanos = self.nanos + rhs.nanos;
- if nanos >= NANOS_PER_SEC {
- nanos -= NANOS_PER_SEC;
- if let Some(new_secs) = secs.checked_add(1) {
- secs = new_secs;
- } else {
- return None;
- }
- }
- debug_assert!(nanos < NANOS_PER_SEC);
- Some(Duration {
- secs,
- nanos,
- })
- } else {
- None
- }
- }
-
- /// Checked `Duration` subtraction. Computes `self - other`, returning [`None`]
- /// if the result would be negative or if overflow occurred.
- ///
- /// [`None`]: ../../std/option/enum.Option.html#variant.None
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// use std::time::Duration;
- ///
- /// assert_eq!(Duration::new(0, 1).checked_sub(Duration::new(0, 0)), Some(Duration::new(0, 1)));
- /// assert_eq!(Duration::new(0, 0).checked_sub(Duration::new(0, 1)), None);
- /// ```
- #[stable(feature = "duration_checked_ops", since = "1.16.0")]
- #[inline]
- pub fn checked_sub(self, rhs: Duration) -> Option<Duration> {
- if let Some(mut secs) = self.secs.checked_sub(rhs.secs) {
- let nanos = if self.nanos >= rhs.nanos {
- self.nanos - rhs.nanos
- } else {
- if let Some(sub_secs) = secs.checked_sub(1) {
- secs = sub_secs;
- self.nanos + NANOS_PER_SEC - rhs.nanos
- } else {
- return None;
- }
- };
- debug_assert!(nanos < NANOS_PER_SEC);
- Some(Duration { secs: secs, nanos: nanos })
- } else {
- None
- }
- }
-
- /// Checked `Duration` multiplication. Computes `self * other`, returning
- /// [`None`] if overflow occurred.
- ///
- /// [`None`]: ../../std/option/enum.Option.html#variant.None
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// use std::time::Duration;
- ///
- /// assert_eq!(Duration::new(0, 500_000_001).checked_mul(2), Some(Duration::new(1, 2)));
- /// assert_eq!(Duration::new(std::u64::MAX - 1, 0).checked_mul(2), None);
- /// ```
- #[stable(feature = "duration_checked_ops", since = "1.16.0")]
- #[inline]
- pub fn checked_mul(self, rhs: u32) -> Option<Duration> {
- // Multiply nanoseconds as u64, because it cannot overflow that way.
- let total_nanos = self.nanos as u64 * rhs as u64;
- let extra_secs = total_nanos / (NANOS_PER_SEC as u64);
- let nanos = (total_nanos % (NANOS_PER_SEC as u64)) as u32;
- if let Some(secs) = self.secs
- .checked_mul(rhs as u64)
- .and_then(|s| s.checked_add(extra_secs)) {
- debug_assert!(nanos < NANOS_PER_SEC);
- Some(Duration {
- secs,
- nanos,
- })
- } else {
- None
- }
- }
-
- /// Checked `Duration` division. Computes `self / other`, returning [`None`]
- /// if `other == 0`.
- ///
- /// [`None`]: ../../std/option/enum.Option.html#variant.None
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// use std::time::Duration;
- ///
- /// assert_eq!(Duration::new(2, 0).checked_div(2), Some(Duration::new(1, 0)));
- /// assert_eq!(Duration::new(1, 0).checked_div(2), Some(Duration::new(0, 500_000_000)));
- /// assert_eq!(Duration::new(2, 0).checked_div(0), None);
- /// ```
- #[stable(feature = "duration_checked_ops", since = "1.16.0")]
- #[inline]
- pub fn checked_div(self, rhs: u32) -> Option<Duration> {
- if rhs != 0 {
- let secs = self.secs / (rhs as u64);
- let carry = self.secs - secs * (rhs as u64);
- let extra_nanos = carry * (NANOS_PER_SEC as u64) / (rhs as u64);
- let nanos = self.nanos / rhs + (extra_nanos as u32);
- debug_assert!(nanos < NANOS_PER_SEC);
- Some(Duration { secs: secs, nanos: nanos })
- } else {
- None
- }
- }
-}
-
-#[stable(feature = "duration", since = "1.3.0")]
-impl Add for Duration {
- type Output = Duration;
-
- fn add(self, rhs: Duration) -> Duration {
- self.checked_add(rhs).expect("overflow when adding durations")
- }
-}
-
-#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
-impl AddAssign for Duration {
- fn add_assign(&mut self, rhs: Duration) {
- *self = *self + rhs;
- }
-}
-
-#[stable(feature = "duration", since = "1.3.0")]
-impl Sub for Duration {
- type Output = Duration;
-
- fn sub(self, rhs: Duration) -> Duration {
- self.checked_sub(rhs).expect("overflow when subtracting durations")
- }
-}
-
-#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
-impl SubAssign for Duration {
- fn sub_assign(&mut self, rhs: Duration) {
- *self = *self - rhs;
- }
-}
-
-#[stable(feature = "duration", since = "1.3.0")]
-impl Mul<u32> for Duration {
- type Output = Duration;
-
- fn mul(self, rhs: u32) -> Duration {
- self.checked_mul(rhs).expect("overflow when multiplying duration by scalar")
- }
-}
-
-#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
-impl MulAssign<u32> for Duration {
- fn mul_assign(&mut self, rhs: u32) {
- *self = *self * rhs;
- }
-}
-
-#[stable(feature = "duration", since = "1.3.0")]
-impl Div<u32> for Duration {
- type Output = Duration;
-
- fn div(self, rhs: u32) -> Duration {
- self.checked_div(rhs).expect("divide by zero error when dividing duration by scalar")
- }
-}
-
-#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
-impl DivAssign<u32> for Duration {
- fn div_assign(&mut self, rhs: u32) {
- *self = *self / rhs;
- }
-}
-
-#[stable(feature = "duration_sum", since = "1.16.0")]
-impl Sum for Duration {
- fn sum<I: Iterator<Item=Duration>>(iter: I) -> Duration {
- iter.fold(Duration::new(0, 0), |a, b| a + b)
- }
-}
-
-#[stable(feature = "duration_sum", since = "1.16.0")]
-impl<'a> Sum<&'a Duration> for Duration {
- fn sum<I: Iterator<Item=&'a Duration>>(iter: I) -> Duration {
- iter.fold(Duration::new(0, 0), |a, b| a + *b)
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::Duration;
-
- #[test]
- fn creation() {
- assert!(Duration::from_secs(1) != Duration::from_secs(0));
- assert_eq!(Duration::from_secs(1) + Duration::from_secs(2),
- Duration::from_secs(3));
- assert_eq!(Duration::from_millis(10) + Duration::from_secs(4),
- Duration::new(4, 10 * 1_000_000));
- assert_eq!(Duration::from_millis(4000), Duration::new(4, 0));
- }
-
- #[test]
- fn secs() {
- 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).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]
- fn add() {
- assert_eq!(Duration::new(0, 0) + Duration::new(0, 1),
- Duration::new(0, 1));
- assert_eq!(Duration::new(0, 500_000_000) + Duration::new(0, 500_000_001),
- Duration::new(1, 1));
- }
-
- #[test]
- fn checked_add() {
- assert_eq!(Duration::new(0, 0).checked_add(Duration::new(0, 1)),
- Some(Duration::new(0, 1)));
- assert_eq!(Duration::new(0, 500_000_000).checked_add(Duration::new(0, 500_000_001)),
- Some(Duration::new(1, 1)));
- assert_eq!(Duration::new(1, 0).checked_add(Duration::new(::u64::MAX, 0)), None);
- }
-
- #[test]
- fn sub() {
- assert_eq!(Duration::new(0, 1) - Duration::new(0, 0),
- Duration::new(0, 1));
- assert_eq!(Duration::new(0, 500_000_001) - Duration::new(0, 500_000_000),
- Duration::new(0, 1));
- assert_eq!(Duration::new(1, 0) - Duration::new(0, 1),
- Duration::new(0, 999_999_999));
- }
-
- #[test]
- fn checked_sub() {
- let zero = Duration::new(0, 0);
- let one_nano = Duration::new(0, 1);
- let one_sec = Duration::new(1, 0);
- assert_eq!(one_nano.checked_sub(zero), Some(Duration::new(0, 1)));
- assert_eq!(one_sec.checked_sub(one_nano),
- Some(Duration::new(0, 999_999_999)));
- assert_eq!(zero.checked_sub(one_nano), None);
- assert_eq!(zero.checked_sub(one_sec), None);
- }
-
- #[test] #[should_panic]
- fn sub_bad1() {
- Duration::new(0, 0) - Duration::new(0, 1);
- }
-
- #[test] #[should_panic]
- fn sub_bad2() {
- Duration::new(0, 0) - Duration::new(1, 0);
- }
-
- #[test]
- fn mul() {
- assert_eq!(Duration::new(0, 1) * 2, Duration::new(0, 2));
- assert_eq!(Duration::new(1, 1) * 3, Duration::new(3, 3));
- assert_eq!(Duration::new(0, 500_000_001) * 4, Duration::new(2, 4));
- assert_eq!(Duration::new(0, 500_000_001) * 4000,
- Duration::new(2000, 4000));
- }
-
- #[test]
- fn checked_mul() {
- assert_eq!(Duration::new(0, 1).checked_mul(2), Some(Duration::new(0, 2)));
- assert_eq!(Duration::new(1, 1).checked_mul(3), Some(Duration::new(3, 3)));
- assert_eq!(Duration::new(0, 500_000_001).checked_mul(4), Some(Duration::new(2, 4)));
- assert_eq!(Duration::new(0, 500_000_001).checked_mul(4000),
- Some(Duration::new(2000, 4000)));
- assert_eq!(Duration::new(::u64::MAX - 1, 0).checked_mul(2), None);
- }
-
- #[test]
- fn div() {
- assert_eq!(Duration::new(0, 1) / 2, Duration::new(0, 0));
- assert_eq!(Duration::new(1, 1) / 3, Duration::new(0, 333_333_333));
- assert_eq!(Duration::new(99, 999_999_000) / 100,
- Duration::new(0, 999_999_990));
- }
-
- #[test]
- fn checked_div() {
- assert_eq!(Duration::new(2, 0).checked_div(2), Some(Duration::new(1, 0)));
- assert_eq!(Duration::new(1, 0).checked_div(2), Some(Duration::new(0, 500_000_000)));
- assert_eq!(Duration::new(2, 0).checked_div(0), None);
- }
-}