From bbbaea850e52c408654a586e0c4fbff8c1efc6f1 Mon Sep 17 00:00:00 2001 From: Onur Özkan Date: Wed, 18 Jun 2025 12:14:42 +0300 Subject: rust: make `clk::Hertz` methods const MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Marks `Hertz` methods as `const` to make them available for `const` contexts. This can be useful when defining static/compile-time frequency parameters in drivers/subsystems. Signed-off-by: Onur Özkan Link: https://lore.kernel.org/r/20250618091442.29104-1-work@onurozkan.dev Reviewed-by: Alice Ryhl Acked-by: Viresh Kumar Reviewed-by: Alexandre Courbot Signed-off-by: Stephen Boyd --- rust/kernel/clk.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'rust/kernel') diff --git a/rust/kernel/clk.rs b/rust/kernel/clk.rs index 6041c6d07527..ef0a2edd52c3 100644 --- a/rust/kernel/clk.rs +++ b/rust/kernel/clk.rs @@ -31,37 +31,37 @@ pub struct Hertz(pub c_ulong); impl Hertz { /// Create a new instance from kilohertz (kHz) - pub fn from_khz(khz: c_ulong) -> Self { + pub const fn from_khz(khz: c_ulong) -> Self { Self(khz * 1_000) } /// Create a new instance from megahertz (MHz) - pub fn from_mhz(mhz: c_ulong) -> Self { + pub const fn from_mhz(mhz: c_ulong) -> Self { Self(mhz * 1_000_000) } /// Create a new instance from gigahertz (GHz) - pub fn from_ghz(ghz: c_ulong) -> Self { + pub const fn from_ghz(ghz: c_ulong) -> Self { Self(ghz * 1_000_000_000) } /// Get the frequency in hertz - pub fn as_hz(&self) -> c_ulong { + pub const fn as_hz(&self) -> c_ulong { self.0 } /// Get the frequency in kilohertz - pub fn as_khz(&self) -> c_ulong { + pub const fn as_khz(&self) -> c_ulong { self.0 / 1_000 } /// Get the frequency in megahertz - pub fn as_mhz(&self) -> c_ulong { + pub const fn as_mhz(&self) -> c_ulong { self.0 / 1_000_000 } /// Get the frequency in gigahertz - pub fn as_ghz(&self) -> c_ulong { + pub const fn as_ghz(&self) -> c_ulong { self.0 / 1_000_000_000 } } -- cgit v1.2.3 From b112dfc74b2040721959935b317d784455c5f635 Mon Sep 17 00:00:00 2001 From: Onur Özkan Date: Wed, 18 Jun 2025 12:35:08 +0300 Subject: rust: shorten `con_id`s in `get` methods in clk module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Converts `if-else` blocks into one line code using `map_or` for simplicity. Signed-off-by: Onur Özkan Link: https://lore.kernel.org/r/20250618093508.16343-1-work@onurozkan.dev Acked-by: Viresh Kumar Reviewed-by: Alexandre Courbot Signed-off-by: Stephen Boyd --- rust/kernel/clk.rs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'rust/kernel') diff --git a/rust/kernel/clk.rs b/rust/kernel/clk.rs index ef0a2edd52c3..d5b1112102e1 100644 --- a/rust/kernel/clk.rs +++ b/rust/kernel/clk.rs @@ -132,11 +132,7 @@ mod common_clk { /// /// [`clk_get`]: https://docs.kernel.org/core-api/kernel-api.html#c.clk_get pub fn get(dev: &Device, name: Option<&CStr>) -> Result { - let con_id = if let Some(name) = name { - name.as_ptr() - } else { - ptr::null() - }; + let con_id = name.map_or(ptr::null(), |n| n.as_ptr()); // SAFETY: It is safe to call [`clk_get`] for a valid device pointer. // @@ -304,11 +300,7 @@ mod common_clk { /// [`clk_get_optional`]: /// https://docs.kernel.org/core-api/kernel-api.html#c.clk_get_optional pub fn get(dev: &Device, name: Option<&CStr>) -> Result { - let con_id = if let Some(name) = name { - name.as_ptr() - } else { - ptr::null() - }; + let con_id = name.map_or(ptr::null(), |n| n.as_ptr()); // SAFETY: It is safe to call [`clk_get_optional`] for a valid device pointer. // -- cgit v1.2.3 From 2a7b4b228cbcebe273d9f4206ed8ee9cbba70ab0 Mon Sep 17 00:00:00 2001 From: Onur Özkan Date: Wed, 18 Jun 2025 12:28:10 +0300 Subject: rust: replace literals with constants in `clk::Hertz` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces repeated numeric literals in `Hertz` conversions with named constants. Signed-off-by: Onur Özkan Link: https://lore.kernel.org/r/20250618092810.29370-1-work@onurozkan.dev Acked-by: Viresh Kumar Reviewed-by: Alexandre Courbot Signed-off-by: Stephen Boyd --- rust/kernel/clk.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'rust/kernel') diff --git a/rust/kernel/clk.rs b/rust/kernel/clk.rs index d5b1112102e1..fbcea31dbcca 100644 --- a/rust/kernel/clk.rs +++ b/rust/kernel/clk.rs @@ -30,19 +30,23 @@ use crate::ffi::c_ulong; pub struct Hertz(pub c_ulong); impl Hertz { + const KHZ_TO_HZ: c_ulong = 1_000; + const MHZ_TO_HZ: c_ulong = 1_000_000; + const GHZ_TO_HZ: c_ulong = 1_000_000_000; + /// Create a new instance from kilohertz (kHz) pub const fn from_khz(khz: c_ulong) -> Self { - Self(khz * 1_000) + Self(khz * Self::KHZ_TO_HZ) } /// Create a new instance from megahertz (MHz) pub const fn from_mhz(mhz: c_ulong) -> Self { - Self(mhz * 1_000_000) + Self(mhz * Self::MHZ_TO_HZ) } /// Create a new instance from gigahertz (GHz) pub const fn from_ghz(ghz: c_ulong) -> Self { - Self(ghz * 1_000_000_000) + Self(ghz * Self::GHZ_TO_HZ) } /// Get the frequency in hertz @@ -52,17 +56,17 @@ impl Hertz { /// Get the frequency in kilohertz pub const fn as_khz(&self) -> c_ulong { - self.0 / 1_000 + self.0 / Self::KHZ_TO_HZ } /// Get the frequency in megahertz pub const fn as_mhz(&self) -> c_ulong { - self.0 / 1_000_000 + self.0 / Self::MHZ_TO_HZ } /// Get the frequency in gigahertz pub const fn as_ghz(&self) -> c_ulong { - self.0 / 1_000_000_000 + self.0 / Self::GHZ_TO_HZ } } -- cgit v1.2.3