Skip to content

Tracking issue for 'checked_duration_since' feature #58402

Closed

Description

@vi

There going to be checked analogues of offseting Instants by Durations, but I see no analogue of non-panicking calculation of Duration from a pair of Instants.

Note: this issue is referred by #[unstable] in the pull request.

Activity

  1. dtolnay commented on Feb 15, 2019

    @dtolnay
    Member

    Some use cases from @vi:

    Most recent example from my code:

    let rtt4 = if now >= send_time { now - send_time } else { Duration::from_secs(999)};

    now.saturating_duration_since(send_time) would probably be OK for this, or a checked_duration_since with more explicit fallback.

    The comparison was added when I suddenly encountered a panic.

    A close call nearby:

    if now < deadline {
        sleeper.sleep(deadline - now);
    }

    Although readable as is, using saturating_duration_since would be almost equally good.

    That comparison is easy to miss, leading to panic when under load. Seeing checked/saturating analogues nearby in documentation puts one in "here be dragonsoverflows" mode and suggests care.

  2. changed the title [-]No non-panicking analogue for Instant::duration_since[/-] [+]Tracking issue for 'checked_duration_since' feature[/+] on Feb 17, 2019
  3. vi commented on Feb 20, 2019

    @vi
    Author
  4. added
    B-unstableBlocker: Implemented in the nightly compiler and unstable.
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    and removed
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    on Feb 20, 2019
  5. faern commented on Mar 22, 2019

    @faern
    Contributor

    I just had good use for Instant::checked_duration_since. It's a good method to have when working with deadlines, as compared to timeouts. I get a deadline: Instant handed to me, and I need to figure out how far into the future it is without risking a panic:

    deadline.checked_duration_since(Instant::now())
    
  6. faern commented on Apr 3, 2019

    @faern
    Contributor

    I do like checked_duration_since, and I would like that function stabilized. It both lowers the amount of code needed and lowers the risk of screwing up some check and ending up with a panic. It's already used by parking_lot here and here. And it can use it in even more places when it's stable.

    However, I personally don't see the benefit of saturating_duration_since. It's a small convenience, but it does not help the user avoid panics like checked_duration_since does.

    In the example posted above, regarding saturating_duration_since:

    if now < deadline {
        sleeper.sleep(deadline - now);
    }

    The author hint that they would like to shorten it to:

    sleeper.sleep(deadline.saturating_duration_since(now));

    But it feels wasteful* to call sleep when we don't want to sleep, and I would personally have used:

    if let Some(time_left) = deadline.checked_duration_since(now) {
        sleeper.sleep(time_left);
    }

    If someone really want a saturating computation it's not that many extra characters to type deadline.checked_duration_since(now).unwrap_or(Duration::new(0, 0)).

    *: Assuming sleeper. means std::thread::, some platforms (Windows and CloudABI from what I can see) don't have any check if the duration is non-zero, and will actually perform the syscall even if the duration is zero.

  7. added a commit that references this issue on Jul 21, 2019
    01d9e57
  8. added a commit that references this issue on Aug 9, 2019
    1b3e7fb
  9. alexcrichton commented on Sep 4, 2019

    @alexcrichton
    Member

    Removing nomination since this is covered by #62860

  10. added 2 commits that reference this issue on Sep 5, 2019
    f062076
    ee437eb
  11. added 2 commits that reference this issue on Mar 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions