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) => { ... };
}Expand description
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:
dordayordaysfor days,sfor seconds,msfor milliseconds,usfor microseconds,nsfor 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)
);