Re: [PATCH v5 1/2] rust: num: add cv! macro to create values from constant expressions

From: Miguel Ojeda

Date: Tue Oct 13 2026 - 10:10:28 EST


On Tue, Oct 13, 2026 at 7:58 AM Eliot Courtney <ecourtney@xxxxxxxxxx> wrote:
>
> Currently, using NonZero/Bounded constants is quite verbose. It's
> unfortunate because it disincentivizes using it in interface boundaries.
> Introduce a macro to make it nicer to use. The macro `cv!` (for constant
> value) takes a const integer expression and widens it to i128 (at build
> time only) before passing it as a const generic value to a new trait
> `FromConst`. The value is then converted and appears in the
> associated constant `FromConst::VALUE`. The trait is implemented by
> NonZero, Bounded, and Alignment and lets values of each be constructed
> from constants without a verbose turbofish syntax.
>
> For example, `const { NonZero::new(1).unwrap() }` can be written as
> `cv!(1)`.
>
> Add the `cv!` macro to the prelude so it doesn't need to be imported
> explicitly to use it.
>
> Suggested-by: Gary Guo <gary@xxxxxxxxxxx>
> Reviewed-by: Gary Guo <gary@xxxxxxxxxxx>
> Acked-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
> Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> Acked-by: Danilo Krummrich <dakr@xxxxxxxxxx>
> Signed-off-by: Eliot Courtney <ecourtney@xxxxxxxxxx>

Applied to `rust-next` -- thanks everyone!

[ Added a few intra-doc links. - Miguel ]

And thanks Eliot for the quick respin :)

Cheers,
Miguel