Skip to main content

NumericalDuration

Trait NumericalDuration 

Source
pub trait NumericalDuration: Sealed {
    // Required methods
    fn nanoseconds(self) -> SignedDuration;
    fn microseconds(self) -> SignedDuration;
    fn milliseconds(self) -> SignedDuration;
    fn seconds(self) -> SignedDuration;
    fn minutes(self) -> SignedDuration;
    fn hours(self) -> SignedDuration;
    fn days(self) -> SignedDuration;
    fn weeks(self) -> SignedDuration;
}
Expand description

Create SignedDurations from numeric literals.

§Examples

Basic construction of SignedDurations.

assert_eq!(5.nanoseconds(), SignedDuration::nanoseconds(5));
assert_eq!(5.microseconds(), SignedDuration::microseconds(5));
assert_eq!(5.milliseconds(), SignedDuration::milliseconds(5));
assert_eq!(5.seconds(), SignedDuration::seconds(5));
assert_eq!(5.minutes(), SignedDuration::minutes(5));
assert_eq!(5.hours(), SignedDuration::hours(5));
assert_eq!(5.days(), SignedDuration::days(5));
assert_eq!(5.weeks(), SignedDuration::weeks(5));

Signed integers work as well!

assert_eq!((-5).nanoseconds(), SignedDuration::nanoseconds(-5));
assert_eq!((-5).microseconds(), SignedDuration::microseconds(-5));
assert_eq!((-5).milliseconds(), SignedDuration::milliseconds(-5));
assert_eq!((-5).seconds(), SignedDuration::seconds(-5));
assert_eq!((-5).minutes(), SignedDuration::minutes(-5));
assert_eq!((-5).hours(), SignedDuration::hours(-5));
assert_eq!((-5).days(), SignedDuration::days(-5));
assert_eq!((-5).weeks(), SignedDuration::weeks(-5));

Just like any other SignedDuration, they can be added, subtracted, etc.

assert_eq!(2.seconds() + 500.milliseconds(), 2_500.milliseconds());
assert_eq!(2.seconds() - 500.milliseconds(), 1_500.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§

Source

fn nanoseconds(self) -> SignedDuration

Create a SignedDuration from the number of nanoseconds.

Source

fn microseconds(self) -> SignedDuration

Create a SignedDuration from the number of microseconds.

Source

fn milliseconds(self) -> SignedDuration

Create a SignedDuration from the number of milliseconds.

Source

fn seconds(self) -> SignedDuration

Create a SignedDuration from the number of seconds.

Source

fn minutes(self) -> SignedDuration

Create a SignedDuration from the number of minutes.

Source

fn hours(self) -> SignedDuration

Create a SignedDuration from the number of hours.

Source

fn days(self) -> SignedDuration

Create a SignedDuration from the number of days.

Source

fn weeks(self) -> SignedDuration

Create a SignedDuration from the number of weeks.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl NumericalDuration for f64

Source§

impl NumericalDuration for i64

Implementors§