pub struct Duration<T, const NOM: u32, const DENOM: u32> { /* private fields */ }
Expand description

Represents a duration of time.

The generic T can either be u32 or u64, and the const generics represent the ratio of the ticks contained within the duration: duration in seconds = NOM / DENOM * ticks

Implementations

Create a Duration from a ticks value.

let _d = Duration::<u32, 1, 1_000>::from_ticks(1);

Extract the ticks from a Duration.

let d = Duration::<u32, 1, 1_000>::from_ticks(234);

assert_eq!(d.ticks(), 234);

Add two durations while checking for overflow.

let d1 = Duration::<u32, 1, 1_000>::from_ticks(1);
let d2 = Duration::<u32, 1, 1_000>::from_ticks(2);
let d3 = Duration::<u32, 1, 1_000>::from_ticks(u32::MAX);

assert_eq!(d1.checked_add(d2).unwrap().ticks(), 3);
assert_eq!(d1.checked_add(d3), None);

Subtract two durations while checking for overflow.

let d1 = Duration::<u32, 1, 1_000>::from_ticks(1);
let d2 = Duration::<u32, 1, 1_000>::from_ticks(2);
let d3 = Duration::<u32, 1, 1_000>::from_ticks(u32::MAX);

assert_eq!(d2.checked_sub(d1).unwrap().ticks(), 1);
assert_eq!(d1.checked_sub(d3), None);

Const partial comparison.

let d1 = Duration::<u32, 1, 1_00>::from_ticks(1);
let d2 = Duration::<u32, 1, 1_000>::from_ticks(1);

assert_eq!(d1.const_partial_cmp(d2), Some(core::cmp::Ordering::Greater));

Const equality check.

let d1 = Duration::<u32, 1, 1_00>::from_ticks(1);
let d2 = Duration::<u32, 1, 1_000>::from_ticks(10);

assert!(d1.const_eq(d2));

Const try into, checking for overflow.

let d1 = Duration::<u32, 1, 1_00>::from_ticks(1);
let d2: Option<Duration::<u32, 1, 1_000>> = d1.const_try_into();

assert_eq!(d2.unwrap().ticks(), 10);

Const try into rate, checking for divide-by-zero.

let d1 = Duration::<u32, 1, 1_000>::from_ticks(2);
let r1: Option<Rate::<u32, 1, 1>> = d1.try_into_rate();

assert_eq!(r1.unwrap().raw(), 500);

Convert from duration to rate.

Convert between bases for a duration.

Unfortunately not a From impl due to collision with the std lib.

let d1 = Duration::<u32, 1, 100>::from_ticks(1);
let d2: Duration::<u32, 1, 1_000> = d1.convert();

assert_eq!(d2.ticks(), 10);

Can be used in const contexts. Compilation will fail if the conversion causes overflow

const TICKS: u32= u32::MAX - 10;
const D1: Duration::<u32, 1, 100> = Duration::<u32, 1, 100>::from_ticks(TICKS);
// Fails conversion due to tick overflow
const D2: Duration::<u32, 1, 200> = D1.convert();

Convert the Duration to an integer number of nanoseconds.

Convert the Duration to an integer number of microseconds.

Convert the Duration to an integer number of milliseconds.

Convert the Duration to an integer number of seconds.

Convert the Duration to an integer number of minutes.

Convert the Duration to an integer number of hours.

Shorthand for creating a duration which represents nanoseconds.

Shorthand for creating a duration which represents microseconds.

Shorthand for creating a duration which represents milliseconds.

Shorthand for creating a duration which represents seconds.

Shorthand for creating a duration which represents minutes.

Shorthand for creating a duration which represents hours.

Create a Duration from a ticks value.

let _d = Duration::<u64, 1, 1_000>::from_ticks(1);

Extract the ticks from a Duration.

let d = Duration::<u64, 1, 1_000>::from_ticks(234);

assert_eq!(d.ticks(), 234);

Add two durations while checking for overflow.

let d1 = Duration::<u64, 1, 1_000>::from_ticks(1);
let d2 = Duration::<u64, 1, 1_000>::from_ticks(2);
let d3 = Duration::<u64, 1, 1_000>::from_ticks(u64::MAX);

assert_eq!(d1.checked_add(d2).unwrap().ticks(), 3);
assert_eq!(d1.checked_add(d3), None);

Subtract two durations while checking for overflow.

let d1 = Duration::<u64, 1, 1_000>::from_ticks(1);
let d2 = Duration::<u64, 1, 1_000>::from_ticks(2);
let d3 = Duration::<u64, 1, 1_000>::from_ticks(u64::MAX);

assert_eq!(d2.checked_sub(d1).unwrap().ticks(), 1);
assert_eq!(d1.checked_sub(d3), None);

Const partial comparison.

let d1 = Duration::<u64, 1, 1_00>::from_ticks(1);
let d2 = Duration::<u64, 1, 1_000>::from_ticks(1);

assert_eq!(d1.const_partial_cmp(d2), Some(core::cmp::Ordering::Greater));

Const equality check.

let d1 = Duration::<u64, 1, 1_00>::from_ticks(1);
let d2 = Duration::<u64, 1, 1_000>::from_ticks(10);

assert!(d1.const_eq(d2));

Const try into, checking for overflow.

let d1 = Duration::<u64, 1, 1_00>::from_ticks(1);
let d2: Option<Duration::<u64, 1, 1_000>> = d1.const_try_into();

assert_eq!(d2.unwrap().ticks(), 10);

Const try into rate, checking for divide-by-zero.

let d1 = Duration::<u64, 1, 1_000>::from_ticks(2);
let r1: Option<Rate::<u64, 1, 1>> = d1.try_into_rate();

assert_eq!(r1.unwrap().raw(), 500);

Convert from duration to rate.

Convert between bases for a duration.

Unfortunately not a From impl due to collision with the std lib.

let d1 = Duration::<u64, 1, 100>::from_ticks(1);
let d2: Duration::<u64, 1, 1_000> = d1.convert();

assert_eq!(d2.ticks(), 10);

Can be used in const contexts. Compilation will fail if the conversion causes overflow

const TICKS: u64= u64::MAX - 10;
const D1: Duration::<u64, 1, 100> = Duration::<u64, 1, 100>::from_ticks(TICKS);
// Fails conversion due to tick overflow
const D2: Duration::<u64, 1, 200> = D1.convert();

Convert the Duration to an integer number of nanoseconds.

Convert the Duration to an integer number of microseconds.

Convert the Duration to an integer number of milliseconds.

Convert the Duration to an integer number of seconds.

Convert the Duration to an integer number of minutes.

Convert the Duration to an integer number of hours.

Shorthand for creating a duration which represents nanoseconds.

Shorthand for creating a duration which represents microseconds.

Shorthand for creating a duration which represents milliseconds.

Shorthand for creating a duration which represents seconds.

Shorthand for creating a duration which represents minutes.

Shorthand for creating a duration which represents hours.

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

Formats the value using the given formatter. Read more

Formats the value using the given formatter. 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

Writes the defmt representation of self to fmt.

Writes the defmt representation of self to fmt.

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

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

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 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 tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

This method tests for !=.

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

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

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

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

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