Struct MicroSeconds

Source
#[repr(transparent)]
pub struct MicroSeconds(pub u64);
Expand description

Microseconds. Represents a span of time like std::time::Duration.

This is an unsigned 64-bit type, and thus represents absolute values only.

Tuple Fields§

§0: u64

Implementations§

Source§

impl MicroSeconds

Source

pub const INVALID: Self

MicroSeconds value representing an ‘invalid’ time.

§Examples
assert_eq!(MicroSeconds::INVALID, MicroSeconds(std::u64::MAX));
Source

pub const SECOND: Self

One second in microseconds.

§Examples
assert_eq!(MicroSeconds::SECOND, MicroSeconds(1_000_000));
Source

pub const MILLISECOND: Self

One millisecond in microseconds.

§Examples
assert_eq!(MicroSeconds::MILLISECOND, MicroSeconds(1_000));
Source

pub const ZERO: Self

Zero value.

§Examples
assert_eq!(MicroSeconds::ZERO, MicroSeconds(0));
Source

pub const MIN: Self

Smallest valid time value (zero).

§Examples
assert_eq!(MicroSeconds::MIN, MicroSeconds(0));
Source

pub const MAX: Self

Largest valid time value (largest integer value is reserved for representing ‘invalid’).

Roughly equal to 5,124,095,576 hours, 213,503,982 days, or 584,542 years.

§Examples
assert_eq!(MicroSeconds::MAX, MicroSeconds(std::u64::MAX - 1));
Source

pub const fn inner(&self) -> u64

Get the inner u64 value.

§Examples
assert_eq!(MicroSeconds(100).inner(), 100);
Source

pub const fn is_valid(&self) -> bool

Returns true so long as inner value is not Self::INVALID.

§Examples
assert_eq!(MicroSeconds::MIN.is_valid(), true);
assert_eq!(MicroSeconds::MAX.is_valid(), true);
assert_eq!(MicroSeconds::INVALID.is_valid(), false);
assert_eq!(MicroSeconds::ZERO.is_valid(), true);
assert_eq!(MicroSeconds(60 * MICROS_PER_SEC).is_valid(), true);
Source

pub const fn is_zero(&self) -> bool

Returns true so long as inner value is zero.

§Examples
assert_eq!(MicroSeconds::ZERO.is_zero(), true);
assert_eq!(MicroSeconds(0).is_zero(), true);
assert_eq!(MicroSeconds(1).is_zero(), false);
Source

pub fn from_secs(secs: u64) -> Option<Self>

Creates a new MicroSeconds from the specified number of whole seconds. Returns None on overflow.

§Examples
assert_eq!(MicroSeconds::from_secs(2), Some(MicroSeconds(2_000_000)));
assert_eq!(MicroSeconds::from_secs(0xffff_ffff_0000_0000), None);
Source

pub fn from_millis(millis: u64) -> Option<Self>

Creates a new MicroSeconds from the specified number of whole milliseconds. Returns None on overflow.

§Examples
assert_eq!(MicroSeconds::from_millis(23), Some(MicroSeconds(23_000)));
assert_eq!(MicroSeconds::from_millis(0xffff_ffff_0000_0000), None);
Source

pub fn diff(self, other: Self) -> Self

Returns the absolute difference with other.

§Examples
assert_eq!(MicroSeconds(0).diff(MicroSeconds(0)), MicroSeconds(0));
assert_eq!(MicroSeconds(100).diff(MicroSeconds(100)), MicroSeconds(0));
assert_eq!(MicroSeconds(200).diff(MicroSeconds(150)), MicroSeconds(50));
assert_eq!(MicroSeconds(150).diff(MicroSeconds(200)), MicroSeconds(50));
Source

pub const fn as_secs(&self) -> u64

Returns the total number of whole seconds.

§Examples
assert_eq!(MicroSeconds(2_300_000).as_secs(), 2);
assert_eq!(MicroSeconds(2_800_000).as_secs(), 2);
Source

pub const fn as_millis(&self) -> u64

Returns the total number of whole milliseconds.

§Examples
assert_eq!(MicroSeconds(23_000_300).as_millis(), 23_000);
assert_eq!(MicroSeconds(23_000_800).as_millis(), 23_000);
Source

pub fn from_secs_f64(secs: f64) -> Self

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

Panics if secs is not finite, is negative, or the value overflows.

§Examples
assert_eq!(MicroSeconds::from_secs_f64(0.5), MicroSeconds(500_000));
assert_eq!(MicroSeconds::from_secs_f64(2.3), MicroSeconds(2_300_000));

These should panic.

