logo
#[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

MicroSeconds value representing an ‘invalid’ time.

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

One second in microseconds.

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

One millisecond in microseconds.

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

Zero value.

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

Smallest valid time value (zero).

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

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

Get the inner u64 value.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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_152));
assert_eq!(micros.mul_f32(3.14e5), MicroSeconds(847_800_018_512_379));

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

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

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

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Formats the value using the given formatter. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

Performs the /= operation. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

Performs the *= operation. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

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

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

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

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

The resulting type after applying the % operator.

Performs the % operation. Read more

Performs the %= operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.