Skip to content

Conversation

@mematthias
Copy link
Contributor

This PR adjusts the generated __BindgenUnionField implementation to be clearer about unsafety and compatible with the Rust 2024 unsafe-op-in-unsafe-fn rules: https://doc.rust-lang.org/edition-guide/rust-2024/unsafe-op-in-unsafe-fn.html

In Rust 2024, unsafe operations inside an unsafe fn must be wrapped in an explicit unsafe {} block. Instead of relying on BindgenContext::wrap_unsafe_ops, the generated code now directly wraps the mem::transmute(self) call in unsafe { ... }, making the unsafety explicit at the operation site.

Additionally, the generated methods are now const:

  • new is emitted as pub const fn
  • as_ref / as_mut are emitted as pub const unsafe fn

This is possible because mem::transmute is available in const contexts since Rust 1.56. The change simplifies the output and improves forward compatibility without changing the observable behavior of the bindings.

@emilio emilio added this pull request to the merge queue Dec 31, 2025
Merged via the queue into rust-lang:main with commit 82911a2 Dec 31, 2025
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants