Skip to main content

decode_duration

Function decode_duration 

Source
pub const fn decode_duration(encoded: u128) -> Duration
Expand description

Decodes a u128 into a Duration.

Accepts non-canonical input without panicking. The encoding produced by encode_duration stores 64 bits of seconds in bits 32..=95 and at most 30 bits of nanoseconds (the valid range 0..1_000_000_000) in bits 0..=31. If the decoded bits carry a nanosecond count of 10⁹ or more — which encode_duration never produces, but which can appear when the encoded value comes from corrupted storage or untrusted input — the extra whole seconds are folded into the seconds field; if that push past u64::MAX, the result saturates at Duration::MAX.

This means decode_duration(u128::MAX) yields a saturated Duration rather than panicking as the previous implementation did.