316. checked_duration_since — Your Deadline Passed, and Instant Subtraction Said Zero
This morning’s bite 315 said Instant has no Result to handle. True — but its subtraction hides a different trap: past a deadline, deadline - now is silently 0, and your retry loop spins forever with a zero timeout.
Since Rust 1.60, subtracting Instants in the wrong order doesn’t panic — it saturates to zero:
| |
That’s kind to platform clock bugs, but cruel to deadline code. The classic pattern computes the remaining budget each iteration:
| |
Zero never looks like “we’re done” — it looks like “no time elapsed”. Worse, set_read_timeout(Some(Duration::ZERO)) is an error on std sockets, and other APIs treat zero as infinite wait.
checked_duration_since tells the truth
None means the deadline is behind you — an unmissable, type-checked “time’s up”:
| |
| |
And when clamping to zero is what you want, say it explicitly with saturating_duration_since — same behavior as the bare subtraction, but the next reader knows it’s deliberate:
| |
Same trio as integer arithmetic — bare op, checked_*, saturating_* — just on the clock. Pick the one that says what you mean.