MicroSeconds::from_secs_f64(std::f64::INFINITY);
MicroSeconds::from_secs_f64(-0.5);
MicroSeconds::from_secs_f64(std::f64::MAX);
MicroSeconds::from_secs_f64(std::f64::NAN);
Source

pub fn from_secs_f32(secs: f32) -> Self

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

Panics if secs is not finite, is negative, or the value overflows.

§Examples
assert_eq!(MicroSeconds::from_secs_f32(0.5), MicroSeconds(500_000));
assert_eq!(MicroSeconds::from_secs_f32(2.3), MicroSeconds(2_299_999));

These should panic.

MicroSeconds::from_secs_f32(std::f32::INFINITY);
MicroSeconds::from_secs_f32(-0.5);
MicroSeconds::from_secs_f32(std::f32::MAX);
MicroSeconds::from_secs_f32(std::f32::NAN);
Source

pub fn as_secs_f64(&self) -> f64

Returns the number of seconds as f64.

§Examples
assert_eq!(MicroSeconds(2_300_000).as_secs_f64(), 2.3);
assert_eq!(MicroSeconds(500_000).as_secs_f64(), 0.5);
Source

pub fn as_secs_f32(&self) -> f32

Returns the number of seconds as f32.

§Examples
assert_eq!(MicroSeconds(2_300_000).as_secs_f32(), 2.3);
assert_eq!(MicroSeconds(500_000).as_secs_f32(), 0.5);
Source

pub fn checked_add(self, other: Self) -> Option<Self>

Checked integer addition. Computes self + rhs, returning None if overflow occurred, using the inner integer’s checked_add() method.

§Examples
let quater_minute = MicroSeconds(15 * MICROS_PER_SEC);
let half_minute = MicroSeconds(30 * MICROS_PER_SEC);
let three_quater_minute = MicroSeconds(45 * MICROS_PER_SEC);

assert_eq!(half_minute.checked_add(quater_minute), Some(three_quater_minute));
assert_eq!(MicroSeconds::MAX.checked_add(half_minute), None);
Source

pub fn checked_add_duration(self, rhs: Duration) -> Option<Self>

Checked integer addition. Computes self + rhs, returning None if overflow occurred, using the inner integer’s checked_add() method.

§Examples
let half_minute = MicroSeconds(30 * MICROS_PER_SEC);
let duration1 = Duration::new(2, 5 * NANOS_PER_MICRO + 20); // 2s + 5us + 20ns
let duration2 = Duration::new(MicroSeconds::MAX.inner() / MICROS_PER_SEC, 0);

assert_eq!(half_minute.checked_add_duration(duration1), Some(MicroSeconds(32_000_005)));
assert_eq!(half_minute.checked_add_duration(duration2), None);
Source

pub fn checked_sub(self, other: Self) -> Option<Self>

Checked integer subtraction. Computes self - rhs, returning None if overflow occurred, using the inner integer’s checked_sub() method.

§Examples
let quater_minute = MicroSeconds(15 * MICROS_PER_SEC);
let three_quater_minute = MicroSeconds(45 * MICROS_PER_SEC);
let whole_minute = MicroSeconds(60 * MICROS_PER_SEC);

assert_eq!(whole_minute.checked_sub(quater_minute), Some(three_quater_minute));
assert_eq!(quater_minute.checked_sub(whole_minute), None);
Source

pub fn checked_sub_duration(self, rhs: Duration) -> Option<Self>

Checked integer subtraction. Computes self - rhs, returning None if overflow occurred, using the inner integer’s checked_sub() method.

§Examples
let half_minute = MicroSeconds(30 * MICROS_PER_SEC);
let duration1 = Duration::new(2, 5 * NANOS_PER_MICRO + 20); // 2s + 5us + 20ns
let duration2 = Duration::new(45, 0);

assert_eq!(half_minute.checked_sub_duration(duration1), Some(MicroSeconds(27_999_995)));
assert_eq!(half_minute.checked_sub_duration(duration2), None);
Source

pub fn checked_mul(self, rhs: u32) -> Option<Self>

Checked integer multiplication. Computes self * rhs, returning None if overflow occurred, using the inner integer’s checked_mul() method.

§Examples
let quater_minute = MicroSeconds(15 * MICROS_PER_SEC);
let whole_minute = MicroSeconds(60 * MICROS_PER_SEC);

assert_eq!(quater_minute.checked_mul(4), Some(whole_minute));
assert_eq!(MicroSeconds::MAX.checked_mul(2), None);
Source

pub fn checked_div(self, rhs: u32) -> Option<Self>

