Re: [PATCH v8 07/10] rust: io: introduce `IntoIoVal` trait and single-argument `write_val`
From: Gary Guo
Date: Wed Mar 11 2026 - 13:21:10 EST
On Wed Mar 11, 2026 at 4:22 PM GMT, Danilo Krummrich wrote:
> On Wed Mar 11, 2026 at 4:42 PM CET, Gary Guo wrote:
>> On Wed Mar 11, 2026 at 2:56 PM GMT, Danilo Krummrich wrote:
>>>
>>> fn write<T, L>(&self, location: L, value: T)
>>> where
>>> L: IoLoc<T>,
>>> Self: IoKnownSize + IoCapable<L::IoType>,
>>>
>>> Which is the reason why L: IoLoc<T>, i.e. the relative location is on T, not on
>>> L.
>>
>> The location has nothing to do with `value`, just the type `T`.
>
> (Cutting all the above, since it all boils down to the same thing.)
>
> So, that's what I mean, the relative location is on type T (the value type), not
> on L (the location type), hence T can't just be an arbitrary primitive, no?
The location is on the trait impl of `L`, it just may depend on `T`.
For a register location, say, `UART_TX`, it is itself contain full information
on location; the `IoLoc<u8>` impl of it would be just used to constrain the type
so that no other types than u8 gets used.
Yes, for the case of `()` or `WithBase`, it is itself only partial information
and the location is only complete with an associated type. But this is more just
a type-system trick so that we can avoid spelling out the full register name.
For the `write_val` case for a bitfield, I would basically consider the canonical
way of doing a register write is
bar.write(REGISTER_NAME, bitfield)
value. Where
bar.write((), bitfield)
or (`write_val`) is a nice sugar to avoid writing the name multiple times.
>
>>> So, what you could say is
>>>
>>> - read() takes an absolute location and returns something that encodes a value
>>> and relative location
>>>
>>> - write() takes a base location (or absolute location) and something that
>>> encodes a value and relative location
>>
>> Only `location` gives you the location. Value has nothing to do with locations at all. Type
>> inference does the trick.
>
> The value does have something to do with the relative location, as it comes from
> the value type. You can't pass an "independent" value whose type does not carry
> location information, can you?
You can also not pass an "independent" value to register of a different type.
With my FIFO example, you cannot write
bar.write(UART_TX, 0u32)
because `UART_TX: IoLoc<u8>` only. It doesn't mean that the `0u32` suddenly
start carrying location information.
I just view this as type system working as designed.
Best,
Gary