Re: [PATCH v2 06/11] rust: io: add view type

Andreas Hindborg
2026-04-28T10:53:39Z

Gary Guo <gary@garyguo.net> writes:

> The view may be created statically via I/O projection using `io_project!()`
> macro to perform compile-time checks, or created by type-casting an
> existing view type with `try_cast()` function, where the size and alignment
> checks are performed at runtime.
>
> Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
> Signed-off-by: Gary Guo <gary@garyguo.net>
> ---
> rust/kernel/io.rs | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 146 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
> index a13be8c5fd2d..869071d47a13 100644
> --- a/rust/kernel/io.rs
> +++ b/rust/kernel/io.rs
> @@ -7,7 +7,11 @@
> use crate::{
> bindings,
> prelude::*,
> - ptr::KnownSize, //
> + ptr::KnownSize,
> + transmute::{
> + AsBytes,
> + FromBytes, //
> + }, //
> };
>
> pub mod mem;
> @@ -297,6 +301,13 @@ pub trait Io {
> /// Type of this I/O region. For untyped I/O regions, [`Region`] type can be used.
> type Type: ?Sized + KnownSize;
>
> + /// Get a [`View`] covering the entire region.
> + #[inline]
> + fn as_view(&self) -> View<'_, Self, Self::Type> {
> + // SAFETY: This is an empty projection, so it trivially satisfies the invariant.
> + unsafe { View::new_unchecked(self, self.as_ptr()) }
> + }
> +

Sorry, I missed your reply on v1. This is better. Should it be "identity
projection" rather than "empty projection", or is "emtpy projection" the
correct term?

However, I don't see why we cannot put:

SAFETY:
- By existence of `&self`, `self.as_ptr()` is aligned.
- `self.as_ptr()` has same provenance as `self.as_ptr()`.
- `self.byte_offset_from(self.as_ptr())` is 0.

I think the verbosity is fine. It makes it easier to check the safety
preconditions when scanning the code.

Best regards,
Andreas Hindborg