Re: [PATCH v2 3/3] rust: Support deriving `AsBytes`/`FromBytes` on bindgen types

From: Matthew Maurer

Date: Wed Dec 17 2025 - 13:27:28 EST


On Tue, Dec 16, 2025 at 7:16 PM Alexandre Courbot <acourbot@xxxxxxxxxx> wrote:
>
> On Tue Dec 16, 2025 at 9:44 AM JST, Matthew Maurer wrote:
> > To support this, we need to move the `transmute` module into a separate
> > crate to allow the `bindings` crate to depend on it. Most user code is
> > still expected to address the module as `kernel::transmute`, which is a
> > re-export. `ffi::transmute` is now available for use in `bindings`.
> >
> > Signed-off-by: Matthew Maurer <mmaurer@xxxxxxxxxx>
>
> Maybe this should be two commits, one for the new crate, another one to
> introduce the ability to use on bindgen types.

I could do this if people think it'd be helpful, but moving it into
the `ffi` crate has no point without using on the main set of bindgen
types.

>
> I have tried this with the Nova bindings, and somehow could not get past
> this error:
>
> error[E0433]: failed to resolve: could not find `ffi` in the list of imported crates
> --> ../drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs:321:54
> |
> 321 | #[derive(Debug, Default, Copy, Clone, MaybeZeroable, FromBytesFfi)]
> | ^^^^^^^^^^^^ could not find `ffi` in the list of imported crates
> |
> = note: this error originates in the derive macro `FromBytesFfi` (in Nightly builds, run with -Z macro-backtrace for more info)
>
> I do have `kernel::ffi` imported in the bindings module though, so I am
> not quite sure what this is about. Any idea?

If you have the kernel module available, please set the bindgen flag
to apply `FromBytes` and `AsBytes` rather than `FromBytesFfi` and
`AsBytesFfi`. Those are only for the crates that are used by the
kernel crate, and so do not have access to the `kernel` crate.

The macro itself will generate `::kernel::transmute::FromBytes` when
you use `FromBytes` and `::ffi::transmute::FromBytes` when you use
`FromBytesFfi`, specifically to avoid weirdness around namespaces and
imports.

If for some reason you really wanted to use `FromBytesFfi` (you
shouldn't, but just so you understand the mechanism), you'd need to
add a direct dependency edge on the `ffi` crate, i.e. `--extern crate
ffi`