Re: [PATCH v2 01/16] rust: io: add static `cast()` method for views
From: Alexandre Courbot
Date: Mon Aug 10 2026 - 05:30:02 EST
On Thu Aug 6, 2026 at 1:35 AM JST, Gary Guo wrote:
> Add a compile-time checked variant of `try_cast()` using the minimum size
> and alignment information.
>
> Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
> ---
> rust/kernel/io.rs | 39 +++++++++++++++++++++++++++++++++++++++
> 1 file changed, 39 insertions(+)
>
> diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
> index a38c20ba3d23..adfc555de7d0 100644
> --- a/rust/kernel/io.rs
> +++ b/rust/kernel/io.rs
> @@ -436,6 +436,45 @@ fn is_empty<T>(self) -> bool
> self.len() == 0
> }
>
> + /// Convert into a different typed I/O view.
> + ///
> + /// The target type must be known (statically) to be of the same or smaller size to current
> + /// type, and the current view is properly aligned for the target type.
grammar nit: s/is/must be.
> + ///
> + /// # Examples
> + ///
> + /// ```no_run
> + /// use kernel::io::{
> + /// io_project,
> + /// Mmio,
> + /// Io,
> + /// Region,
> + /// };
> + /// #[derive(FromBytes, IntoBytes)]
> + /// #[repr(C)]
> + /// struct MyStruct { field: u32, }
> + ///
> + /// # fn test(mmio: &Mmio<'_, Region<0x1000>>) {
> + /// // let mmio: Mmio<'_, Region>;
Should this be `Region<0x1000>`? Because as written the `cast` right
below wouldn't build since `Region::MIN_SIZE == 0`.
Also, looking at Sashiko's comment about `KnownSize` we should maybe
bite the bullet and make it unsafe? There was some talk about it [1] but
the reason why we kept it safe back then doesn't seem to protect us
here.
[1] https://lore.kernel.org/rust-for-linux/DGSDGDIVUHO0.P594H9B4LLO5@xxxxxxxxxx/