Struct easytime::Duration

source ·
pub struct Duration(/* private fields */);
Expand description

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§

source§

impl Duration

source

pub const NONE: Self = _

Returns a “none” value

source

pub const ZERO: Self = _

A duration of zero time.

§Examples
use easytime::Duration;

let duration = Duration::ZERO;
assert!(duration.is_zero());
assert_eq!(duration.as_nanos(), Some(0));
source

pub const MAX: Self = _

The maximum duration.

May vary by platform as necessary. Must be able to contain the difference between two instances of Instant or two instances of SystemTime. This constraint gives it a value of about 584,942,417,355 years in practice, which is currently used on all platforms.

§Examples
use easytime::Duration;

assert_eq!(Duration::MAX, Duration::new(u64::MAX, 1_000_000_000 - 1));
source

pub const fn new(secs: u64, nanos: u32) -> Self

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);
source

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

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());
source

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

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());
source

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

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());
source

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

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());
source

pub const fn is_zero(&self) -> bool

Returns true if this Duration spans no time.

§Examples
use easytime::Duration;

assert!(Duration::ZERO.is_zero());
assert!(Duration::new(0, 0).is_zero());
assert!(Duration::from_nanos(0).is_zero());
assert!(Duration::from_secs(0).is_zero());

assert!(!Duration::new(1, 1).is_zero());
assert!(!Duration::from_nanos(1).is_zero());
assert!(!Duration::from_secs(1).is_zero());
source

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

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));
source

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

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));
source

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

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));
source

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

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));
source

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

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));
source

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

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));
source

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

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));
source

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

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));
source

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

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));
source

pub fn from_secs_f64(secs: f64) -> Self

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));
source

pub fn from_secs_f32(secs: f32) -> Duration

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));
source

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

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));
source

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

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));
source

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

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));
source

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

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));
source

pub const fn is_some(&self) -> bool

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());
source

pub const fn is_none(&self) -> bool

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());
source

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

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);
source

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

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)
);
source

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

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§

source§

impl Add<Duration> for Duration

§

type Output = Duration

The resulting type after applying the + operator.
source§

fn add(self, rhs: Duration) -> Self::Output

Performs the + operation. Read more
source§

impl Add<Duration> for Instant

§

type Output = Instant

The resulting type after applying the + operator.
source§

fn add(self, other: Duration) -> Self::Output

Performs the + operation. Read more
source§

impl Add for Duration

§

type Output = Duration

The resulting type after applying the + operator.
source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
source§

impl AddAssign<Duration> for Duration

source§

fn add_assign(&mut self, rhs: Duration)

Performs the += operation. Read more
source§

impl AddAssign<Duration> for Instant

source§

fn add_assign(&mut self, other: Duration)

Performs the += operation. Read more
source§

impl AddAssign for Duration

source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
source§

impl Clone for Duration

source§

fn clone(&self) -> Duration

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Duration

source§

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

Formats the value using the given formatter. Read more
source§

impl Default for Duration

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Div<u32> for Duration

§

type Output = Duration

The resulting type after applying the / operator.
source§

fn div(self, rhs: u32) -> Self::Output

Performs the / operation. Read more
source§

impl DivAssign<u32> for Duration

source§

fn div_assign(&mut self, rhs: u32)

Performs the /= operation. Read more
source§

impl From<Duration> for Duration

source§

fn from(dur: Duration) -> Self

Converts to this type from the input type.
source§

impl From<Option<Duration>> for Duration

source§

fn from(dur: Option<Duration>) -> Self

Converts to this type from the input type.
source§

impl Hash for Duration

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Mul<Duration> for u32

§

type Output = Duration

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Duration) -> Self::Output

Performs the * operation. Read more
source§

impl Mul<u32> for Duration

§

type Output = Duration

The resulting type after applying the * operator.
source§

fn mul(self, rhs: u32) -> Self::Output

Performs the * operation. Read more
source§

impl MulAssign<u32> for Duration

source§

fn mul_assign(&mut self, rhs: u32)

Performs the *= operation. Read more
source§

impl Ord for Duration

source§

fn cmp(&self, other: &Duration) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<Duration> for Duration

source§

fn eq(&self, other: &Duration) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Duration> for Duration

source§

fn eq(&self, other: &Duration) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq for Duration

source§

fn eq(&self, other: &Duration) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Duration> for Duration

source§

fn partial_cmp(&self, other: &Duration) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<Duration> for Duration

source§

fn partial_cmp(&self, other: &Duration) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd for Duration

source§

fn partial_cmp(&self, other: &Duration) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Sub<Duration> for Duration

§

type Output = Duration

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Duration) -> Self::Output

Performs the - operation. Read more
source§

impl Sub<Duration> for Instant

§

type Output = Instant

The resulting type after applying the - operator.
source§

fn sub(self, other: Duration) -> Self::Output

Performs the - operation. Read more
source§

impl Sub for Duration

§

type Output = Duration

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
source§

impl SubAssign<Duration> for Duration

source§

fn sub_assign(&mut self, rhs: Duration)

Performs the -= operation. Read more
source§

impl SubAssign<Duration> for Instant

source§

fn sub_assign(&mut self, other: Duration)

Performs the -= operation. Read more
source§

impl SubAssign for Duration

source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
source§

impl TryFrom<Duration> for Duration

§

type Error = TryFromTimeError

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

fn try_from(dur: Duration) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Copy for Duration

source§

impl Eq for Duration

source§

impl StructuralPartialEq for Duration

Auto Trait Implementations§

Blanket Implementations§

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<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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.