Negative Duration

Hello. I have read conversation about duration at Duration::zero() -> Duration::ZERO by workingjubilee · Pull Request #78216 · rust-lang/rust · GitHub and I would like to share some ideas how to improve it.

I see two usecases for Duration

  1. I need some value for thread::sleep and similar methods - clearly negative duration makes no sense there and should

    1. be impossible
    2. cause panic
    3. behave as zero duration (e.g. this actually happen in golang)
  2. And there is another kind of duration, when I want calculate difference between two timestamps surely here I need negative durations. The problem of MIN duration can be solved the same way as for real numbers. There is smallest positive non zero value, which could be applied to duration too if needed, and similar value exists for negative real number - so could be also for duration - but for "smallest negative" duration (negative and closest to zero) I see no usecase.

These two usecases can be covered by single type in case of 1.1. or 1.3. (preferably 1.3.) or by two types, in case of 1.1.

To prevent BC break, another type should be introduced, e.g. TimeDifference with some nice conversion to Duration and back.

What's wrong with using crates here?

1 Like

Good question. Actually it did not occur to me that it does not have to be part of standard library.

1 Like