Trait mz_ore::time::DurationExt

source ·
pub trait DurationExt {
    // Required methods
    fn try_from_secs_i64(secs: i64) -> Result<Duration, DurationError>;
    fn saturating_mul_f64(&self, rhs: f64) -> Duration;
}
Expand description

Extensions for std::time::Duration.

Required Methods§

source

fn try_from_secs_i64(secs: i64) -> Result<Duration, DurationError>

Creates a Duration from the specified number of seconds represented as an i64.

Returns an error if the provided number of seconds is negative.

use std::time::Duration;
use mz_ore::time::DurationExt;

// Negative numbers will fail.
assert!(Duration::try_from_secs_i64(-100).is_err());

// i64 max works.
assert!(Duration::try_from_secs_i64(i64::MAX).is_ok());
source

fn saturating_mul_f64(&self, rhs: f64) -> Duration

Saturating Duration multiplication. Computes self * rhs, saturating at the numeric bounds instead of overflowing.

use std::time::Duration;
use mz_ore::time::DurationExt;

let one = Duration::from_secs(1);
assert_eq!(one.saturating_mul_f64(f64::INFINITY), Duration::from_secs(u64::MAX));

assert_eq!(one.saturating_mul_f64(f64::NEG_INFINITY), Duration::from_secs(0));
assert_eq!(one.saturating_mul_f64(f64::NAN), Duration::from_secs(0));
assert_eq!(one.saturating_mul_f64(-0.0), Duration::from_secs(0));
assert_eq!(one.saturating_mul_f64(0.0), Duration::from_secs(0));

assert_eq!(Duration::from_secs(20).saturating_mul_f64(0.1), Duration::from_secs(2));

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl DurationExt for Duration

Implementors§