Struct fugit_timer::Duration
source ·
[−]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);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();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.
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);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();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.
Trait Implementations
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 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 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 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
