Skip to main content

FormattedDuration

Struct FormattedDuration 

Source
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: Duration

Trait Implementations§

Source§

impl Display for FormattedDuration

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V