Re: [PATCH 1/2] rust: impl_flags: add conversion functions
From: Miguel Ojeda
Date: Sun Feb 15 2026 - 15:54:05 EST
On Sun, Feb 15, 2026 at 9:23 PM Andreas Hindborg <a.hindborg@xxxxxxxxxx> wrote:
>
> + impl ::core::convert::From<$flag> for $ty {
> + #[inline]
> + fn from(value: $flag) -> Self {
> + // SAFETY: All `$flag` values are valid for use as `$ty`.
> + unsafe { core::mem::transmute::<$flag, $ty>(value) }
> + }
> + }
Why do we need the transmute instead of a cast? Did you notice a difference?
Same in the other one -- why not constructing the object normally?
Also, you can use `Self` for `$ty` and `$flags` to make it easier to read.
In addition, I would suggest an early return with a simple `if`.
Finally, it wouldn't hurt adding a one-liner example for each at the top.
Thanks!
Cheers,
Miguel