From: "Gary Guo" <gary@garyguo.net>
To: "Gary Guo" <gary@garyguo.net>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Eliot Courtney" <ecourtney@nvidia.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>,
"Alice Ryhl" <aliceryhl@google.com>,
"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>
Cc: <linux-kernel@vger.kernel.org>, <rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH 2/2] rust: num: add `cv!` macro to create values from constant expressions
Date: Fri, 28 Aug 2026 15:18:39 +0100 [thread overview]
Message-ID: <DL0N4JZSMKJ4.3SCNEZ9NL32T0@garyguo.net> (raw)
In-Reply-To: <20260828-cv-v1-2-694a695ff17f@garyguo.net>
On Fri Aug 28, 2026 at 1:03 PM BST, Gary Guo wrote:
> Currently, constructing a `NonZero` or `Bounded` from a constant is
> verbose. The former would require `const { NonZero::new(...).unwrap() }`
> and the latter require turbofish. Similarly, the `num::casts` exposes
> methods that cast numbers using turbofish syntax, which is unergonomic and
> unnecessarily causes the value to flow into the type system, which is very
> restrictive without `generic_const_exprs`.
>
> Implement a macro `cv!` (short for constant value) which converts a const
> integer to types that implements `FromConst` trait and validate them during
> const evaluation.
>
> The usage is of form
>
> cv!(<expression>)
>
> for inferred type and
>
> cv!(<expression> => <type>)
>
> for explicit type specification.
>
> As we do not have const trait implementation yet, dark magic is used. The
> dark magic is documented in the code, but in essence it defines inherent
> `__from_const` impls on types, which can be marked const, and rely on
> Rust's method resolution algorithm to pick the correct function. Multiple
> helpers are defined to aid type inference to work properly.
Here's a generalized version that work for any trait methods (same limitation,
that you cannot use this trick if you have only `T: Trait` instead of a specific
`T`):
// Inference helper.
const fn would_call<T, U, F: FnOnce(T) -> U>(_: T, _: F) -> U {
todo!()
}
// We can have a single helper type for *all* fake const traits, as long as
// method names don't duplicate.
struct Const<T>(T);
impl<T> core::ops::Deref for Const<T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
&self.0
}
}
// Const call!
macro_rules! cc {
(($e:expr).$ident:ident($($args:tt)*)) => {
if false {
// The normal "runtime" call path for type inference.
would_call($e, |x| x.$ident($($args)*));
} else {
// Dispatch via inherent methods. Type already known with the above
// helper!
// A drawback here is that `$e` -> `&$e` autoref won't work here, as
// `Deref` impl is not const.
Const($e).$ident($($args)*)
}
}
}
// Imagine this being an attribute macro.
macro_rules! const_trait {
(impl const $tr:ident for $ty:ty {
fn $method:ident(self: $self_ty:ty) $body:block
}) => {
impl $tr for $ty {
fn $method(self: $self_ty) {
Const(self).$method()
}
}
impl $ty {
pub const fn $method(self: Const<$self_ty>) $body
}
}
}
struct MyStruct;
trait MyTrait {
fn foo(self: Self);
}
const_trait!{
impl const MyTrait for MyStruct {
fn foo(self: Self) {}
}
}
fn test() {
const {
let x = MyStruct;
// MAGIC!
cc!((x).foo());
}
}
next prev parent reply other threads:[~2026-08-28 14:18 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-28 12:03 [PATCH 0/2] rust: num: add cv! macro to create values from constant expressions (alt) Gary Guo
2026-08-28 12:03 ` [PATCH 1/2] rust: build_assert: add utility to require const eval Gary Guo
2026-08-28 12:03 ` [PATCH 2/2] rust: num: add `cv!` macro to create values from constant expressions Gary Guo
2026-08-28 14:18 ` Gary Guo [this message]
2026-08-29 4:05 ` Alexandre Courbot
2026-08-31 7:20 ` Eliot Courtney
2026-08-31 13:09 ` Gary Guo
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=DL0N4JZSMKJ4.3SCNEZ9NL32T0@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=ecourtney@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
--cc=yury.norov@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.