Re: [PATCH v5] rust: kernel: add support for bits/genmask macros

From: Miguel Ojeda
Date: Mon May 19 2025 - 14:00:06 EST


On Tue, May 13, 2025 at 8:52 PM Daniel Almeida
<daniel.almeida@xxxxxxxxxxxxx> wrote:
>
> I have been staring at this for a little while.
>
> I wonder what is everyone's opinions on an extra set of:
>
> // e.g.: for u32
> const fn const_genmask_u32<const H: u32, const L: u32>() -> u32 {
> crate::build_assert!(H >= L);
> ...
> }
>
> ..on top of the current genmask functions we already have?

It seems you want `consteval` from C++ :)

Without having thought about the particular use case, just a quick
note: if you have const generics, then you can use `const { assert!(H
>= L); }` instead.

Nowadays `.unwrap()` on `Option` is `const`, but not `Result`'s. That
would be a way to have a single fallible function that allows users to
decide to unwrap in a const context or use them fallibly for runtime
values. Another is having a custom `const` unwrap for those concrete
`Result`s.

Cheers,
Miguel