From: Andreas Hindborg <a.hindborg@kernel.org> Date: 2026-03-23 12:48:36
Rename the existing `value()` method to `value_ref()` which returns a
shared reference to the parameter value, and add a new `value()`
method on `ModuleParamAccess<T>` where `T: Copy` that returns the
value by copy.
This provides a more ergonomic API for the common case where the
parameter type implements `Copy`, avoiding the need to explicitly
dereference the return value at call sites.
Currently `value_ref()` has no in-tree callers, but it will be needed
when support for non-`Copy` parameter types such as arrays and
strings is added.
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
This change was suggested at [1].
Link: https://lore.kernel.org/r/87cy13swpw.fsf@t14s.mail-host-address-is-not-set [1]
---
rust/kernel/module_param.rs | 11 ++++++++++-
samples/rust/rust_minimal.rs | 2 +-
2 files changed, 11 insertions(+), 2 deletions(-)
@@ -134,7 +134,7 @@ pub const fn new(default: T) -> Self {/// Get a shared reference to the parameter value.// Note: When sysfs access to parameters are enabled, we have to pass in a// held lock guard here.-pubfnvalue(&self)->&T{+pubfnvalue_ref(&self)->&T{self.value.as_ref().unwrap_or(&self.default)}
@@ -146,6 +146,15 @@ pub const fn as_void_ptr(&self) -> *mut c_void {}}+impl<T:Copy>ModuleParamAccess<T>{+/// Get a copy of the parameter value.+// Note: When sysfs access to parameters are enabled, we have to pass in a+// held lock guard here.+pubfnvalue(&self)->T{+self.value.copy().unwrap_or(self.default)+}+}+#[doc(hidden)]/// Generate a static [`kernel_param_ops`](srctree/include/linux/moduleparam.h) struct.///
From: Alice Ryhl <aliceryhl@google.com> Date: 2026-03-23 12:49:53
On Mon, Mar 23, 2026 at 1:48 PM Andreas Hindborg [off-list ref] wrote:
Rename the existing `value()` method to `value_ref()` which returns a
shared reference to the parameter value, and add a new `value()`
method on `ModuleParamAccess<T>` where `T: Copy` that returns the
value by copy.
This provides a more ergonomic API for the common case where the
parameter type implements `Copy`, avoiding the need to explicitly
dereference the return value at call sites.
Currently `value_ref()` has no in-tree callers, but it will be needed
when support for non-`Copy` parameter types such as arrays and
strings is added.
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
On Mon Mar 23, 2026 at 12:47 PM GMT, Andreas Hindborg wrote:
quoted hunk
Rename the existing `value()` method to `value_ref()` which returns a
shared reference to the parameter value, and add a new `value()`
method on `ModuleParamAccess<T>` where `T: Copy` that returns the
value by copy.
This provides a more ergonomic API for the common case where the
parameter type implements `Copy`, avoiding the need to explicitly
dereference the return value at call sites.
Currently `value_ref()` has no in-tree callers, but it will be needed
when support for non-`Copy` parameter types such as arrays and
strings is added.
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
This change was suggested at [1].
Link: https://lore.kernel.org/r/87cy13swpw.fsf@t14s.mail-host-address-is-not-set [1]
---
rust/kernel/module_param.rs | 11 ++++++++++-
samples/rust/rust_minimal.rs | 2 +-
2 files changed, 11 insertions(+), 2 deletions(-)
@@ -134,7 +134,7 @@ pub const fn new(default: T) -> Self {/// Get a shared reference to the parameter value.// Note: When sysfs access to parameters are enabled, we have to pass in a// held lock guard here.-pubfnvalue(&self)->&T{+pubfnvalue_ref(&self)->&T{self.value.as_ref().unwrap_or(&self.default)}
@@ -146,6 +146,15 @@ pub const fn as_void_ptr(&self) -> *mut c_void {}}+impl<T:Copy>ModuleParamAccess<T>{+/// Get a copy of the parameter value.+// Note: When sysfs access to parameters are enabled, we have to pass in a+// held lock guard here.+pubfnvalue(&self)->T{
It's better to keep this close to `value_ref` in the same impl block. The `T:
Copy` bound doesn't need to be on the impl block, it can be on the item itself
with
pub fn value(&self) -> T where T: Copy
Best,
Gary
From: Andreas Hindborg <a.hindborg@kernel.org> Date: 2026-03-23 13:23:25
"Gary Guo" [off-list ref] writes:
On Mon Mar 23, 2026 at 12:47 PM GMT, Andreas Hindborg wrote:
quoted
Rename the existing `value()` method to `value_ref()` which returns a
shared reference to the parameter value, and add a new `value()`
method on `ModuleParamAccess<T>` where `T: Copy` that returns the
value by copy.
This provides a more ergonomic API for the common case where the
parameter type implements `Copy`, avoiding the need to explicitly
dereference the return value at call sites.
Currently `value_ref()` has no in-tree callers, but it will be needed
when support for non-`Copy` parameter types such as arrays and
strings is added.
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
This change was suggested at [1].
Link: https://lore.kernel.org/r/87cy13swpw.fsf@t14s.mail-host-address-is-not-set [1]
---
rust/kernel/module_param.rs | 11 ++++++++++-
samples/rust/rust_minimal.rs | 2 +-
2 files changed, 11 insertions(+), 2 deletions(-)
@@ -134,7 +134,7 @@ pub const fn new(default: T) -> Self {/// Get a shared reference to the parameter value.// Note: When sysfs access to parameters are enabled, we have to pass in a// held lock guard here.-pubfnvalue(&self)->&T{+pubfnvalue_ref(&self)->&T{self.value.as_ref().unwrap_or(&self.default)}
@@ -146,6 +146,15 @@ pub const fn as_void_ptr(&self) -> *mut c_void {}}+impl<T:Copy>ModuleParamAccess<T>{+/// Get a copy of the parameter value.+// Note: When sysfs access to parameters are enabled, we have to pass in a+// held lock guard here.+pubfnvalue(&self)->T{
It's better to keep this close to `value_ref` in the same impl block. The `T:
Copy` bound doesn't need to be on the impl block, it can be on the item itself
with
pub fn value(&self) -> T where T: Copy
Cool, I'll do that.
Best regards,
Andreas Hindborg