Skip to content

Commit 376d955

Browse files
committed
Add panic docs describing old, current and possible future behavior
1 parent bda2693 commit 376d955

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

library/std/src/time.rs

+31-3
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,12 @@ pub use core::time::FromFloatSecsError;
133133
/// if available, which is the case for all [tier 1] platforms.
134134
/// In practice such guarantees are – under rare circumstances – broken by hardware, virtualization
135135
/// or operating system bugs. To work around these bugs and platforms not offering monotonic clocks
136-
/// [`duration_since`], [`elapsed`] and [`sub`] saturate to zero. [`checked_duration_since`] can
137-
/// be used to detect and handle situations where monotonicity is violated, or `Instant`s are
138-
/// subtracted in the wrong order.
136+
/// [`duration_since`], [`elapsed`] and [`sub`] saturate to zero. In older rust versions this
137+
/// lead to a panic instead. [`checked_duration_since`] can be used to detect and handle situations
138+
/// where monotonicity is violated, or `Instant`s are subtracted in the wrong order.
139+
///
140+
/// This workaround obscures programming errors where earlier and later instants are accidentally
141+
/// swapped. For this reason future rust versions may reintroduce panics.
139142
///
140143
/// [tier 1]: https://doc.rust-lang.org/rustc/platform-support.html
141144
/// [`duration_since`]: Instant::duration_since
@@ -271,6 +274,13 @@ impl Instant {
271274
/// Returns the amount of time elapsed from another instant to this one,
272275
/// or zero duration if that instant is later than this one.
273276
///
277+
/// # Panics
278+
///
279+
/// Previous rust versions panicked when `earlier` was later than `self`. Currently this
280+
/// method saturates. Future versions may reintroduce the panic in some circumstances.
281+
/// See [Monotonicity].
282+
///
283+
/// [Monotonicity]: Instant#monotonicity
274284
///
275285
/// # Examples
276286
///
@@ -339,6 +349,14 @@ impl Instant {
339349

340350
/// Returns the amount of time elapsed since this instant was created.
341351
///
352+
/// # Panics
353+
///
354+
/// Previous rust versions panicked when self was earlier than the current time. Currently this
355+
/// method returns a Duration of zero in that case. Future versions may reintroduce the panic.
356+
/// See [Monotonicity].
357+
///
358+
/// [Monotonicity]: Instant#monotonicity
359+
///
342360
/// # Examples
343361
///
344362
/// ```no_run
@@ -413,6 +431,16 @@ impl SubAssign<Duration> for Instant {
413431
impl Sub<Instant> for Instant {
414432
type Output = Duration;
415433

434+
/// Returns the amount of time elapsed from another instant to this one,
435+
/// or zero duration if that instant is later than this one.
436+
///
437+
/// # Panics
438+
///
439+
/// Previous rust versions panicked when `other` was later than `self`. Currently this
440+
/// method saturates. Future versions may reintroduce the panic in some circumstances.
441+
/// See [Monotonicity].
442+
///
443+
/// [Monotonicity]: Instant#monotonicity
416444
fn sub(self, other: Instant) -> Duration {
417445
self.duration_since(other)
418446
}

0 commit comments

Comments
 (0)