nix icon indicating copy to clipboard operation
nix copied to clipboard

Shouldn't TimeSpec::from_duration preserve monotonicity?

Open sirhcel opened this issue 1 year ago • 4 comments
trafficstars

When creating a TimeSpec from a std::time::Duration with more than libc::time_t seconds, the resulting value might be negative and will no longer preserve monotonicity with respect to the input duration. This happens to the integer casting in TimeSpec::from_duration.

See this example on Rust Playground.

I learned about this behavior through errors for negative timeouts from ppoll on Linux at runtime (https://github.com/serialport/serialport-rs/issues/207). I did not expect this for the "porcellain" type Duration and the docs did not hint at it. And likely other users of the "porcellain" as well. When manually converting to the "plumbing" typetime_t I wold have looked deeper into the nitty gritty integer casts and cared for this case by saturating.

So shouldn't TimeSpec::from_duration preserve monotonicity? Isn't an inaccurate but still large TimeSpec less surprising?

sirhcel avatar Sep 01 '24 15:09 sirhcel