Skip to content

Improve std::time::Instant documentation - OS behavior #79881

Closed
@jnqnfe

Description

@jnqnfe

The following piece of documentation from std::time::Instant, which comes from PR #72836 via issue #48980, does not make sufficient sense and is not sufficiently helpful:

/// # OS-specific behaviors
///
/// An `Instant` is a wrapper around system-specific types and it may behave
/// differently depending on the underlying operating system. For example,
/// the following snippet is fine on Linux but panics on macOS:
///
/// ```no_run
/// use std::time::{Instant, Duration};
///
/// let now = Instant::now();
/// let max_nanoseconds = u64::MAX / 1_000_000_000;
/// let duration = Duration::new(max_nanoseconds, 0);
/// println!("{:?}", now + duration);
/// ```

The parameters to Duration::new() are the number of seconds and the number of nanoseconds. Here we seem to be giving a quantity in nanoseconds as the number of seconds, and with that quantity representing... well, from the name max_nanoseconds and calculation, the maximum number of seconds that can fit into a u64 if it later it is to be converted to a u64 quantity of nanoseconds without overflowing... huh :S The reviewers of PR #72836 seem to have overlooked this problem.

This text and example are there due to the fact that the internal attributes of Instant differ per different platforms, and so a large Duration added to an Instant on one platform might work, while on another it might overflow and thus panic.

Here it follows that u64::MAX / 1_000_000_000 as the number of seconds in a Duration just happens to be too large for addition to an Instant on some platforms - macOS. The problem with the example fundamentally is the misleading variable name and possibly poor choice of chosen value.

One further problem with the text is that although it is helpful in pointing out that there is a difference in behaviour, it leaves the reader to have to guess at what the cause of the different behaviour is, and it does not give any indication of what range of values can typically be added to it without encountering failure.

I don't know why Instant has been designed to use different underlying attributes rather than something consistent which would avoid it suffering from this unfortunate inconsistency, but I'll leave that for separate discussion / resolution, which already seems to have gone no-where in #48980, though #44394 for SystemTime is still open.

Tasks to address this issue:

  • Replace the code example with code that makes sense.
  • Improve the text to better explain the cause of the difference.
  • Improve the text to give guidance on what approximate size of values will trigger failure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-timeArea: TimeC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions