pub trait DurationExt {
// Required methods
fn seconds(self) -> Duration;
fn minutes(self) -> Duration;
fn hours(self) -> Duration;
fn milliseconds(self) -> Duration;
fn microseconds(self) -> Duration;
fn nanoseconds(self) -> Duration;
}Expand description
An extension trait that adds fluent time unit methods to integer primitives, allowing for highly readable time duration creation.
This crate is optimized for system timing (timeouts, sleeps, fixed cache TTLs). It explicitly excludes methods for units longer than hours (days, weeks) to prevent calendar errors related to Daylight Saving Time (DST) and time zones.
§Panics
- Signed integers (
i32,i64) panic if the value is negative. - Overflow panics for
.minutes()and.hours()when the resulting seconds exceedu64::MAX.
§Examples
use duration_extender::DurationExt;
use std::time::Duration;
let timeout = 10.seconds();
let delay = 5.minutes();
// For a fixed 2-day duration, use the hour equivalent:
let fixed_long_wait = (2 * 24).hours();
let total_time = 2.hours() + 30.minutes() + 15.seconds();
// Signed integers must be non-negative
let elapsed = 100.seconds(); // ✅ Works
// let bad = (-100).seconds(); // ❌ Panics!Required Methods§
Sourcefn milliseconds(self) -> Duration
fn milliseconds(self) -> Duration
Creates a Duration representing this many milliseconds.
Sourcefn microseconds(self) -> Duration
fn microseconds(self) -> Duration
Creates a Duration representing this many microseconds.
Sourcefn nanoseconds(self) -> Duration
fn nanoseconds(self) -> Duration
Creates a Duration representing this many nanoseconds.