Alexandre Courbot
2025-May-22 04:00 UTC
[PATCH v4 04/20] rust: add new `num` module with useful integer operations
On Wed May 21, 2025 at 3:44 PM JST, Alexandre Courbot wrote:> Introduce the `num` module, featuring the `NumExt` extension trait > that expands unsigned integers with useful operations for the kernel. > > These are to be used by the nova-core driver, but they are so ubiquitous > that other drivers should be able to take advantage of them as well. > > The currently implemented operations are: > > - align_down() > - align_up() > - fls() > > But this trait is expected to be expanded further.A trait is nice, but prevents any use in const context... After looking at the genmask patch [1] I am now wondering (again) whether a set of const functions would not better serve the needs of the kernel. Either that, or we enable `#![feature(const_trait_impl)]`. I just tried and with it we could indeed define and implement `NumExt` as const, which looks like the cleanest way to do this to me. The functions of [1] could then also be implemented as methods of that trait, which would allow them to leverage the macro generating the impl blocks for all supporting types, while having their examples/doc-tests in the trait declaration. [1] https://lore.kernel.org/rust-for-linux/20250326-topic-panthor-rs-genmask-v5-1-bfa6140214da at collabora.com/
Miguel Ojeda
2025-May-22 08:45 UTC
[PATCH v4 04/20] rust: add new `num` module with useful integer operations
On Thu, May 22, 2025 at 6:01?AM Alexandre Courbot <acourbot at nvidia.com> wrote:> > Either that, or we enable `#![feature(const_trait_impl)]`. I just tried > and with it we could indeed define and implement `NumExt` as const, > which looks like the cleanest way to do this to me.Hmm... I think that one is actively being worked on, with a possible syntax change in the works. We would need to speak to upstream Rust to see when we could reasonably stat to use it, and consider the older compilers (e.g. if the syntax changes). Cheers, Miguel
Alexandre Courbot
2025-May-22 09:31 UTC
[PATCH v4 04/20] rust: add new `num` module with useful integer operations
On Thu May 22, 2025 at 5:44 PM JST, Miguel Ojeda wrote:> On Thu, May 22, 2025 at 6:01?AM Alexandre Courbot <acourbot at nvidia.com> wrote: >> >> Either that, or we enable `#![feature(const_trait_impl)]`. I just tried >> and with it we could indeed define and implement `NumExt` as const, >> which looks like the cleanest way to do this to me. > > Hmm... I think that one is actively being worked on, with a possible > syntax change in the works. We would need to speak to upstream Rust to > see when we could reasonably stat to use it, and consider the older > compilers (e.g. if the syntax changes).Yeah that could be a problem. Which is a bit sad as with this approach we only need one method name instead of having multiple const functions suffixed with `_u8`, `_u16`, etc. for each type we want to extend.