[][src]Trait time::NumericalStdDurationShort

pub trait NumericalStdDurationShort {
    fn nanoseconds(self) -> StdDuration;
fn microseconds(self) -> StdDuration;
fn milliseconds(self) -> StdDuration;
fn seconds(self) -> StdDuration;
fn minutes(self) -> StdDuration;
fn hours(self) -> StdDuration;
fn days(self) -> StdDuration;
fn weeks(self) -> StdDuration; }

Create std::time::Durations from primitive and core numeric types. Unless you are always expecting a std::time::Duration, you should prefer to use NumericalStdDuration for clarity.

Due to limitations in rustc, these methods are currently not const fn. See this RFC for details.

Examples

Basic construction of std::time::Durations.

assert_eq!(5.nanoseconds(), Duration::from_nanos(5));
assert_eq!(5.microseconds(), Duration::from_micros(5));
assert_eq!(5.milliseconds(), Duration::from_millis(5));
assert_eq!(5.seconds(), Duration::from_secs(5));
assert_eq!(5.minutes(), Duration::from_secs(5 * 60));
assert_eq!(5.hours(), Duration::from_secs(5 * 3_600));
assert_eq!(5.days(), Duration::from_secs(5 * 86_400));
assert_eq!(5.weeks(), Duration::from_secs(5 * 604_800));

Just like any other std::time::Duration, they can be added, subtracted, etc.

assert_eq!(2.seconds() + 500.milliseconds(), 2_500.milliseconds());
assert_eq!(2.seconds() - 500.milliseconds(), 1_500.milliseconds());

As floats can not be used to construct via anything other than Duration::from_secs_f32 and Duration::from_secs_f64, floats do not implement NumericalStdDurationShort.

This example deliberately fails to compile
5.0.seconds();

Required methods

fn nanoseconds(self) -> StdDuration

Create a std::time::Duration from the number of nanoseconds.

fn microseconds(self) -> StdDuration

Create a std::time::Duration from the number of microseconds.

fn milliseconds(self) -> StdDuration

Create a std::time::Duration from the number of milliseconds.

fn seconds(self) -> StdDuration

Create a std::time::Duration from the number of seconds.

fn minutes(self) -> StdDuration

Create a std::time::Duration from the number of minutes.

fn hours(self) -> StdDuration

Create a std::time::Duration from the number of hours.

fn days(self) -> StdDuration

Create a std::time::Duration from the number of days.

fn weeks(self) -> StdDuration

Create a std::time::Duration from the number of weeks.

Loading content...

Implementations on Foreign Types

impl NumericalStdDurationShort for u8[src]

impl NumericalStdDurationShort for u16[src]

impl NumericalStdDurationShort for u32[src]

impl NumericalStdDurationShort for u64[src]

impl NumericalStdDurationShort for NonZeroU8[src]

impl NumericalStdDurationShort for NonZeroU16[src]

impl NumericalStdDurationShort for NonZeroU32[src]

impl NumericalStdDurationShort for NonZeroU64[src]

impl NumericalStdDurationShort for i32[src]

Implement on i32 because that's the default type for integers. This performs a runtime check and panics if the value is negative.

Loading content...

Implementors

Loading content...