From: "Eliot Courtney" <ecourtney@nvidia.com>
To: "Alexandre Courbot" <acourbot@nvidia.com>,
"Eliot Courtney" <ecourtney@nvidia.com>
Cc: "Alice Ryhl" <aliceryhl@google.com>,
"Gary Guo" <gary@garyguo.net>,
"Burak Emir" <burak.emir@gmail.com>,
"Yury Norov" <yury.norov@gmail.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Onur Özkan" <work@onurozkan.dev>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"John Hubbard" <jhubbard@nvidia.com>,
"Alistair Popple" <apopple@nvidia.com>,
"Timur Tabi" <ttabi@nvidia.com>, "Zhi Wang" <zhiw@nvidia.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org,
dri-devel <dri-devel-bounces@lists.freedesktop.org>
Subject: Re: [PATCH v8 03/12] rust: num: add cv! macro to create values from constant expressions
Date: Fri, 28 Aug 2026 13:53:59 +0900 [thread overview]
Message-ID: <DL0B47OUUMD5.3A6OG1Y5Z2MZY@nvidia.com> (raw)
In-Reply-To: <DL09JQ5CRJLA.4Q6OTGU4CMWN@nvidia.com>
On Fri Aug 28, 2026 at 12:40 PM JST, Alexandre Courbot wrote:
[...]
>> Using an associated const by itself appears to work - I tried this which
>> is very similar to Alice's suggested approach above:
>>
>> ```
>> macro_rules! const_assert {
>> ($condition:expr $(,$arg:literal)?) => {
>> const { ::core::assert!($condition $(,$arg)?) };
>> };
>> }
>
> Any reason this cannot use the `const_assert` already in the kernel
> crate?
No reason. I prototyped this in a single file so I had some misc shims.
This is just me copy pasting badly.
>
>>
>> trait FromConst<const V: i128>: Sized {
>> const VALUE: Self;
>> }
>>
>> macro_rules! cv {
>> (@widen $v:expr) => {{
>> #[allow(unused_comparisons, unused_assignments)]
>> {
>> let v = $v;
>> let r = v as i128;
>> let mut back = v;
>> back = r as _;
>>
>> ::core::assert!(
>> back == v && (v < 0) == (r < 0),
>> "value cannot be losslessly widened to `i128`"
>> );
>>
>> r
>> }
>> }};
>> ($v:expr => $t:ty) => {
>> <$t as FromConst<{ cv!(@widen $v) }>>::VALUE
>
> nit for the actual posting: make sure to fully qualify `cv` when calling
> it recursively (and make sure all symbols are fully qualified).
>
>> };
>> ($v:expr) => {
>> <_ as FromConst<{ cv!(@widen $v) }>>::VALUE
>> };
>> }
>>
>> macro_rules! impl_from_const_int {
>> ($($t:ty)*) => {$(
>> impl<const V: i128> FromConst<V> for $t {
>> const VALUE: Self = {
>> const_assert!(
>> V >= <$t>::MIN as i128 && V <= <$t>::MAX as i128,
>> "Constant cannot be represented by the target type."
>> );
>> V as $t
>> };
>> }
>>
>> impl<const V: i128> FromConst<V> for NonZero<$t> {
>> const VALUE: Self = {
>> const_assert!(
>> V >= <$t>::MIN as i128 && V <= <$t>::MAX as i128,
>> "Constant cannot be represented by the underlying type."
>> );
>
> Let's also have a `const_assert!(V != 0, ...)` to provide a better
> error message than "unwrap on None" if users call this with 0.
Sounds good~
>
>> NonZero::new(V as $t).unwrap()
>> };
>> }
>> )*};
>> }
>> impl_from_const_int!(u8 u16 u32 u64 usize i8 i16 i32 i64 isize);
>>
>> impl<const V: i128> FromConst<V> for Alignment {
>> const VALUE: Self = {
>> const_assert!(V > 0 && V <= usize::MAX as i128);
>> // The unwrap fails the build if `V` is not a power of two.
>> Alignment::new_checked(V as usize).unwrap()
>> };
>> }
>>
>> const A: u8 = cv!(200u32);
>> const B: NonZero<u8> = cv!(5);
>> const C: Alignment = cv!(4096);
>> const D: u8 = cv!(200u32 => u8);
>> ```
>
> That looks like it could work! IIUC it even supports something like
> `cv!(x => NonZero<u8>)`. The only limitation is see is that this cannot
> take expressions using generic parameters, but we can probably work
> around that.
Yeah agreed. One workaround is just to list each primitive type in the
macro, although it doesn't work if you define a type alias or something.
I think that's reasonable and not too complex. FWIW I found one
potential user that would want to be able to reference generics in cv!.
In drm::Device:
`num_ioctls: T::IOCTLS.len() as i32,`
wants to be `cv!(T::IOCTLS.len() => i32),` which needs this.
>
> I guess you'll want to split this out into its own series so it doesn't
> remain hidden within the ranges/bitmap work. Basically as a replacement
> for the `const_as` I was driving [1]. I'll recycle the `const_as` series
> to just switch to the kernel converters, and will follow-up with using
> `cv!` once it lands.
>
> Since this is going to be a multi-cycle effort we should probably keep
> the legacy `*_into_*` functions around for now and remove them in
> another patch once all users are converted.
>
> [1] https://lore.kernel.org/20260825-const_as-v1-0-1ce712225fe2@nvidia.com
Yes I'll split it out. Thanks!
next prev parent reply other threads:[~2026-08-28 4:54 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-27 7:28 [PATCH v8 00/12] rust: Add support for reserving of ranges of IDs Eliot Courtney
2026-08-27 7:28 ` [PATCH v8 01/12] rust: bitmap: use function-level cfg on kunit test Eliot Courtney
2026-08-27 7:28 ` [PATCH v8 02/12] rust: bitmap: restrict bitmap length to at most i32::MAX Eliot Courtney
2026-08-27 7:40 ` sashiko-bot
2026-08-27 7:28 ` [PATCH v8 03/12] rust: num: add cv! macro to create values from constant expressions Eliot Courtney
2026-08-27 9:32 ` Alice Ryhl
2026-08-27 10:42 ` Alexandre Courbot
2026-08-27 11:12 ` Alexandre Courbot
2026-08-27 13:48 ` Eliot Courtney
2026-08-27 13:59 ` Gary Guo
2026-08-27 14:29 ` Alexandre Courbot
2026-08-27 14:37 ` Gary Guo
2026-08-27 14:55 ` Alice Ryhl
2026-08-28 0:08 ` Eliot Courtney
2026-08-28 3:40 ` Alexandre Courbot
2026-08-28 4:53 ` Eliot Courtney [this message]
2026-08-28 14:01 ` Gary Guo
2026-08-27 7:28 ` [PATCH v8 04/12] rust: prelude: add `num::cv` Eliot Courtney
2026-08-27 7:28 ` [PATCH v8 05/12] rust: use cv! to build Bounded values from constants Eliot Courtney
2026-08-27 7:28 ` [PATCH v8 06/12] rust: sizes: add sub-1K size constants Eliot Courtney
2026-08-28 5:11 ` Alexandre Courbot
2026-08-27 7:28 ` [PATCH v8 07/12] rust: sizes: implement SizeConstants for Alignment Eliot Courtney
2026-08-28 5:13 ` Alexandre Courbot
2026-08-28 5:23 ` Eliot Courtney
2026-08-28 5:36 ` Alexandre Courbot
2026-08-28 6:27 ` Miguel Ojeda
2026-08-27 7:28 ` [PATCH v8 08/12] rust: use Alignment size constants Eliot Courtney
2026-08-27 7:28 ` [PATCH v8 09/12] rust: bitmap: add contiguous area operations Eliot Courtney
2026-08-27 7:43 ` sashiko-bot
2026-08-27 7:28 ` [PATCH v8 10/12] rust: id_pool: add contiguous ID reservation Eliot Courtney
2026-08-27 7:28 ` [PATCH v8 11/12] rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN Eliot Courtney
2026-08-27 7:41 ` sashiko-bot
2026-08-27 7:28 ` [PATCH v8 12/12] gpu: nova-core: add ChannelIdPool Eliot Courtney
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DL0B47OUUMD5.3A6OG1Y5Z2MZY@nvidia.com \
--to=ecourtney@nvidia.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=apopple@nvidia.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=burak.emir@gmail.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=dri-devel-bounces@lists.freedesktop.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=jhubbard@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=nova-gpu@lists.linux.dev \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=ttabi@nvidia.com \
--cc=work@onurozkan.dev \
--cc=yury.norov@gmail.com \
--cc=zhiw@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox