You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tracking issue for 'checked_duration_since' feature #58402
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.
changed the title [-]No non-panicking analogue for Instant::duration_since[/-][+]Tracking issue for 'checked_duration_since' feature[/+]on Feb 17, 2019
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:
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_lothere 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:
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.
Activity
dtolnay commentedon Feb 15, 2019
Some use cases from @vi:
[-]No non-panicking analogue for Instant::duration_since[/-][+]Tracking issue for 'checked_duration_since' feature[/+]vi commentedon Feb 20, 2019
faern commentedon Mar 22, 2019
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 adeadline: Instanthanded to me, and I need to figure out how far into the future it is without risking a panic:faern commentedon Apr 3, 2019
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 byparking_lothere 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 likechecked_duration_sincedoes.In the example posted above, regarding
saturating_duration_since:The author hint that they would like to shorten it to:
But it feels wasteful* to call sleep when we don't want to sleep, and I would personally have used:
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.meansstd::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.Stabilize checked_duration_since for 1.38.0
Rollup merge of rust-lang#62860 - vi:stabilize_checked_duration_since…
alexcrichton commentedon Sep 4, 2019
Removing nomination since this is covered by #62860
Rollup merge of rust-lang#62860 - vi:stabilize_checked_duration_since…
Rollup merge of rust-lang#62860 - vi:stabilize_checked_duration_since…
Rollup merge of rust-lang#69561 - JohnTitor:clean-up-unstable-book, r…
Rollup merge of rust-lang#69561 - JohnTitor:clean-up-unstable-book, r…