Skip to content

Commit 9af8a23

Browse files
Rollup merge of rust-lang#38346 - GuillaumeGomez:duration_doc, r=frewsxcv
Duration doc r? @frewsxcv
2 parents 5f1f0e1 + 60fbe7a commit 9af8a23

File tree

1 file changed

+66
-13
lines changed

1 file changed

+66
-13
lines changed

src/libstd/time/duration.rs

+66-13
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ const NANOS_PER_SEC: u32 = 1_000_000_000;
1414
const NANOS_PER_MILLI: u32 = 1_000_000;
1515
const MILLIS_PER_SEC: u64 = 1_000;
1616

17-
/// A duration type to represent a span of time, typically used for system
17+
/// A `Duration` type to represent a span of time, typically used for system
1818
/// timeouts.
1919
///
20-
/// Each duration is composed of a number of seconds and nanosecond precision.
20+
/// Each `Duration` is composed of a number of seconds and nanosecond precision.
2121
/// APIs binding a system timeout will typically round up the nanosecond
2222
/// precision if the underlying system does not support that level of precision.
2323
///
24-
/// Durations implement many common traits, including `Add`, `Sub`, and other
25-
/// ops traits. Currently a duration may only be inspected for its number of
26-
/// seconds and its nanosecond precision.
24+
/// `Duration`s implement many common traits, including [`Add`], [`Sub`], and other
25+
/// [`ops`] traits.
26+
///
27+
/// [`Add`]: ../../std/ops/trait.Add.html
28+
/// [`Sub`]: ../../std/ops/trait.Sub.html
29+
/// [`ops`]: ../../std/ops/index.html
2730
///
2831
/// # Examples
2932
///
@@ -56,6 +59,14 @@ impl Duration {
5659
///
5760
/// This constructor will panic if the carry from the nanoseconds overflows
5861
/// the seconds counter.
62+
///
63+
/// # Examples
64+
///
65+
/// ```
66+
/// use std::time::Duration;
67+
///
68+
/// let five_seconds = Duration::new(5, 0);
69+
/// ```
5970
#[stable(feature = "duration", since = "1.3.0")]
6071
#[inline]
6172
pub fn new(secs: u64, nanos: u32) -> Duration {
@@ -66,13 +77,29 @@ impl Duration {
6677
}
6778

6879
/// Creates a new `Duration` from the specified number of seconds.
80+
///
81+
/// # Examples
82+
///
83+
/// ```
84+
/// use std::time::Duration;
85+
///
86+
/// let five_seconds = Duration::from_secs(5);
87+
/// ```
6988
#[stable(feature = "duration", since = "1.3.0")]
7089
#[inline]
7190
pub fn from_secs(secs: u64) -> Duration {
7291
Duration { secs: secs, nanos: 0 }
7392
}
7493

7594
/// Creates a new `Duration` from the specified number of milliseconds.
95+
///
96+
/// # Examples
97+
///
98+
/// ```
99+
/// use std::time::Duration;
100+
///
101+
/// let five_seconds = Duration::from_millis(5000);
102+
/// ```
76103
#[stable(feature = "duration", since = "1.3.0")]
77104
#[inline]
78105
pub fn from_millis(millis: u64) -> Duration {
@@ -81,26 +108,46 @@ impl Duration {
81108
Duration { secs: secs, nanos: nanos }
82109
}
83110

84-
/// Returns the number of whole seconds represented by this duration.
111+
/// Returns the number of whole seconds represented by this `Duration`.
85112
///
86113
/// The extra precision represented by this duration is ignored (i.e. extra
87114
/// nanoseconds are not represented in the returned value).
115+
///
116+
/// # Examples
117+
///
118+
/// ```
119+
/// use std::time::Duration;
120+
///
121+
/// let five_seconds = Duration::new(5, 0);
122+
/// assert_eq!(five_seconds.as_secs(), 5);
123+
/// ```
88124
#[stable(feature = "duration", since = "1.3.0")]
89125
#[inline]
90126
pub fn as_secs(&self) -> u64 { self.secs }
91127

92-
/// Returns the nanosecond precision represented by this duration.
128+
/// Returns the nanosecond precision represented by this `Duration`.
93129
///
94130
/// This method does **not** return the length of the duration when
95131
/// represented by nanoseconds. The returned number always represents a
96132
/// fractional portion of a second (i.e. it is less than one billion).
133+
///
134+
/// # Examples
135+
///
136+
/// ```
137+
/// use std::time::Duration;
138+
///
139+
/// let duration = Duration::from_millis(5010);
140+
/// assert_eq!(duration.subsec_nanos(), 10000000);
141+
/// ```
97142
#[stable(feature = "duration", since = "1.3.0")]
98143
#[inline]
99144
pub fn subsec_nanos(&self) -> u32 { self.nanos }
100145

101-
/// Checked duration addition. Computes `self + other`, returning `None`
146+
/// Checked `Duration` addition. Computes `self + other`, returning [`None`]
102147
/// if overflow occurred.
103148
///
149+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
150+
///
104151
/// # Examples
105152
///
106153
/// Basic usage:
@@ -136,9 +183,11 @@ impl Duration {
136183
}
137184
}
138185

139-
/// Checked duration subtraction. Computes `self + other`, returning `None`
186+
/// Checked `Duration` subtraction. Computes `self - other`, returning [`None`]
140187
/// if the result would be negative or if underflow occurred.
141188
///
189+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
190+
///
142191
/// # Examples
143192
///
144193
/// Basic usage:
@@ -172,8 +221,10 @@ impl Duration {
172221
}
173222
}
174223

175-
/// Checked duration multiplication. Computes `self * other`, returning
176-
/// `None` if underflow or overflow occurred.
224+
/// Checked `Duration` multiplication. Computes `self * other`, returning
225+
/// [`None`] if overflow occurred.
226+
///
227+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
177228
///
178229
/// # Examples
179230
///
@@ -207,8 +258,10 @@ impl Duration {
207258
}
208259
}
209260

210-
/// Checked duration division. Computes `self / other`, returning `None`
211-
/// if `other == 0` or the operation results in underflow or overflow.
261+
/// Checked `Duration` division. Computes `self / other`, returning [`None`]
262+
/// if `other == 0`.
263+
///
264+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
212265
///
213266
/// # Examples
214267
///

0 commit comments

Comments
 (0)