duration-extender
A lightweight, zero-dependency Rust crate that extends primitive integer types with intuitive methods for creating std::time::Duration objects.
Write expressive, human-readable code for timeouts, delays, and schedules without the verbosity of Duration::from_secs().
⚠️ What's New in v0.5.0
Added f64 and f32 support for fractional durations!
// New in v0.5.0 - Fractional durations
let half_second = 0.5.seconds;
let two_and_half_minutes = 2.5.minutes;
// Still works - Integer durations
let timeout = 30.seconds;
let delay = 5.minutes;
Thanks to @oconnor663 for the suggestion!
⚠️ Breaking Changes in v0.4.0
.days()and.weeks()methods are removed.
// ❌ Not allowed anymore
// let week = 1.weeks();
// ✅ Use hours instead
let week = .hours;
⚠️ Breaking Changes in v0.3.0
Overflow values now panic for all integer types, in addition to negative values panicking for signed integers.
Previously in v0.2.0:
.minutes // ❌ Panics
u64MAX.minutes // ❌ Panics
Now in v0.3.0:
5.minutes // ✅ Works fine
.minutes // ❌ Panics: "duration cannot be negative: got -5 minutes"
u64MAX.minutes // ❌ Panics: "duration value ... overflows u64 seconds capacity"
This makes the behavior fully explicit and safe. See CHANGELOG.md for full details.
Before:
let timeout = from_secs;
let delay = from_secs;
let cache_ttl = from_secs;
After:
let timeout = 30.seconds;
let delay = 5.minutes;
let cache_ttl = 24.hours;
Features
- Fluent API — Natural, readable syntax for duration creation
- Type-safe — Works with
u64,u32,i64,i32,f64, andf32 - Explicit errors — Panics on overflow and negative values with clear messages
- Zero dependencies — Only the standard library
- Minimal overhead — Compiles down to the same code as manual duration creation
Installation
Add this to your Cargo.toml:
[]
= "0.5"
Usage
Import the DurationExt trait to unlock duration methods on integers:
use DurationExt;
use Duration;
Real-World Examples
HTTP client timeout:
let client = builder
.timeout
.build?;
Tokio sleep:
sleep.await;
Cache expiration:
cache.insert_with_ttl;
Rate limiting:
let rate_limit = new;
Fractional timeouts:
let timeout = 1.5.seconds; // 1500 milliseconds
let delay = 0.5.minutes; // 30 seconds
Available Methods
| Method | Equivalent |
|---|---|
.seconds() |
Duration::from_secs(n) |
.minutes() |
Duration::from_secs(n * 60) |
.hours() |
Duration::from_secs(n * 3600) |
.milliseconds() |
Duration::from_millis(n) |
.microseconds() |
Duration::from_micros(n) |
.nanoseconds() |
Duration::from_nanos(n) |
Supported Types
The DurationExt trait is implemented for:
u64andu32— Direct conversioni64andi32— Panics on negative values to prevent bugsf64andf32(NEW in v0.5.0) — For fractional durations like0.5.seconds()
All operations use checked arithmetic to prevent silent overflow.
Safety Guarantees
- Overflow checked — Panics on overflow with a clear message
- Negative handling — Signed integers and floats panic on negative values with clear error messages
- NaN/Infinity handling — Float types panic on NaN and infinity (handled by
std::time::Duration) - Type safety — Uses Rust's strong type system for compile-time correctness
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
Dual-licensed under:
Choose whichever license suits your needs.
Acknowledgments
Inspired by ergonomic duration APIs in other ecosystems and refined by community feedback from @oconnor663, @burntsushi, @nik-rev, and others.