From 327d44754c54060baf9679ac073a21839912be16 Mon Sep 17 00:00:00 2001 From: Andreas Hindborg Date: Mon, 1 Jun 2026 12:17:05 +0200 Subject: [PATCH] rust: module_param: return value by copy from `value` For `Copy` parameter types it is more ergonomic to retrieve the parameter value by copy than through a shared reference. Change `ModuleParamAccess::value` to return `T` by copy when `T: Copy`, and rename the previous reference-returning accessor to `value_ref`. Update the in-tree caller in `rust_minimal`. Suggested-by: Alice Ryhl Signed-off-by: Andreas Hindborg Reviewed-by: Petr Pavlu Reviewed-by: Gary Guo Signed-off-by: Petr Pavlu --- rust/kernel/module_param.rs | 18 +++++++++++++++++- samples/rust/rust_minimal.rs | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/rust/kernel/module_param.rs b/rust/kernel/module_param.rs index 6541af218390..a1724c890566 100644 --- a/rust/kernel/module_param.rs +++ b/rust/kernel/module_param.rs @@ -130,10 +130,26 @@ pub const fn new(default: T) -> Self { } } + /// Get a copy of the parameter value. + /// + /// Returns the value supplied at module load time, or the default value + /// if the parameter has not been set. + #[inline] + pub fn value(&self) -> T + where + T: Copy, + { + self.value.copy().unwrap_or(self.default) + } + /// Get a shared reference to the parameter value. + /// + /// Returns a reference to the value supplied at module load time, or a + /// reference to the default value if the parameter has not been set. // Note: When sysfs access to parameters are enabled, we have to pass in a // held lock guard here. - pub fn value(&self) -> &T { + #[inline] + pub fn value_ref(&self) -> &T { self.value.as_ref().unwrap_or(&self.default) } diff --git a/samples/rust/rust_minimal.rs b/samples/rust/rust_minimal.rs index 8eb9583571d7..60d03df6cd80 100644 --- a/samples/rust/rust_minimal.rs +++ b/samples/rust/rust_minimal.rs @@ -28,7 +28,7 @@ fn init(_module: &'static ThisModule) -> Result { pr_info!("Am I built-in? {}\n", !cfg!(MODULE)); pr_info!( "test_parameter: {}\n", - *module_parameters::test_parameter.value() + module_parameters::test_parameter.value() ); let mut numbers = KVec::new();