[go: up one dir, main page]

[][src]Trait time::ext::NumericalStdDuration

pub trait NumericalStdDuration {
    pub fn std_nanoseconds(self) -> StdDuration;
pub fn std_microseconds(self) -> StdDuration;
pub fn std_milliseconds(self) -> StdDuration;
pub fn std_seconds(self) -> StdDuration;
pub fn std_minutes(self) -> StdDuration;
pub fn std_hours(self) -> StdDuration;
pub fn std_days(self) -> StdDuration;
pub fn std_weeks(self) -> StdDuration; }

Create std::time::Durations from primitive and core numeric types.

This trait can be imported (alongside others) with use time::prelude::*.

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

Examples

Basic construction of std::time::Durations.

assert_eq!(5.std_nanoseconds(), Duration::from_nanos(5));
assert_eq!(5.std_microseconds(), Duration::from_micros(5));
assert_eq!(5.std_milliseconds(), Duration::from_millis(5));
assert_eq!(5.std_seconds(), Duration::from_secs(5));
assert_eq!(5.std_minutes(), Duration::from_secs(5 * 60));
assert_eq!(5.std_hours(), Duration::from_secs(5 * 3_600));
assert_eq!(5.std_days(), Duration::from_secs(5 * 86_400));
assert_eq!(5.std_weeks(), Duration::from_secs(5 * 604_800));

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

assert_eq!(
    2.std_seconds() + 500.std_milliseconds(),
    2_500.std_milliseconds()
);
assert_eq!(
    2.std_seconds() - 500.std_milliseconds(),
    1_500.std_milliseconds()
);

When called on floating point values, any remainder of the floating point value will be truncated. Keep in mind that floating point numbers are inherently imprecise and have limited capacity.

Required methods

pub fn std_nanoseconds(self) -> StdDuration[src]

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

pub fn std_microseconds(self) -> StdDuration[src]

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

pub fn std_milliseconds(self) -> StdDuration[src]

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

pub fn std_seconds(self) -> StdDuration[src]

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

pub fn std_minutes(self) -> StdDuration[src]

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

pub fn std_hours(self) -> StdDuration[src]

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

pub fn std_days(self) -> StdDuration[src]

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

pub fn std_weeks(self) -> StdDuration[src]

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

Loading content...

Implementations on Foreign Types

impl NumericalStdDuration for u8[src]

impl NumericalStdDuration for u16[src]

impl NumericalStdDuration for u32[src]

impl NumericalStdDuration for u64[src]

impl NumericalStdDuration for NonZeroU8[src]

impl NumericalStdDuration for NonZeroU16[src]

impl NumericalStdDuration for NonZeroU32[src]

impl NumericalStdDuration for NonZeroU64[src]

impl NumericalStdDuration 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.

impl NumericalStdDuration for f64[src]

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

Loading content...

Implementors

Loading content...