Checked integer division. Computes self / rhs, returning None if rhs == 0, using the inner integer’s checked_div() method.

§Examples
let quater_minute = MicroSeconds(15 * MICROS_PER_SEC);
let whole_minute = MicroSeconds(60 * MICROS_PER_SEC);

assert_eq!(whole_minute.checked_div(4), Some(quater_minute));
assert_eq!(whole_minute.checked_div(0), None);
Source

pub fn checked_rem(self, rhs: u32) -> Option<Self>

Checked integer remainder. Computes self % rhs, returning None if rhs == 0, using the inner integer’s checked_rem() method.

§Examples
let quater_minute = MicroSeconds(15 * MICROS_PER_SEC);
let whole_minute = MicroSeconds(60 * MICROS_PER_SEC);

assert_eq!(whole_minute.checked_rem(4), Some(MicroSeconds::ZERO));
assert_eq!(whole_minute.checked_rem(7), Some(MicroSeconds(4)));
assert_eq!(whole_minute.checked_rem(0), None);
Source

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

Multiplies MicroSeconds by f64.

Converts to an f64 representing seconds, multiplies by the given factor, then converts back to microseconds.

Panics if rhs is not finite, is negative, or the value overflows.

§Examples
let micros = MicroSeconds(2_700_000_000);

assert_eq!(micros.mul_f64(3.14), MicroSeconds(8_478_000_000));
assert_eq!(micros.mul_f64(3.14e5), MicroSeconds(847_800_000_000_000));

These should panic.

MicroSeconds::SECOND.mul_f64(std::f64::INFINITY);
MicroSeconds::SECOND.mul_f64(-0.5);
MicroSeconds(2 * MICROS_PER_SEC).mul_f64(std::f64::MAX / 10.0);
MicroSeconds::SECOND.mul_f64(std::f64::NAN);
Source

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

Multiplies MicroSeconds by f32.

Converts to an f32 representing seconds, multiplies by the given factor, then converts back to microseconds.

Panics if rhs is not finite, is negative, or the value overflows.

§Examples
let micros = MicroSeconds(2_700_000_000);

// Note the rounding errors that are clear here.
assert_eq!(micros.mul_f32(3.14), MicroSeconds(8_478_000_000));
assert_eq!(micros.mul_f32(3.14e5), MicroSeconds(847_800_000_000_000));

These should panic.

MicroSeconds::SECOND.mul_f32(std::f32::INFINITY);
MicroSeconds::SECOND.mul_f32(-0.5);
MicroSeconds(2 * MICROS_PER_SEC).mul_f32(std::f32::MAX / 10.0);
MicroSeconds::SECOND.mul_f32(std::f32::NAN);
Source

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

Divides MicroSeconds by f64.

Converts to an f64 representing seconds, divides by the given factor, then converts back to microseconds.

Panics if rhs is not finite, is negative, or the value overflows.

§Examples
let micros = MicroSeconds(2_700_000_000);

assert_eq!(micros.div_f64(3.14), MicroSeconds(859_872_611));
assert_eq!(micros.div_f64(3.14e5), MicroSeconds(8_598));

These should panic.

MicroSeconds::SECOND.div_f64(-2.0);
MicroSeconds::MAX.div_f64(0.5);
MicroSeconds::SECOND.div_f64(std::f64::NAN);
Source

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

Divides MicroSeconds by f32.

Converts to an f32 representing seconds, divides by the given factor, then converts back to microseconds.

Panics if rhs is not finite, is negative, or the value overflows.

§Examples
let micros = MicroSeconds(2_700_000_000);

assert_eq!(micros.div_f32(3.14), MicroSeconds(859_872_558));
assert_eq!(micros.div_f32(3.14e5), MicroSeconds(8_598));

These should panic.

MicroSeconds::SECOND.div_f32(-2.0);
MicroSeconds::MAX.div_f32(0.5);
MicroSeconds::SECOND.div_f32(std::f32::NAN);

Trait Implementations§

Source§

impl Add<Duration> for MicroSeconds

Source§

type Output = MicroSeconds

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add<MicroSeconds> for Duration

Source§

type Output = Duration

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add<MicroSeconds> for MonotonicTs

Source§

type Output = MonotonicTs

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add<MicroSeconds> for Timeval

Source§

type Output = Timeval

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add<MicroSeconds> for UnixTs

Source§

type Output = UnixTs

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add for MicroSeconds

Source§

type Output = MicroSeconds

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl AddAssign<Duration> for MicroSeconds

Source§

fn add_assign(&mut self, rhs: Duration)

