[][src]Macro duration_macro::duration

macro_rules! duration {
    ($something:tt $name:ident $($something2:tt $name2:ident)+) => { ... };
    ($nanoseconds:tt ns) => { ... };
    ($milliseconds:tt ms) => { ... };
    ($microseconds:tt us) => { ... };
    ($seconds:tt s) => { ... };
    ($minutes:tt m) => { ... };
    ($hours:tt h) => { ... };
    ($days:tt d) => { ... };
    ($days:tt day) => { ... };
    ($days:tt days) => { ... };
}

Compile-time duration parsing macro.

The macro accepts duration in a form of {block} {unit} [{block} {unit} ...], where {block} is a "token tree", and {unit} is one of the following literals:

  • d or day or days for days,
  • s for seconds,
  • ms for milliseconds,
  • us for microseconds,
  • ns for nanoseconds.
use core::time::Duration;
use duration_macro::duration;

let reference_duration = Duration::new(86400 + 3600 * 2 + 3 * 60 + 4, 5 * 1_000_000 + 6 * 1_000 + 7);
assert_eq!(
    duration!(1 d 2 h 3 m 4 s 5 ms 6 us 7 ns),
    reference_duration
);
// The order doesn't matter!
assert_eq!(
    duration!(6 us 2 h 3 m 4 s 1 d 5 ms 7 ns),
    reference_duration
);
// Expressions can be passed using `{..}` blocks:
assert_eq!(
    duration!({3 * 2} ns {7 / 2} d),
    Duration::from_nanos(6) + Duration::from_secs(3 * 86400)
);