[][src]Struct easytime::Duration

pub struct Duration(_);

A Duration type to represent a span of time, typically used for system timeouts.

Each Duration is composed of a whole number of seconds and a fractional part represented in nanoseconds. If the underlying system does not support nanosecond-level precision, APIs binding a system timeout will typically round up the number of nanoseconds.

Durations implement many common traits, including Add, Sub, and other ops traits.

Examples

use easytime::Duration;

let five_seconds = Duration::new(5, 0);
let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5);

assert_eq!(five_seconds_and_five_nanos.as_secs(), Some(5));
assert_eq!(five_seconds_and_five_nanos.subsec_nanos(), Some(5));

let ten_millis = Duration::from_millis(10);

Implementations

impl Duration[src]

pub fn new(secs: u64, nanos: u32) -> Self[src]

Creates a new Duration from the specified number of whole seconds and additional nanoseconds.

If the number of nanoseconds is greater than 1 billion (the number of nanoseconds in a second), then it will carry over into the seconds provided.

Examples

use easytime::Duration;

let five_seconds = Duration::new(5, 0);

pub const fn from_secs(secs: u64) -> Self[src]

Creates a new Duration from the specified number of whole seconds.

Examples

use easytime::Duration;

let duration = Duration::from_secs(5);

assert_eq!(Some(5), duration.as_secs());
assert_eq!(Some(0), duration.subsec_nanos());

pub const fn from_millis(millis: u64) -> Self[src]

Creates a new Duration from the specified number of milliseconds.

Examples

use easytime::Duration;

let duration = Duration::from_millis(2_569);

assert_eq!(Some(2), duration.as_secs());
assert_eq!(Some(569_000_000), duration.subsec_nanos());

pub const fn from_micros(micros: u64) -> Self[src]

Creates a new Duration from the specified number of microseconds.

Examples

use easytime::Duration;

let duration = Duration::from_micros(1_000_002);

assert_eq!(Some(1), duration.as_secs());
assert_eq!(Some(2000), duration.subsec_nanos());

pub const fn from_nanos(nanos: u64) -> Self[src]

Creates a new Duration from the specified number of nanoseconds.

Examples

use easytime::Duration;

let duration = Duration::from_nanos(1_000_000_123);

assert_eq!(Some(1), duration.as_secs());
assert_eq!(Some(123), duration.subsec_nanos());

pub const fn as_secs(&self) -> Option<u64>[src]

Returns the number of whole seconds contained by this Duration.

The returned value does not include the fractional (nanosecond) part of the duration, which can be obtained using subsec_nanos.

Examples

use easytime::Duration;

let duration = Duration::new(5, 730_023_852);
assert_eq!(duration.as_secs(), Some(5));

pub const fn subsec_millis(&self) -> Option<u32>[src]

Returns the fractional part of this Duration, in whole milliseconds.

This method does not return the length of the duration when represented by milliseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one thousand).

Examples

use easytime::Duration;

let duration = Duration::from_millis(5_432);
assert_eq!(duration.as_secs(), Some(5));
assert_eq!(duration.subsec_millis(), Some(432));

pub const fn subsec_micros(&self) -> Option<u32>[src]

Returns the fractional part of this Duration, in whole microseconds.

This method does not return the length of the duration when represented by microseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one million).

Examples

use easytime::Duration;

let duration = Duration::from_micros(1_234_567);
assert_eq!(duration.as_secs(), Some(1));
assert_eq!(duration.subsec_micros(), Some(234_567));

pub const fn subsec_nanos(&self) -> Option<u32>[src]

Returns the fractional part of this Duration, in nanoseconds.

This method does not return the length of the duration when represented by nanoseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one billion).

Examples

use easytime::Duration;

let duration = Duration::from_millis(5_010);
assert_eq!(duration.as_secs(), Some(5));
assert_eq!(duration.subsec_nanos(), Some(10_000_000));

pub const fn as_millis(&self) -> Option<u128>[src]

Returns the total number of whole milliseconds contained by this Duration.

Examples

use easytime::Duration;

let duration = Duration::new(5, 730_023_852);
assert_eq!(duration.as_millis(), Some(5_730));

pub const fn as_micros(&self) -> Option<u128>[src]

Returns the total number of whole microseconds contained by this Duration.

Examples

use easytime::Duration;

let duration = Duration::new(5, 730_023_852);
assert_eq!(duration.as_micros(), Some(5_730_023));

pub const fn as_nanos(&self) -> Option<u128>[src]

Returns the total number of nanoseconds contained by this Duration.

Examples

use easytime::Duration;

let duration = Duration::new(5, 730_023_852);
assert_eq!(duration.as_nanos(), Some(5_730_023_852));

pub fn as_secs_f64(&self) -> Option<f64>[src]

Returns the number of seconds contained by this Duration as f64.

The returned value does include the fractional (nanosecond) part of the duration.

Examples

use easytime::Duration;

let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.as_secs_f64(), Some(2.7));

pub fn as_secs_f32(&self) -> Option<f32>[src]

Returns the number of seconds contained by this Duration as f32.

The returned value does include the fractional (nanosecond) part of the duration.

Examples

use easytime::Duration;

let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.as_secs_f32(), Some(2.7));

pub fn from_secs_f64(secs: f64) -> Self[src]

Creates a new Duration from the specified number of seconds represented as f64.

Examples

use easytime::Duration;

let dur = Duration::from_secs_f64(2.7);
assert_eq!(dur, Duration::new(2, 700_000_000));

pub fn from_secs_f32(secs: f32) -> Duration[src]

Creates a new Duration from the specified number of seconds represented as f32.

Examples

