[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <DE2UR8B3NFSN.27WJF1J897R4Z@nvidia.com>
Date: Sat, 08 Nov 2025 08:30:55 +0900
From: "Alexandre Courbot" <acourbot@...dia.com>
To: "Alexandre Courbot" <acourbot@...dia.com>, "Alice Ryhl"
<aliceryhl@...gle.com>, "David Airlie" <airlied@...il.com>, "Simona Vetter"
<simona@...ll.ch>, "Miguel Ojeda" <ojeda@...nel.org>, "Alex Gaynor"
<alex.gaynor@...il.com>, "Boqun Feng" <boqun.feng@...il.com>, "Gary Guo"
<gary@...yguo.net>, Björn Roy Baron
<bjorn3_gh@...tonmail.com>, "Benno Lossin" <lossin@...nel.org>, "Andreas
Hindborg" <a.hindborg@...nel.org>, "Trevor Gross" <tmgross@...ch.edu>
Cc: "John Hubbard" <jhubbard@...dia.com>, "Alistair Popple"
<apopple@...dia.com>, "Joel Fernandes" <joelagnelf@...dia.com>, "Timur
Tabi" <ttabi@...dia.com>, "Edwin Peer" <epeer@...dia.com>,
<nouveau@...ts.freedesktop.org>, <dri-devel@...ts.freedesktop.org>,
<linux-kernel@...r.kernel.org>, <rust-for-linux@...r.kernel.org>, "Danilo
Krummrich" <dakr@...nel.org>, "Nouveau"
<nouveau-bounces@...ts.freedesktop.org>
Subject: Re: [PATCH v3 0/6] gpu: nova-core: remove use of `as` for integer
conversions
On Wed Oct 29, 2025 at 8:12 AM JST, Alexandre Courbot wrote:
> Using the `as` operator for integer conversions is discouraged, as it
> silently strips data if the destination type is smaller than the source.
> Many such conversions can be replaced with `from`/`into` or (when
> justified) `try_from`/`try_into`, but these traits cannot unfortunately
> cover all conversions satisfyingly.
>
> There is for instance the case of converting a `usize` to `u64`, which,
> in the case of the kernel today, is completely lossless but cannot be
> done because the Rust standard library does not provide a `From`
> implementation for conversions that are not future-proof.
>
> Still, in the kernel it is very practical to be able to perform such
> conversions when they are safe to do for the current build target.
>
> This patchset tries to eradicate the use of `as` in nova-core, by using
> existing means and introducing new ones.
>
> The first 4 patches use the already-available `From` and `TryFrom` trait
> where it is possible or advisable.
>
> The fifth patch introduces a new module that proposes conversion
> functions for those that are infallible under the current build target.
> This is done through a set of const functions, and the `FromSafeCast`
> and `IntoSafeCast` extension traits which, as their names lightly
> suggest, offer conversion for those types on which the `as` operator can
> be used losslessly.
>
> This new module is put to use in the sixth patch.
>
> The idea was first suggested by Danilo.
>
> As Danilo suggested, this could eventually find its place in the kernel
> crate if the implementation is deemed to be fit, but for now let's
> review and let it mature in nova-core.
All patches pushed to drm-rust-next, thanks!
Powered by blists - more mailing lists