pub struct FormattedDuration(pub Duration);Expand description
A Duration wrapper whose Display renders a timer
interval as <unit>/<count>, choosing the coarsest unit that represents the
interval exactly.
This is the inverse of how a dora/timer/<unit>/<count> input is parsed, so
the rendered string round-trips back to the same Duration. Picking the
coarsest exact unit matters: rendering a sub-millisecond interval as
millis/0 would parse back to a zero-length (busy-loop) timer
(dora-rs#2031).
Usually constructed via format_duration.
use std::time::Duration;
use dora_message::config::format_duration;
// Coarsest exact unit is chosen: whole seconds render as `secs`.
assert_eq!(format_duration(Duration::from_secs(2)).to_string(), "secs/2");
assert_eq!(format_duration(Duration::from_millis(100)).to_string(), "millis/100");
// 1500 ms is not a whole number of seconds, so it stays in `millis`.
assert_eq!(format_duration(Duration::from_millis(1500)).to_string(), "millis/1500");
// Sub-millisecond intervals keep their precision instead of truncating to 0.
assert_eq!(format_duration(Duration::from_micros(500)).to_string(), "micros/500");
assert_eq!(format_duration(Duration::from_nanos(333_333)).to_string(), "nanos/333333");Tuple Fields§
§0: DurationTrait Implementations§
Auto Trait Implementations§
impl Freeze for FormattedDuration
impl RefUnwindSafe for FormattedDuration
impl Send for FormattedDuration
impl Sync for FormattedDuration
impl Unpin for FormattedDuration
impl UnsafeUnpin for FormattedDuration
impl UnwindSafe for FormattedDuration
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more