Skip to content

Commit 2a7b4b2

Browse files
onur-ozkanbebarino
authored andcommitted
rust: replace literals with constants in clk::Hertz
Replaces repeated numeric literals in `Hertz` conversions with named constants. Signed-off-by: Onur Özkan <work@onurozkan.dev> Link: https://lore.kernel.org/r/20250618092810.29370-1-work@onurozkan.dev Acked-by: Viresh Kumar <viresh.kumar@linaro.org> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com> Signed-off-by: Stephen Boyd <sboyd@kernel.org>
1 parent b112dfc commit 2a7b4b2

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

rust/kernel/clk.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,23 @@ use crate::ffi::c_ulong;
3030
pub struct Hertz(pub c_ulong);
3131

3232
impl Hertz {
33+
const KHZ_TO_HZ: c_ulong = 1_000;
34+
const MHZ_TO_HZ: c_ulong = 1_000_000;
35+
const GHZ_TO_HZ: c_ulong = 1_000_000_000;
36+
3337
/// Create a new instance from kilohertz (kHz)
3438
pub const fn from_khz(khz: c_ulong) -> Self {
35-
Self(khz * 1_000)
39+
Self(khz * Self::KHZ_TO_HZ)
3640
}
3741

3842
/// Create a new instance from megahertz (MHz)
3943
pub const fn from_mhz(mhz: c_ulong) -> Self {
40-
Self(mhz * 1_000_000)
44+
Self(mhz * Self::MHZ_TO_HZ)
4145
}
4246

4347
/// Create a new instance from gigahertz (GHz)
4448
pub const fn from_ghz(ghz: c_ulong) -> Self {
45-
Self(ghz * 1_000_000_000)
49+
Self(ghz * Self::GHZ_TO_HZ)
4650
}
4751

4852
/// Get the frequency in hertz
@@ -52,17 +56,17 @@ impl Hertz {
5256

5357
/// Get the frequency in kilohertz
5458
pub const fn as_khz(&self) -> c_ulong {
55-
self.0 / 1_000
59+
self.0 / Self::KHZ_TO_HZ
5660
}
5761

5862
/// Get the frequency in megahertz
5963
pub const fn as_mhz(&self) -> c_ulong {
60-
self.0 / 1_000_000
64+
self.0 / Self::MHZ_TO_HZ
6165
}
6266

6367
/// Get the frequency in gigahertz
6468
pub const fn as_ghz(&self) -> c_ulong {
65-
self.0 / 1_000_000_000
69+
self.0 / Self::GHZ_TO_HZ
6670
}
6771
}
6872

0 commit comments

Comments
 (0)