Re: [PATCH v9 2/5] rust: io: factor common I/O helpers into Io trait
From: Gary Guo
Date: Fri Jan 16 2026 - 10:23:55 EST
On Fri Jan 16, 2026 at 10:44 AM GMT, Alice Ryhl wrote:
> On Thu, Jan 15, 2026 at 11:26:46PM +0200, Zhi Wang wrote:
>> The previous Io<SIZE> type combined both the generic I/O access helpers
>> and MMIO implementation details in a single struct.
>>
>> To establish a cleaner layering between the I/O interface and its concrete
>> backends, paving the way for supporting additional I/O mechanisms in the
>> future, Io<SIZE> need to be factored.
>>
>> Factor the common helpers into new {Io, Io64} traits, and move the
>> MMIO-specific logic into a dedicated Mmio<SIZE> type implementing that
>> trait. Rename the IoRaw to MmioRaw and update the bus MMIO implementations
>> to use MmioRaw.
>>
>> No functional change intended.
>>
>> Cc: Alexandre Courbot <acourbot@xxxxxxxxxx>
>> Cc: Alice Ryhl <aliceryhl@xxxxxxxxxx>
>> Cc: Bjorn Helgaas <helgaas@xxxxxxxxxx>
>> Cc: Danilo Krummrich <dakr@xxxxxxxxxx>
>> Cc: John Hubbard <jhubbard@xxxxxxxxxx>
>> Signed-off-by: Zhi Wang <zhiw@xxxxxxxxxx>
>
>> +pub trait IoBase {
>> +pub trait IoKnownSize: IoBase {
>> +pub trait Io: IoBase {
>> +pub trait IoKnownSize64: IoKnownSize {
>> +pub trait Io64: Io {
>
> The following combinations are possible:
>
> 1. IoBase
> 2. IoBase + Io
> 3. IoBase + IoKnownSize
> 4. IoBase + Io + IoKnownSize
> 5. IoBase + Io + Io64
> 6. IoBase + Io + Io64 + IoKnownSize
> 7. IoBase + IoKnownSize + IoKnownSize64
> 8. IoBase + Io + IoKnownSize + IoKnownSize64
> 9. IoBase + Io + IoKnownSize + Io64 + IoKnownSize64
>
> I'm not sure all of them make sense. I can't see a scenario where I
> would pick 1, 3, 6, 7, or 8.
>
> How about this trait hierachy? I believe I suggested something along
> these lines before.
>
> pub trait Io {
> pub trait Io64: Io {
> pub trait IoKnownSize: Io {
>
> With these traits, these scenarios are possible:
>
> 1. Io
> 2. Io + Io64
> 3. Io + IoKnownSize
> 4. Io + Io64 + IoKnownSize
>
> which seems to be the actual set of cases we care about.
>
> Note that IoKnownSize can have methods that only apply when Io64 is
> implemented:
>
> trait IoKnownSize: Io {
> /// Infallible 8-bit read with compile-time bounds check.
> fn read8(&self, offset: usize) -> u8;
>
> /// Infallible 64-bit read with compile-time bounds check.
> fn read64(&self, offset: usize) -> u64
> where
> Self: Io64;
> }
I like this.
I wonder if we can keep all methods on `Io` trait. And then have marker trait to
represent capability on performing Io access.
Something like:
trait IoCapable<T> {}
trait Io {
fn read8(&self, offset: usize) -> u8 where Self: IoCapable<u8>;
fn read16(&self, offset: usize) -> u16 where Self: IoCapable<u16>;
fn read32(&self, offset: usize) -> u32 where Self: IoCapable<u32>;
fn read64(&self, offset: usize) -> u64 where Self: IoCapable<u64>;
}
Then you have a single (non-marker) trait and not a hierachy of them.
Best,
Gary