Re: [PATCH v6 5/9] rust: io: add IoRef and IoWrite types
From: Alice Ryhl
Date: Mon Feb 16 2026 - 04:02:53 EST
On Mon, Feb 16, 2026 at 05:04:41PM +0900, Alexandre Courbot wrote:
> I/O accesses are defined by the following properties:
>
> - For reads, a start address, a width, and a type to interpret the read
> value as,
> - For writes, the same as above, and a value to write.
>
> Introduce the `IoRef` trait, which allows implementing types to specify
> the address a type expects to be accessed at, as well as the width of
> the access, and the user-facing type used to perform the access.
>
> This allows read operations to be made generic with the `read` method
> over an `IoRef` argument.
>
> Write operations need a value to write on top of the `IoRef`: fulfill
> that purpose with the `IoWrite`, which is the combination of an `IoRef`
> and a value of the type it expects. This allows write operations to be
> made generic with the `write` method over a single `IoWrite` argument.
>
> The main purpose of these new entities is to allow register types to be
> written using these generic `read` and `write` methods of `Io`.
>
> Co-developed-by: Gary Guo <gary@xxxxxxxxxxx>
> Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
> Signed-off-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
> ---
> rust/kernel/io.rs | 243 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 243 insertions(+)
>
> diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
> index b150743ffa4f..6da8593f7858 100644
> --- a/rust/kernel/io.rs
> +++ b/rust/kernel/io.rs
> @@ -173,6 +173,160 @@ pub trait IoCapable<T> {
> unsafe fn io_write(&self, value: T, address: usize);
> }
>
> +/// Reference to an I/O location, describing the offset, width, and return type of an access.
In the next patch you implement this for usize, but here you say it's a
reference to an I/O location. I'm pretty sure usize is not a reference
to an I/O location.
> +/// This trait is the key abstraction allowing [`Io::read`], [`Io::write`], and [`Io::update`]
> +/// to work uniformly with both raw `usize` offsets (for primitive types like `u32`) and typed
> +/// references.
> +///
> +/// An `IoRef<T>` carries three pieces of information:
> +///
> +/// - The offset to access (returned by [`IoRef::offset`]),
> +/// - The width of the access (determined by [`IoRef::IoType`]),
> +/// - The type `T` in which data is returned or provided.
> +///
> +/// `T` and `IoType` may differ: for instance, a typed register has `T` = the register type with
> +/// its bitfields, and `IoType` = its backing primitive (e.g. `u32`), with `From`/`Into`
> +/// conversions between them.
> +///
> +/// An `IoRef` can be passed directly to [`Io::read`] or [`Io::try_read`] to obtain a value, or
> +/// turned into an [`IoWrite`] via [`IoRef::set`] to be passed to [`Io::write`] or
> +/// [`Io::try_write`].
> +pub trait IoRef<T>: Copy
> +where
> + T: From<Self::IoType> + Into<Self::IoType>,
Prefer to use Into for trait bounds:
where
T: Into<Self::IoType>,
Self::IoType: Into<T>,
Alice