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.

ba8cafb20c/embassy-time/src/duration.rs (L44-L47)

ba8cafb20c/embassy-time/src/instant.rs (L29-L34)

Co-authored-by: Sebastian Goll <sebastian.goll@gmx.de>
This commit is contained in:
bors[bot] 2023-04-12 22:13:44 +00:00 committed by GitHub
commit 5a03b2e9e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,31 +8,31 @@ use core::ops::{Div, Mul};
pub struct Hertz(pub u32);
impl Hertz {
pub fn hz(hertz: u32) -> Self {
pub const fn hz(hertz: u32) -> Self {
Self(hertz)
}
pub fn khz(kilohertz: u32) -> Self {
pub const fn khz(kilohertz: u32) -> Self {
Self(kilohertz * 1_000)
}
pub fn mhz(megahertz: u32) -> Self {
pub const fn mhz(megahertz: u32) -> Self {
Self(megahertz * 1_000_000)
}
}
/// This is a convenience shortcut for [`Hertz::hz`]
pub fn hz(hertz: u32) -> Hertz {
pub const fn hz(hertz: u32) -> Hertz {
Hertz::hz(hertz)
}
/// This is a convenience shortcut for [`Hertz::khz`]
pub fn khz(kilohertz: u32) -> Hertz {
pub const fn khz(kilohertz: u32) -> Hertz {
Hertz::khz(kilohertz)
}
/// This is a convenience shortcut for [`Hertz::mhz`]
pub fn mhz(megahertz: u32) -> Hertz {
pub const fn mhz(megahertz: u32) -> Hertz {
Hertz::mhz(megahertz)
}