mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-09-18 23:09:29 +02:00
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 <aliceryhl@google.com> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> Reviewed-by: Petr Pavlu <petr.pavlu@suse.com> Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Petr Pavlu <petr.pavlu@suse.com>
This commit is contained in:
committed by
Petr Pavlu
parent
6b722d1ec0
commit
327d44754c
@@ -130,10 +130,26 @@ impl<T> ModuleParamAccess<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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)
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ impl kernel::Module for RustMinimal {
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user