Re: [PATCH v6 RESEND 5/7] rust: io: factor out MMIO read/write macros
From: Zhi Wang
Date: Fri Nov 14 2025 - 11:07:21 EST
On Thu, 13 Nov 2025 16:36:47 +0900
"Alexandre Courbot" <acourbot@xxxxxxxxxx> wrote:
> I understand the intent from the commit message, but this is starting
> to look like an intricate maze of macro expansion and it might not be
> as easy for first-time readers - could you add an explanatory
> doccomment for these?
>
Sure.
> > +
> > macro_rules! define_read {
> > - (infallible, $(#[$attr:meta])* $vis:vis $name:ident,
> > $c_fn:ident -> $type_name:ty) => {
> > + (infallible, $(#[$attr:meta])* $vis:vis $name:ident,
> > $call_macro:ident, $c_fn:ident ->
> > + $type_name:ty) => {
> > /// Read IO data from a given offset known at compile time.
> > ///
> > /// Bound checks are performed on compile time, hence if
> > the offset is not known at compile @@ -135,12 +161,13 @@
> > macro_rules! define_read { $vis fn $name(&self, offset: usize) ->
> > $type_name { let addr = self.io_addr_assert::<$type_name>(offset);
> >
> > - // SAFETY: By the type invariant `addr` is a valid
> > address for MMIO operations.
> > - unsafe { bindings::$c_fn(addr as *const c_void) }
> > + // SAFETY: By the type invariant `addr` is a valid
> > address for IO operations.
> > + $call_macro!(infallible, $c_fn, self, $type_name, addr)
> > }
> > };
>
> To convey the fact that `$c_fn` is passed to `$call_macro`, how about
> changing the syntax to something like
>
> `define_read(infallible, $vis $name $call_macro($c_fn) ->
> $type_name`
>
> ?
Then the macros will look like:
define_read!(infallible, pub read32 call_mmio_read!(ioread32) -> u32);
define_write!(infallible, pub write32 call_mmio_write!(iowrite32) ->
u32);
The readability is indeed better - I’ll take that, thanks. :)
Z.