From: Matthew Maurer <matthew.r.maurer@gmail.com>
To: Igor Korotin <igor.korotin.linux@gmail.com>
Cc: "Danilo Krummrich" <dakr@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
rust-for-linux@vger.kernel.org
Subject: Re: Rust: Manual reimplementation of C unions and layout testing
Date: Tue, 21 Oct 2025 14:22:52 -0700 [thread overview]
Message-ID: <CAM22NNBbQ2w1GDT5ep-ccwV1zWP-OpfRiM=CvzbPoYdpie6rmw@mail.gmail.com> (raw)
In-Reply-To: <af7a8880-e166-4e1a-851d-31b731c2d2fa@gmail.com>
On Tue, Oct 21, 2025 at 2:04 PM Igor Korotin
<igor.korotin.linux@gmail.com> wrote:
>
> Hello all,
>
> As you might know, I'm working on the Rust side of the I2C subsystem,
> and currently looking into representing `struct i2c_algorithm`.
>
> In C, this struct contains two anonymous unions for callbacks.
In the specific case of `struct i2c_algorithm`, all the unions are
solely for the sake of supporting both a deprecated name and a new
name, e.g.`master_xfer` (deprecated) vs `xfer` (current). Since the
Rust bindings should never need to reference the deprecated name, you
could consider proposing an `#ifdef __BINDGEN__` which removes the
`union` and *only* includes the new name.
> bindgen generates rather awkward names for them, which makes the Rust
> bindings less readable and less stable.
You should only need to reference the bindings name in exactly one
location, when translating from your Rust representation to the
bindings C representation or vice versa. You could also apply a
wrapper struct and create accessors if you really don't want to be
proxying between two types.
>
> I'm considering declaring this structure manually in i2c.rs, effectively
> copying the layout that bindgen would generate but renaming the union
> fields for clarity. To avoid accidental ABI drift, I could add
> compile-time assertions to ensure the Rust and C layouts remain compatible.
The problem is that those compile-time assertions will require to to
reference the bindgen struct, or invoke some kind of C parser on the
header or debug information, which is much more complex than just
referencing the field.
>
> Would that be an acceptable approach?
> Any thoughts or preferences on how such layout checks should be done
> in-tree?
tl;dr: Such layout checks should be done by bindgen, and I don't think
that unions where you only ever need to reference one arm ought to be
considered complex enough that we try to implement custom layout
checks.
>
> Best regards,
> Igor Korotin
>
prev parent reply other threads:[~2025-10-21 21:23 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-21 21:02 Rust: Manual reimplementation of C unions and layout testing Igor Korotin
2025-10-21 21:22 ` Matthew Maurer [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAM22NNBbQ2w1GDT5ep-ccwV1zWP-OpfRiM=CvzbPoYdpie6rmw@mail.gmail.com' \
--to=matthew.r.maurer@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=igor.korotin.linux@gmail.com \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).