Re: [PATCH 1/2] rust: impl_flags: add conversion functions
From: Andreas Hindborg
Date: Tue May 12 2026 - 04:07:30 EST
Miguel Ojeda <miguel.ojeda.sandonis@xxxxxxxxx> writes:
> 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?
No, I did not think of using `as`. I can do that.
> Same in the other one -- why not constructing the object normally?
Will do.
> Also, you can use `Self` for `$ty` and `$flags` to make it easier to read.
Ok.
>
> In addition, I would suggest an early return with a simple `if`.
Ok.
>
> Finally, it wouldn't hurt adding a one-liner example for each at the top.
I'll add an example.
Best regards,
Andreas Hindborg