Re: [PATCH v4 6/6] rust: bitfield: Use 'as' operator for setter type conversion

Alexandre Courbot
2025-09-29T06:47:15Z

On Sun Sep 21, 2025 at 3:22 AM JST, Joel Fernandes wrote:
> The bitfield macro's setter currently uses the From trait for type
> conversion, which is overly restrictive and prevents use cases such as
> narrowing conversions (e.g., u32 storage size to u8 field size) which
> aren't supported by From.
>
> Replace 'from' with 'as' in the setter implementation to support this.
>
> Suggested-by: Yury Norov <yury.norov@gmail.com>

Can you add a `Link: ` tag to the discussion for context?

But I am not really convinced this is needed or desirable at all. Where
would it make sense to define a field that is larger that its containing
type? This looks like it can introduce confusion or errors. It's already
not ideal that we can pass values that would be truncated; but this
makes it worse.

Anyway, if we decide to keep this, I think you want to remove the

+//! Note that the compiler will error out if the size of the setter's
arg exceeds the
+//! struct's storage size.

bit that was introduced in patch 2.