diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2023-04-12 22:13:44 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-04-12 22:13:44 +0000 |
| commit | 5a03b2e9e802626127038cff5634795f576f1c69 (patch) | |
| tree | dc283068d72f8e14dcc14aeaff314c23f8827f0e | |
| parent | ba8cafb20c2458a2016c7db3efd91d718f1a91b6 (diff) | |
| parent | 4863f88d02ca1821eac534729fb9ba347a8c6726 (diff) | |
Merge #1359
1359: Make Hertz constructors `const` r=Dirbaio a=sgoll
This PR makes `Hertz` associated functions `hz()`, `khz()`, `mhz()` and their unassociated variants `const`, allowing `Hertz` to be used more easily in constant values:
```rust
const FREQ1: Hertz = Hertz::khz(120);
const FREQ2: Hertz = mhz(1);
```
This follows the pattern used for similar types such as `Duration` and `Instant`, from `embassy-time/src/duration.rs` and `embassy-time/src/instant.rs`, respectively.
https://github.com/embassy-rs/embassy/blob/ba8cafb20c2458a2016c7db3efd91d718f1a91b6/embassy-time/src/duration.rs#L44-L47
https://github.com/embassy-rs/embassy/blob/ba8cafb20c2458a2016c7db3efd91d718f1a91b6/embassy-time/src/instant.rs#L29-L34
Co-authored-by: Sebastian Goll <[email protected]>
| -rw-r--r-- | embassy-stm32/src/time.rs | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/embassy-stm32/src/time.rs b/embassy-stm32/src/time.rs index f08abe331..604503e61 100644 --- a/embassy-stm32/src/time.rs +++ b/embassy-stm32/src/time.rs | |||
| @@ -8,31 +8,31 @@ use core::ops::{Div, Mul}; | |||
| 8 | pub struct Hertz(pub u32); | 8 | pub struct Hertz(pub u32); |
| 9 | 9 | ||
| 10 | impl Hertz { | 10 | impl Hertz { |
| 11 | pub fn hz(hertz: u32) -> Self { | 11 | pub const fn hz(hertz: u32) -> Self { |
| 12 | Self(hertz) | 12 | Self(hertz) |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | pub fn khz(kilohertz: u32) -> Self { | 15 | pub const fn khz(kilohertz: u32) -> Self { |
| 16 | Self(kilohertz * 1_000) | 16 | Self(kilohertz * 1_000) |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | pub fn mhz(megahertz: u32) -> Self { | 19 | pub const fn mhz(megahertz: u32) -> Self { |
| 20 | Self(megahertz * 1_000_000) | 20 | Self(megahertz * 1_000_000) |
| 21 | } | 21 | } |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | /// This is a convenience shortcut for [`Hertz::hz`] | 24 | /// This is a convenience shortcut for [`Hertz::hz`] |
| 25 | pub fn hz(hertz: u32) -> Hertz { | 25 | pub const fn hz(hertz: u32) -> Hertz { |
| 26 | Hertz::hz(hertz) | 26 | Hertz::hz(hertz) |
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | /// This is a convenience shortcut for [`Hertz::khz`] | 29 | /// This is a convenience shortcut for [`Hertz::khz`] |
| 30 | pub fn khz(kilohertz: u32) -> Hertz { | 30 | pub const fn khz(kilohertz: u32) -> Hertz { |
| 31 | Hertz::khz(kilohertz) | 31 | Hertz::khz(kilohertz) |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | /// This is a convenience shortcut for [`Hertz::mhz`] | 34 | /// This is a convenience shortcut for [`Hertz::mhz`] |
| 35 | pub fn mhz(megahertz: u32) -> Hertz { | 35 | pub const fn mhz(megahertz: u32) -> Hertz { |
| 36 | Hertz::mhz(megahertz) | 36 | Hertz::mhz(megahertz) |
| 37 | } | 37 | } |
| 38 | 38 | ||