Performs the += operation. Read more
Source§

impl AddAssign<MicroSeconds> for Duration

Source§

fn add_assign(&mut self, rhs: MicroSeconds)

Performs the += operation. Read more
Source§

impl AddAssign<MicroSeconds> for MonotonicTs

Source§

fn add_assign(&mut self, rhs: MicroSeconds)

Performs the += operation. Read more
Source§

impl AddAssign<MicroSeconds> for Timeval

Source§

fn add_assign(&mut self, rhs: MicroSeconds)

Performs the += operation. Read more
Source§

impl AddAssign<MicroSeconds> for UnixTs

Source§

fn add_assign(&mut self, rhs: MicroSeconds)

Performs the += operation. Read more
Source§

impl AddAssign for MicroSeconds

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for MicroSeconds

Source§

fn clone(&self) -> MicroSeconds

Returns a duplicate 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 MicroSeconds

Source§

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

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

impl Default for MicroSeconds

Source§

fn default() -> MicroSeconds

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

impl Display for MicroSeconds

Source§

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

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

impl Div<u32> for MicroSeconds

Source§

type Output = MicroSeconds

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl DivAssign<u32> for MicroSeconds

Source§

fn div_assign(&mut self, rhs: u32)

Performs the /= operation. Read more
Source§

impl From<MicroSeconds> for Duration

Source§

fn from(t: MicroSeconds) -> Self

Converts to this type from the input type.
Source§

impl From<MicroSeconds> for Timeval

Source§

fn from(t: MicroSeconds) -> Self

Converts to this type from the input type.
Source§

impl From<Timeval> for MicroSeconds

Source§

fn from(t: Timeval) -> Self

Converts to this type from the input type.
Source§

impl Mul<MicroSeconds> for u32

Source§

type Output = MicroSeconds

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: MicroSeconds) -> MicroSeconds

Performs the * operation. Read more
Source§

impl Mul<u32> for MicroSeconds

Source§

type Output = MicroSeconds

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl MulAssign<u32> for MicroSeconds

Source§

fn mul_assign(&mut self, rhs: u32)

Performs the *= operation. Read more
Source§

impl Ord for MicroSeconds

Source§

fn cmp(&self, other: &MicroSeconds) -> 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,

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

impl PartialEq for MicroSeconds

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for MicroSeconds

Source§

fn partial_cmp(&self, other: &MicroSeconds) -> 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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Rem<u32> for MicroSeconds

Source§

type Output = MicroSeconds

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: u32) -> Self

Performs the % operation. Read more
Source§

impl RemAssign<u32> for MicroSeconds

Source§

fn rem_assign(&mut self, rhs: u32)

Performs the %= operation. Read more
Source§

impl Sub<Duration> for MicroSeconds

Source§

type Output = MicroSeconds

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<MicroSeconds> for Duration

Source§

type Output = Duration

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<MicroSeconds> for MonotonicTs

Source§

type Output = MonotonicTs

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<MicroSeconds> for Timeval

Source§

type Output = Timeval

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<MicroSeconds> for UnixTs

Source§

type Output = UnixTs

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub for MicroSeconds

Source§

type Output = MicroSeconds

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl SubAssign<Duration> for MicroSeconds

Source§

fn sub_assign(&mut self, rhs: Duration)

Performs the -= operation. Read more
Source§

impl SubAssign<MicroSeconds> for Duration

Source§

fn sub_assign(&mut self, rhs: MicroSeconds)

Performs the -= operation. Read more
Source§

impl SubAssign<MicroSeconds> for MonotonicTs

Source§

fn sub_assign(&mut self, rhs: MicroSeconds)

Performs the -= operation. Read more
Source§

impl SubAssign<MicroSeconds> for Timeval

Source§

fn sub_assign(&mut self, rhs: MicroSeconds)

Performs the -= operation. Read more
Source§

impl SubAssign<MicroSeconds> for UnixTs

Source§

fn sub_assign(&mut self, rhs: MicroSeconds)

Performs the -= operation. Read more
Source§

impl SubAssign for MicroSeconds

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl TryFrom<Duration> for MicroSeconds

Source§

type Error = ()

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

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

Performs the conversion.
Source§

impl Copy for MicroSeconds

Source§

impl Eq for MicroSeconds

Source§

impl StructuralPartialEq for MicroSeconds

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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>,

Source§

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.
Source§

impl<T, Rhs> NumAssignOps<Rhs> for T
where T: AddAssign<Rhs> + SubAssign<Rhs> + MulAssign<Rhs> + DivAssign<Rhs> + RemAssign<Rhs>,