From: Matthew Maurer <mmaurer@google.com>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
Matthew Maurer <mmaurer@google.com>
Subject: [PATCH v2 0/3] Support more safe `AsBytes`/`FromBytes` usage
Date: Tue, 16 Dec 2025 00:44:40 +0000 [thread overview]
Message-ID: <20251216-transmute-v2-0-b23e5277ad02@google.com> (raw)
Currently:
* Slices of `AsBytes`/`FromBytes` types cannot be synthesized from
bytes slices (without unsafe).
* Users must use `unsafe impl` to assert that structs are `AsBytes` or
`FromBytes` and write appropriate justifications.
* Bindgen-generated types cannot implement `AsBytes` or `FromBytes`,
meaning that casting them to or from bytes involves assumptions in the
`unsafe impl` that could easily go out of sync if the underlying
header is edited or an assumption is invalid on a platform the author
did not consider.
This series seeks to address all there of these by:
1. Adding slice cast functions to `FromBytes`
2. Adding a derive for `AsBytes` and `FromBytes`, for now restricted to
the simple case of structs.
3. Refactoring the crate structure to allow the derives added in 2 to be
used on bindgen definitions.
1 or 2 can be taken independently, 3 requires 2.
Signed-off-by: Matthew Maurer <mmaurer@google.com>
---
Changes in v2:
- Reworked to put `transmute` in `ffi` rather than creating a new crate,
per Alice's comment on Zulip.
- Switched to new kernel import style.
- Link to v1: https://lore.kernel.org/r/20251212-transmute-v1-0-9b28e06c6508@google.com
---
Matthew Maurer (3):
rust: transmute: Support transmuting slices of AsBytes/FromBytes types
rust: Add support for deriving `AsBytes` and `FromBytes`
rust: Support deriving `AsBytes`/`FromBytes` on bindgen types
rust/Makefile | 14 ++++---
rust/bindgen_parameters | 8 ++++
rust/bindings/lib.rs | 4 ++
rust/{ffi.rs => ffi/lib.rs} | 5 +++
rust/{kernel => ffi}/transmute.rs | 72 +++++++++++++++++++++++++++++++++
rust/kernel/lib.rs | 2 +-
rust/macros/lib.rs | 83 +++++++++++++++++++++++++++++++++++++++
rust/macros/transmute.rs | 60 ++++++++++++++++++++++++++++
rust/uapi/lib.rs | 4 ++
scripts/generate_rust_analyzer.py | 2 +-
10 files changed, 247 insertions(+), 7 deletions(-)
---
base-commit: 008d3547aae5bc86fac3eda317489169c3fda112
change-id: 20251212-transmute-8ab6076700a8
Best regards,
--
Matthew Maurer <mmaurer@google.com>
next reply other threads:[~2025-12-16 0:44 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-16 0:44 Matthew Maurer [this message]
2025-12-16 0:44 ` [PATCH v2 1/3] rust: transmute: Support transmuting slices of AsBytes/FromBytes types Matthew Maurer
2025-12-17 16:51 ` Daniel Almeida
2025-12-26 20:27 ` Matthew Maurer
2025-12-16 0:44 ` [PATCH v2 2/3] rust: Add support for deriving `AsBytes` and `FromBytes` Matthew Maurer
2025-12-17 3:12 ` Alexandre Courbot
2025-12-17 18:01 ` Matthew Maurer
2025-12-17 19:14 ` Daniel Almeida
2025-12-18 7:23 ` Alexandre Courbot
2025-12-18 8:26 ` Alice Ryhl
2025-12-17 17:35 ` Daniel Almeida
2025-12-17 17:57 ` Matthew Maurer
2025-12-17 19:11 ` Daniel Almeida
2025-12-16 0:44 ` [PATCH v2 3/3] rust: Support deriving `AsBytes`/`FromBytes` on bindgen types Matthew Maurer
2025-12-17 3:15 ` Alexandre Courbot
2025-12-17 18:26 ` Matthew Maurer
2025-12-17 19:26 ` Daniel Almeida
2025-12-17 19:33 ` Matthew Maurer
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=20251216-transmute-v2-0-b23e5277ad02@google.com \
--to=mmaurer@google.com \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--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
all inboxes | Powered by JetHome®