Skip to content

Rollup of 9 pull requests #38450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 23 commits into from
Closed
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9976f5f
Add missing doc examples for SocketAddr struct
GuillaumeGomez Dec 8, 2016
7fe17f9
Add doc examples for UnixStream
GuillaumeGomez Dec 8, 2016
c35b9f6
Add UnixListener doc examples
GuillaumeGomez Dec 8, 2016
a78a33c
Add Incoming doc examples
GuillaumeGomez Dec 8, 2016
8323185
minor fix about visibility in reference
liigo Dec 7, 2016
e095c71
Associated items and variants inherit visibility from their traits an…
liigo Dec 13, 2016
16d4b7b
doc: Explain meaning of Result iters and link to factory functions.
sourcefrog Dec 3, 2016
60fbe7a
Add missing Duration examples
GuillaumeGomez Dec 13, 2016
8cd3081
rustdoc: a formatting nit
tshepang Dec 15, 2016
2938e6a
Add missing doc examples for UnixDatagram
GuillaumeGomez Dec 8, 2016
0971cb1
Document platform-specific differences for `std::process::exit`.
frewsxcv Dec 15, 2016
d409fc3
Rewrite, improve documentation for `core::hash::BuildHasherDefault`.
frewsxcv Dec 13, 2016
ca37604
tidy features: use 2-parameter form of internal try macro for open err
zackmdavis Dec 17, 2016
1e7bc90
stage0.txt: typo fix
est31 Dec 18, 2016
5f3b2c7
Rollup merge of #38158 - sourcefrog:doc-iter, r=GuillaumeGomez
GuillaumeGomez Dec 18, 2016
d808ae1
Rollup merge of #38215 - liigo:patch-12, r=petrochenkov
GuillaumeGomez Dec 18, 2016
1b90f83
Rollup merge of #38236 - GuillaumeGomez:unix_socket_doc, r=frewsxcv
GuillaumeGomez Dec 18, 2016
ca8f287
Rollup merge of #38334 - frewsxcv:BuildHasherDefault, r=GuillaumeGomez
GuillaumeGomez Dec 18, 2016
6f1a3c0
Rollup merge of #38346 - GuillaumeGomez:duration_doc, r=frewsxcv
GuillaumeGomez Dec 18, 2016
1fbcb3e
Rollup merge of #38395 - tshepang:nit, r=steveklabnik
GuillaumeGomez Dec 18, 2016
fdc9150
Rollup merge of #38397 - frewsxcv:platform-specific-process-exit, r=a…
GuillaumeGomez Dec 18, 2016
dae4eaa
Rollup merge of #38422 - zackmdavis:enjoy_tidy_path_error_macro_nicet…
GuillaumeGomez Dec 18, 2016
3e02adc
Rollup merge of #38445 - est31:master, r=apasel422
GuillaumeGomez Dec 18, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 66 additions & 13 deletions src/libstd/time/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ const NANOS_PER_SEC: u32 = 1_000_000_000;
const NANOS_PER_MILLI: u32 = 1_000_000;
const MILLIS_PER_SEC: u64 = 1_000;

/// A duration type to represent a span of time, typically used for system
/// A `Duration` type to represent a span of time, typically used for system
/// timeouts.
///
/// Each duration is composed of a number of seconds and nanosecond precision.
/// Each `Duration` is composed of a number of seconds and nanosecond precision.
/// APIs binding a system timeout will typically round up the nanosecond
/// precision if the underlying system does not support that level of precision.
///
/// Durations implement many common traits, including `Add`, `Sub`, and other
/// ops traits. Currently a duration may only be inspected for its number of
/// seconds and its nanosecond precision.
/// `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
///
Expand Down Expand Up @@ -56,6 +59,14 @@ impl Duration {
///
/// 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 {
Expand All @@ -66,13 +77,29 @@ impl Duration {
}

/// Creates a new `Duration` from the specified number of seconds.
///
/// # Examples
///
/// ```
/// use std::time::Duration;
///
/// let five_seconds = Duration::from_secs(5);
/// ```
#[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 five_seconds = Duration::from_millis(5000);
/// ```
#[stable(feature = "duration", since = "1.3.0")]
#[inline]
pub fn from_millis(millis: u64) -> Duration {
Expand All @@ -81,26 +108,46 @@ impl Duration {
Duration { secs: secs, nanos: nanos }
}

/// Returns the number of whole seconds represented by this duration.
/// Returns the number of whole seconds represented by this `Duration`.
///
/// The extra precision represented by this duration is ignored (i.e. extra
/// nanoseconds are not represented in the returned value).
///
/// # Examples
///
/// ```
/// use std::time::Duration;
///
/// let five_seconds = Duration::new(5, 0);
/// assert_eq!(five_seconds.as_secs(), 5);
/// ```
#[stable(feature = "duration", since = "1.3.0")]
#[inline]
pub fn as_secs(&self) -> u64 { self.secs }

/// Returns the nanosecond precision represented by this duration.
/// 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 (i.e. it is less than one billion).
///
/// # Examples
///
/// ```
/// use std::time::Duration;
///
/// let duration = Duration::from_millis(5010);
/// assert_eq!(duration.subsec_nanos(), 10000000);
/// ```
#[stable(feature = "duration", since = "1.3.0")]
#[inline]
pub fn subsec_nanos(&self) -> u32 { self.nanos }

/// Checked duration addition. Computes `self + other`, returning `None`
/// Checked `Duration` addition. Computes `self + other`, returning [`None`]
/// if overflow occurred.
///
/// [`None`]: ../../std/option/enum.Option.html#variant.None
///
/// # Examples
///
/// Basic usage:
Expand Down Expand Up @@ -136,9 +183,11 @@ impl Duration {
}
}

/// Checked duration subtraction. Computes `self + other`, returning `None`
/// Checked `Duration` subtraction. Computes `self - other`, returning [`None`]
/// if the result would be negative or if underflow occurred.
///
/// [`None`]: ../../std/option/enum.Option.html#variant.None
///
/// # Examples
///
/// Basic usage:
Expand Down Expand Up @@ -172,8 +221,10 @@ impl Duration {
}
}

/// Checked duration multiplication. Computes `self * other`, returning
/// `None` if underflow or overflow occurred.
/// Checked `Duration` multiplication. Computes `self * other`, returning
/// [`None`] if overflow occurred.
///
/// [`None`]: ../../std/option/enum.Option.html#variant.None
///
/// # Examples
///
Expand Down Expand Up @@ -207,8 +258,10 @@ impl Duration {
}
}

/// Checked duration division. Computes `self / other`, returning `None`
/// if `other == 0` or the operation results in underflow or overflow.
/// Checked `Duration` division. Computes `self / other`, returning [`None`]
/// if `other == 0`.
///
/// [`None`]: ../../std/option/enum.Option.html#variant.None
///
/// # Examples
///
Expand Down