Re: [PATCH v5 16/20] rust: io: add `read_val` and `write_val` functions on `Io`

From: Alexandre Courbot

Date: Fri Jul 03 2026 - 10:50:40 EST


On Fri Jun 26, 2026 at 11:45 PM JST, Gary Guo wrote:
> Provide `read_val` and `write_val` that allow I/O views to be accessed when
> they're narrowed down to just views of primitives.
>
> This is used to provide `io_read!` and `io_write!` macros, which are
> generalized version of current `dma_read!` and `dma_write!` macro that work
> for all types that implement `Io`.
>
> Note though `io_read!` and `io_write!` only works if backend implements
> `IoCapable` for the type; which is typically only implemented for
> atomically accessible primitives. `dma_read!` and `dma_write!` currently
> supports them via `read_volatile` and `write_volatile`; this can be
> undesirable for aggregates as LLVM may turn them to multiple instructions
> to access parts and re-assemble, even if they could be combined to a single
> instruction. Thus, `io_read!()` and `io_write!()` does not fully replace
> `dma_read!()` and `dma_write!()` in this scenario. The ability to
> read/write aggregates (when atomicity is of no concern) is better served
> with copying primitives (e.g. memcpy_{from,to}io).
>
> Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>

Reviewed-by: Alexandre Courbot <acourbot@xxxxxxxxxx>

<...>
> @@ -1225,3 +1269,63 @@ macro_rules! io_project {
> }
> #[doc(inline)]
> pub use crate::io_project;
> +
> +/// Read from I/O memory.
> +///
> +/// The syntax is of form `io_read!(io, proj)` where `io` is an expression to a type that
> +/// implements [`Io`] and `proj` is a [projection specification](kernel::ptr::project!).
> +///
> +/// # Examples
> +///
> +/// ```
> +/// struct MyStruct { field: u32, }

Same as patch 13, maybe this should be `#[repr(C)]` to give the good
example to readers.

(same for the other example below)