Re: [PATCH 07/10] rust: io: add subregion method with compile-time check
From: Alexandre Courbot
Date: Tue Jul 28 2026 - 03:28:13 EST
On Wed Jul 22, 2026 at 1:54 AM JST, Gary Guo wrote:
> Add a helper function that allows obtaining a subregion from region with
> compile-time check. This can be used by drivers to obtaining a subregion
> for relative register access.
>
> Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
> ---
> rust/kernel/io.rs | 24 ++++++++++++++++++++++++
> 1 file changed, 24 insertions(+)
>
> diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
> index 0f8b166a6d3b..f31d37c8a3d5 100644
> --- a/rust/kernel/io.rs
> +++ b/rust/kernel/io.rs
> @@ -80,6 +80,30 @@ pub fn ptr_try_from_raw_parts_mut(base: *mut u8, size: usize) -> Result<*mut Sel
>
> Ok(Self::ptr_from_raw_parts_mut(base, size))
> }
> +
> + /// Create a subregion with provided offset and size.
> + #[inline]
> + pub fn subregion<'a, const OFFSET: usize, const NEW_SIZE: usize, IO>(
> + io: IO,
> + ) -> <IO::Backend as IoBackend>::View<'a, Region<NEW_SIZE>>
> + where
> + IO: IoBase<'a, Target = Self>,
> + {
> + const_assert!(
> + OFFSET + NEW_SIZE <= SIZE && OFFSET.is_multiple_of(4) && NEW_SIZE.is_multiple_of(4)
We are starting to have quite a bit of these magic `4` throughout the
file (they are also used in `ptr_try_from_raw_parts_mut`). It's a bit
heavier, but how about using the semantically more meaningful
`Self::MIN_ALIGN.as_usize()`?