[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20250929204641.GA3056955@joelbox2>
Date: Mon, 29 Sep 2025 16:46:41 -0400
From: Joel Fernandes <joelagnelf@...dia.com>
To: Miguel Ojeda <miguel.ojeda.sandonis@...il.com>
Cc: linux-kernel@...r.kernel.org, rust-for-linux@...r.kernel.org,
dri-devel@...ts.freedesktop.org, dakr@...nel.org,
acourbot@...dia.com, Alistair Popple <apopple@...dia.com>,
Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
bjorn3_gh@...tonmail.com, Benno Lossin <lossin@...nel.org>,
Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>,
David Airlie <airlied@...il.com>, Simona Vetter <simona@...ll.ch>,
Maarten Lankhorst <maarten.lankhorst@...ux.intel.com>,
Maxime Ripard <mripard@...nel.org>,
Thomas Zimmermann <tzimmermann@...e.de>,
John Hubbard <jhubbard@...dia.com>, Timur Tabi <ttabi@...dia.com>,
joel@...lfernandes.org, Elle Rhumsaa <elle@...thered-steel.dev>,
Yury Norov <yury.norov@...il.com>,
Daniel Almeida <daniel.almeida@...labora.com>,
nouveau@...ts.freedesktop.org
Subject: Re: [PATCH v4 6/6] rust: bitfield: Use 'as' operator for setter type
conversion
On Mon, Sep 29, 2025 at 03:59:32PM +0200, Miguel Ojeda wrote:
> On Sat, Sep 20, 2025 at 8:23 PM Joel Fernandes <joelagnelf@...dia.com> 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.
>
> Being restrictive is a good thing -- it would be nice to know more
> context about this change, like Alexandre points out.
Sure, I replied to that thread. Lets discuss there as well about the usecase.
> In particular, the line:
>
> .set_nibble(0x12345678_u32) // truncated to 0x8
>
> sounds fairly alarming, and not what we usually want. Why cannot the
> caller cast on their side, if they really want that?
The setter function generated in this example accepts a u32.
Actually my test case here is not good, I will fix it. In the new v5 series I
am going to post, set_nibble(0x12345678) will actually fail because the value
passed exceeds 4 bit range.
So in reality, there will be no truncation at all, this is just a bad
test case (I developed the failure mode for when the value passed exceeds
the bit range, only by v5).
> We avoid `as` for similar reasons and nowadays enable some Clippy
> warnings to prevent its use where not needed.
Understood, I can add a comment here to explain why we do it. It is just to
make the code compile, in reality we're not really truncating anything. The
issue solved in this patch is the following line wont compile when narrowing
from 32 bit to 8 bit (even if the value passed does not exceed 8 bits):
let val = (<$storage>::from(value) << SHIFT) & MASK;
> (By the way, please follow our usual coding conventions for comments.)
Sorry about that, will do.
Thanks!
- Joel
Powered by blists - more mailing lists