Re: [PATCH v8 07/10] rust: io: introduce `IntoIoVal` trait and single-argument `write_val`
From: Gary Guo
Date: Mon Mar 09 2026 - 11:44:21 EST
On Mon Mar 9, 2026 at 3:14 PM GMT, Alexandre Courbot wrote:
> Some I/O types, like fixed address registers, carry their location
> alongside their values. For these types, the regular `Io::write` method
> can lead into repeating the location information twice: once to provide
> the location itself, another time to build the value.
>
> Add a new `Io::write_val` convenience method that takes a single
> argument implementing `IntoIoVal`, a trait that decomposes implementors
> into a `(location, value)` tuple. This allows write operations on fixed
> offset registers to be done while specifying their name only once.
>
> Signed-off-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
> ---
> rust/kernel/io.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 68 insertions(+)
>
> diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
> index ed6fab001a39..09a0fe06f201 100644
> --- a/rust/kernel/io.rs
> +++ b/rust/kernel/io.rs
> @@ -216,6 +216,22 @@ fn offset(&self) -> usize {
> // Provide the ability to read any primitive type from a [`usize`].
> impl_usize_ioloc!(u8, u16, u32, u64);
>
> +/// Trait implemented by items that contain both an I/O location and a value to assign to it.
> +///
> +/// Implementors can be used with [`Io::write_val`].
> +pub trait IntoIoVal<T, L: IoLoc<T>> {
Is this generics instead of associative types for some reason? If so, please
mention it in the commit mesasge.
> + /// Consumes `self` and returns a `(location, value)` tuple describing a valid I/O write
> + /// operation.
> + fn into_io_val(self) -> (L, T);
> +}
> +
> +/// `(location, value)` tuples can be used as [`IntoIoVal`]s.
> +impl<T, L: IoLoc<T>> IntoIoVal<T, L> for (L, T) {
Missing inline annotations.
Best,
Gary
> + fn into_io_val(self) -> (L, T) {
> + self
> + }
> +}
> +
> /// Types implementing this trait (e.g. MMIO BARs or PCI config regions)
> /// can perform I/O operations on regions of memory.
> ///
> @@ -463,6 +479,32 @@ fn try_write<T, L>(&self, location: L, value: T) -> Result
> Ok(())
> }
>
> + /// Generic fallible write of value bearing its location, with runtime bounds check.
> + ///
> + /// # Examples
> + ///
> + /// Tuples carrying a location and a value can be used with this method:
> + ///
> + /// ```no_run
> + /// use kernel::io::{Io, Mmio};
> + ///
> + /// fn do_writes(io: &Mmio) -> Result {
> + /// // 32-bit write of value `1` at address `0x10`.
> + /// io.try_write_val((0x10, 1u32))
> + /// }
> + /// ```
> + #[inline(always)]
> + fn try_write_val<T, L, V>(&self, value: V) -> Result
> + where
> + L: IoLoc<T>,
> + V: IntoIoVal<T, L>,
> + Self: IoCapable<L::IoType>,
> + {
> + let (location, value) = value.into_io_val();
> +
> + self.try_write(location, value)
> + }
> +
> /// Generic fallible update with runtime bounds check.
> ///
> /// Caution: this does not perform any synchronization. Race conditions can occur in case of
> @@ -559,6 +601,32 @@ fn write<T, L>(&self, location: L, value: T)
> unsafe { self.io_write(io_value, address) }
> }
>
> + /// Generic infallible write of value bearing its location, with compile-time bounds check.
> + ///
> + /// # Examples
> + ///
> + /// Tuples carrying a location and a value can be used with this method:
> + ///
> + /// ```no_run
> + /// use kernel::io::{Io, Mmio};
> + ///
> + /// fn do_writes(io: &Mmio) {
> + /// // 32-bit write of value `1` at address `0x10`.
> + /// io.write_val((0x10, 1u32));
> + /// }
> + /// ```
> + #[inline(always)]
> + fn write_val<T, L, V>(&self, value: V)
> + where
> + L: IoLoc<T>,
> + V: IntoIoVal<T, L>,
> + Self: IoKnownSize + IoCapable<L::IoType>,
> + {
> + let (location, value) = value.into_io_val();
> +
> + self.write(location, value)
> + }
> +
> /// Generic infallible update with compile-time bounds check.
> ///
> /// Caution: this does not perform any synchronization. Race conditions can occur in case of