use easytime::Duration;

let dur = Duration::from_secs_f32(2.7);
assert_eq!(dur, Duration::new(2, 700_000_000));

pub fn mul_f64(self, rhs: f64) -> Duration[src]

Multiplies Duration by f64.

Examples

use easytime::Duration;

let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000));
assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0));

pub fn mul_f32(self, rhs: f32) -> Duration[src]

Multiplies Duration by f32.

Examples

use easytime::Duration;

let dur = Duration::new(2, 700_000_000);
// note that due to rounding errors result is slightly different
// from 8.478 and 847800.0
assert_eq!(dur.mul_f32(3.14), Duration::new(8, 478_000_640));
assert_eq!(dur.mul_f32(3.14e5), Duration::new(847799, 969_120_256));

pub fn div_f64(self, rhs: f64) -> Duration[src]

Divide Duration by f64.

Examples

use easytime::Duration;

let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611));
// note that truncation is used, not rounding
assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_598));

pub fn div_f32(self, rhs: f32) -> Duration[src]

Divide Duration by f32.

Examples

use easytime::Duration;

let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611));
// note that truncation is used, not rounding
assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_598));

pub const fn is_some(&self) -> bool[src]

Returns true if into_inner returns Some.

Examples

use easytime::Duration;

let zero = Duration::new(0, 0);
let one_sec = Duration::new(1, 0);
assert!((one_sec - zero).is_some());
assert!(!(zero - one_sec).is_some());

pub const fn is_none(&self) -> bool[src]

Returns true if into_inner returns None.

Examples

use easytime::Duration;

let zero = Duration::new(0, 0);
let one_sec = Duration::new(1, 0);
assert!(!(one_sec - zero).is_none());
assert!((zero - one_sec).is_none());

pub const fn into_inner(self) -> Option<Duration>[src]

Returns the contained std::time::Duration or None.

Examples

use easytime::Duration;

let zero = Duration::new(0, 0);
let one_sec = Duration::new(1, 0);
assert_eq!((one_sec - zero).into_inner(), Some(std::time::Duration::from_secs(1)));
assert_eq!((zero - one_sec).into_inner(), None);

pub const fn unwrap_or(self, default: Duration) -> Duration[src]

Returns the contained std::time::Duration or a default.

dur.unwrap_or(default) is equivalent to dur.into_inner().unwrap_or(default).

Examples

use easytime::Duration;

let zero = Duration::new(0, 0);
let one_sec = Duration::new(1, 0);
assert_eq!(
    (one_sec - zero).unwrap_or(std::time::Duration::from_secs(2)),
    std::time::Duration::from_secs(1)
);
assert_eq!(
    (zero - one_sec).unwrap_or(std::time::Duration::from_secs(2)),
    std::time::Duration::from_secs(2)
);

pub fn unwrap_or_else<F>(self, default: F) -> Duration where
    F: FnOnce() -> Duration
[src]

Returns the contained std::time::Duration or computes it from a closure.

dur.unwrap_or_else(default) is equivalent to dur.into_inner().unwrap_or_else(default).

Examples

use easytime::Duration;

let zero = Duration::new(0, 0);
let one_sec = Duration::new(1, 0);
assert_eq!(
    (one_sec - zero).unwrap_or_else(|| std::time::Duration::from_secs(2)),
    std::time::Duration::from_secs(1)
);
assert_eq!(
    (zero - one_sec).unwrap_or_else(|| std::time::Duration::from_secs(2)),
    std::time::Duration::from_secs(2)
);

Trait Implementations

impl Add<Duration> for Duration[src]

type Output = Self

The resulting type after applying the + operator.

impl Add<Duration> for Duration[src]

type Output = Self

The resulting type after applying the + operator.

impl Add<Duration> for Instant[src]

type Output = Self

The resulting type after applying the + operator.

impl AddAssign<Duration> for Duration[src]

impl AddAssign<Duration> for Duration[src]

impl AddAssign<Duration> for Instant[src]

impl Clone for Duration[src]

impl Copy for Duration[src]

impl Debug for Duration[src]

impl Default for Duration[src]

impl Div<u32> for Duration[src]

type Output = Self

The resulting type after applying the / operator.

impl DivAssign<u32> for Duration[src]

impl Eq for Duration[src]

impl From<Duration> for Duration[src]

impl From<Option<Duration>> for Duration[src]

impl Hash for Duration[src]

impl Mul<Duration> for u32[src]

type Output = Duration

The resulting type after applying the * operator.

impl Mul<u32> for Duration[src]

type Output = Self

The resulting type after applying the * operator.

impl MulAssign<u32> for Duration[src]

impl Ord for Duration[src]

impl PartialEq<Duration> for Duration[src]

impl PartialEq<Duration> for Duration[src]

impl PartialEq<Duration> for Duration[src]

impl PartialOrd<Duration> for Duration[src]

impl PartialOrd<Duration> for Duration[src]

impl PartialOrd<Duration> for Duration[src]

impl StructuralEq for Duration[src]

impl StructuralPartialEq for Duration[src]

impl Sub<Duration> for Duration[src]

type Output = Self

The resulting type after applying the - operator.

impl Sub<Duration> for Duration[src]

type Output = Self

The resulting type after applying the - operator.

impl Sub<Duration> for Instant[src]

type Output = Self

The resulting type after applying the - operator.

impl SubAssign<Duration> for Duration[src]

impl SubAssign<Duration> for Duration[src]

impl SubAssign<Duration> for Instant[src]

impl TryFrom<Duration> for Duration[src]

type Error = TryFromTimeError

The type returned in the event of a conversion error.

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.