@@ -14,16 +14,19 @@ const NANOS_PER_SEC: u32 = 1_000_000_000;
14
14
const NANOS_PER_MILLI : u32 = 1_000_000 ;
15
15
const MILLIS_PER_SEC : u64 = 1_000 ;
16
16
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
18
18
/// timeouts.
19
19
///
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.
21
21
/// APIs binding a system timeout will typically round up the nanosecond
22
22
/// precision if the underlying system does not support that level of precision.
23
23
///
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
27
30
///
28
31
/// # Examples
29
32
///
@@ -56,6 +59,14 @@ impl Duration {
56
59
///
57
60
/// This constructor will panic if the carry from the nanoseconds overflows
58
61
/// the seconds counter.
62
+ ///
63
+ /// # Examples
64
+ ///
65
+ /// ```
66
+ /// use std::time::Duration;
67
+ ///
68
+ /// let five_seconds = Duration::new(5, 0);
69
+ /// ```
59
70
#[ stable( feature = "duration" , since = "1.3.0" ) ]
60
71
#[ inline]
61
72
pub fn new ( secs : u64 , nanos : u32 ) -> Duration {
@@ -66,13 +77,29 @@ impl Duration {
66
77
}
67
78
68
79
/// 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
+ /// ```
69
88
#[ stable( feature = "duration" , since = "1.3.0" ) ]
70
89
#[ inline]
71
90
pub fn from_secs ( secs : u64 ) -> Duration {
72
91
Duration { secs : secs, nanos : 0 }
73
92
}
74
93
75
94
/// 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
+ /// ```
76
103
#[ stable( feature = "duration" , since = "1.3.0" ) ]
77
104
#[ inline]
78
105
pub fn from_millis ( millis : u64 ) -> Duration {
@@ -81,26 +108,46 @@ impl Duration {
81
108
Duration { secs : secs, nanos : nanos }
82
109
}
83
110
84
- /// Returns the number of whole seconds represented by this duration .
111
+ /// Returns the number of whole seconds represented by this `Duration` .
85
112
///
86
113
/// The extra precision represented by this duration is ignored (i.e. extra
87
114
/// 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
+ /// ```
88
124
#[ stable( feature = "duration" , since = "1.3.0" ) ]
89
125
#[ inline]
90
126
pub fn as_secs ( & self ) -> u64 { self . secs }
91
127
92
- /// Returns the nanosecond precision represented by this duration .
128
+ /// Returns the nanosecond precision represented by this `Duration` .
93
129
///
94
130
/// This method does **not** return the length of the duration when
95
131
/// represented by nanoseconds. The returned number always represents a
96
132
/// 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
+ /// ```
97
142
#[ stable( feature = "duration" , since = "1.3.0" ) ]
98
143
#[ inline]
99
144
pub fn subsec_nanos ( & self ) -> u32 { self . nanos }
100
145
101
- /// Checked duration addition. Computes `self + other`, returning `None`
146
+ /// Checked `Duration` addition. Computes `self + other`, returning [ `None`]
102
147
/// if overflow occurred.
103
148
///
149
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
150
+ ///
104
151
/// # Examples
105
152
///
106
153
/// Basic usage:
@@ -136,9 +183,11 @@ impl Duration {
136
183
}
137
184
}
138
185
139
- /// Checked duration subtraction. Computes `self + other`, returning `None`
186
+ /// Checked `Duration` subtraction. Computes `self - other`, returning [ `None`]
140
187
/// if the result would be negative or if underflow occurred.
141
188
///
189
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
190
+ ///
142
191
/// # Examples
143
192
///
144
193
/// Basic usage:
@@ -172,8 +221,10 @@ impl Duration {
172
221
}
173
222
}
174
223
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
177
228
///
178
229
/// # Examples
179
230
///
@@ -207,8 +258,10 @@ impl Duration {
207
258
}
208
259
}
209
260
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
212
265
///
213
266
/// # Examples
214
267
///
0 commit comments