Re: [PATCH 5/7] rust: io: let I/O locations carry an access mode
From: Benno Lossin
Date: Wed Oct 21 2026 - 06:07:46 EST
On Tue Oct 20, 2026 at 1:23 PM BST, Gary Guo wrote:
> On Thu Oct 15, 2026 at 1:39 PM BST, Daniel Almeida wrote:
>> `Io::read`, `Io::write`, `Io::update` and their fallible variants
>> project the view to the given `IoLoc` and access it there. A location
>> has no say in which accesses are allowed, so a register cannot be
>> declared read-only or write-only.
>>
>> Add the `io::access` module with the `Mode` trait and the `ReadWrite`,
>> `ReadOnly` and `WriteOnly` modes, and add an `Access` associated type to
>> `IoLoc`. Projecting through a location applies its mode: `ReadWrite`
>> leaves the view as it is, while `ReadOnly` and `WriteOnly` wrap it in the
>> `ReadOnly` and `WriteOnly` views. The bounds of the generic accessors now
>> apply to the backend of the restricted view, so a write to a read-only
>> location, or a read from a write-only one, does not compile.
>>
>> Raw offsets are `ReadWrite`. `OffsetLoc` takes the mode as a type
>> parameter that defaults to `ReadWrite`, and `RegisterArray` gets an
>> `Access` type, which `register!` sets to `ReadWrite` for now.
>>
>> No existing location changes its mode, so there is no functional change.
>> A following patch lets `register!` declare read-only and write-only
>> registers.
>
> So far, without considering access modes, we have a clean mapping after the
> register projection series that a `register!()` macro just generates a custom
> projection. However, this commit makes it special again and this isn't possible
> with struct definitions, so it breaks feature composibility. It is also somewhat
> surprising in the sense that it changes the base view type.
>
> What about making RO/WO wrapper types on actual data? Something like
>
> Mmio<'_, ReadOnly<T>>
>
> and
>
> Mmio<'_, WriteOnly<T>>
>
> Then we can implement
>
> impl<T, B> IoRead<ReadOnly<T>> for B where B: IoRead<T> {}
>
> and
>
> impl<T, B> IoWrite<WriteOnly<T>> for B where B: IoWrite<T> {}
>
> ReadOnly will not implement FromBytes, and WriteOnly will not implement
> IntoBytes, to avoid people using them with `copy_read`/`copy_write` API.
>
> This way, the design works for struct too. One issue of this, though, is that
> you cannot project from `Mmio<'_, ReadOnly<Struct>>` to `Mmio<'_,
> ReadOnly<Field>>` currently. However, I believe this is solved with the "place
> wrapper" concept in field projection.
indeed. if you implement `PlaceWrapper` for `ReadOnly` correctly, then
you can do that projection "as if the wrapper wasn't there".
> Regardless, before we proceed with any design, I'd like to hear opinions from
> Benno on how this interacts with the field projection design as language
> feature.
not sure what interaction you are referring to. I read Daniel's patch,
but I can't infer what you mean by "this" :)
best,
Benno