Struct fugit::Rate

source · []
pub struct Rate<T, const NOM: u32, const DENOM: u32> { /* private fields */ }
Expand description

Represents a frequency.

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

Implementations

Create a Rate from a raw value.

let _d = Rate::<u32, 1, 1_000>::from_raw(1);

Extract the raw value from a Rate.

let d = Rate::<u32, 1, 1_000>::from_raw(234);

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

Add two rates while checking for overflow.

let r1 = Rate::<u32, 1, 1_000>::from_raw(1);
let r2 = Rate::<u32, 1, 1_000>::from_raw(2);
let r3 = Rate::<u32, 1, 1_000>::from_raw(u32::MAX);

assert_eq!(r1.checked_add(r2).unwrap().raw(), 3);
assert_eq!(r1.checked_add(r3), None);

Subtract two rates while checking for overflow.

let r1 = Rate::<u32, 1, 1_000>::from_raw(1);
let r2 = Rate::<u32, 1, 1_000>::from_raw(2);
let r3 = Rate::<u32, 1, 1_000>::from_raw(u32::MAX);

assert_eq!(r2.checked_sub(r1).unwrap().raw(), 1);
assert_eq!(r1.checked_sub(r3), None);

Const partial comparison.

let r1 = Rate::<u32, 1, 1_00>::from_raw(1);
let r2 = Rate::<u32, 1, 1_000>::from_raw(1);

assert_eq!(r1.const_partial_cmp(r2), Some(core::cmp::Ordering::Greater));

Const equality check.

let r1 = Rate::<u32, 1, 1_00>::from_raw(1);
let r2 = Rate::<u32, 1, 1_000>::from_raw(10);

assert!(r1.const_eq(r2));

Const try into, checking for overflow.

let r1 = Rate::<u32, 1, 1_00>::from_raw(1);
let r2: Option<Rate::<u32, 1, 1_000>> = r1.const_try_into();

assert_eq!(r2.unwrap().raw(), 10);

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

let r1 = Rate::<u32, 1, 1>::from_raw(1);
let d1: Option<Duration::<u32, 1, 1_000>> = r1.try_into_duration();

assert_eq!(d1.unwrap().ticks(), 1_000);

Convert between bases for a rate.

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

let r1 = Rate::<u32, 1, 100>::from_raw(1);
let r2: Rate::<u32, 1, 1_000> = r1.convert();

assert_eq!(r2.raw(), 10);

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

const raw: u32= u32::MAX - 10;
const r1: Rate::<u32, 1, 100> = Rate::<u32, 1, 100>::from_raw(raw);
// Fails conversion due to overflow
const r2: Rate::<u32, 1, 200> = r1.convert();

Convert from rate to duration.

Convert the Rate to Hz.

Convert the Rate to kHz.

Convert the Rate to MHz.

Shorthand for creating a rate which represents hertz.

Shorthand for creating a rate which represents kilohertz.

Shorthand for creating a rate which represents megahertz.

Create a Rate from a raw value.

let _d = Rate::<u64, 1, 1_000>::from_raw(1);

Extract the raw value from a Rate.

let d = Rate::<u64, 1, 1_000>::from_raw(234);

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

Add two rates while checking for overflow.

let r1 = Rate::<u64, 1, 1_000>::from_raw(1);
let r2 = Rate::<u64, 1, 1_000>::from_raw(2);
let r3 = Rate::<u64, 1, 1_000>::from_raw(u64::MAX);

assert_eq!(r1.checked_add(r2).unwrap().raw(), 3);
assert_eq!(r1.checked_add(r3), None);

Subtract two rates while checking for overflow.

let r1 = Rate::<u64, 1, 1_000>::from_raw(1);
let r2 = Rate::<u64, 1, 1_000>::from_raw(2);
let r3 = Rate::<u64, 1, 1_000>::from_raw(u64::MAX);

assert_eq!(r2.checked_sub(r1).unwrap().raw(), 1);
assert_eq!(r1.checked_sub(r3), None);

Const partial comparison.

let r1 = Rate::<u64, 1, 1_00>::from_raw(1);
let r2 = Rate::<u64, 1, 1_000>::from_raw(1);

assert_eq!(r1.const_partial_cmp(r2), Some(core::cmp::Ordering::Greater));

Const equality check.

let r1 = Rate::<u64, 1, 1_00>::from_raw(1);
let r2 = Rate::<u64, 1, 1_000>::from_raw(10);

assert!(r1.const_eq(r2));

Const try into, checking for overflow.

let r1 = Rate::<u64, 1, 1_00>::from_raw(1);
let r2: Option<Rate::<u64, 1, 1_000>> = r1.const_try_into();

assert_eq!(r2.unwrap().raw(), 10);

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

let r1 = Rate::<u64, 1, 1>::from_raw(1);
let d1: Option<Duration::<u64, 1, 1_000>> = r1.try_into_duration();

assert_eq!(d1.unwrap().ticks(), 1_000);

Convert between bases for a rate.

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

let r1 = Rate::<u64, 1, 100>::from_raw(1);
let r2: Rate::<u64, 1, 1_000> = r1.convert();

assert_eq!(r2.raw(), 10);

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

const raw: u64= u64::MAX - 10;
const r1: Rate::<u64, 1, 100> = Rate::<u64, 1, 100>::from_raw(raw);
// Fails conversion due to overflow
const r2: Rate::<u64, 1, 200> = r1.convert();

Convert from rate to duration.

Convert the Rate to Hz.

Convert the Rate to kHz.

Convert the Rate to MHz.

Shorthand for creating a rate which represents hertz.

Shorthand for creating a rate which represents kilohertz.

Shorthand for creating a rate which represents megahertz.

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

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

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.