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

Daniel Almeida
2025-05-13T18:52:59Z

Hi Miguel,

> On 27 Mar 2025, at 18:27, Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote:
>
> Hi Daniel,
>
> My usual docs-only review... I hope that helps!
>
> On Wed, Mar 26, 2025 at 3:07 PM Daniel Almeida
> <daniel.almeida@collabora.com> wrote:
>>
>> +/// Equivalent to the kernel's `BIT` macro.
>
> "To the C `BIT` macro" or "The C side ..." or similar -- these one
> would be also the kernel's :)
>
>> +/// Create a contiguous bitmask starting at bit position `l` and ending at
>> +/// position `h`, where `h >= l`.
>
> The first paragraph is a "short description" / title -- you may want
> to leave the details to a second sentence, i.e. in a second paragraph.
> Please check in any case how it looks in the rendered docs -- it may
> be fine to have all in the title.
>
> In fact, given you `assert!`, we should probably mention that very
> prominently e.g. in a `# Panics` section. Or, better, avoid the panics
> to begin with if it makes sense.

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?

This lets us move the checks to compile time for most cases, because for the
majority of users, h and l are simply integer literals.

For the rest, we can probably modify the current functions:

fn genmask_u32(h: u32, l: u32) -> Result<u32> {
if(h < l) {
return Err(EINVAL);
}
..
}

The implementation can probably be shared by using macros like kernel::io::Io,
for example, and the panics would be gone.

— Daniel