Alexandre Courbot
2025-Jun-20 13:59 UTC
[PATCH 1/3] rust: add `num` module with `PowerOfTwo` type
On Fri Jun 20, 2025 at 10:35 PM JST, Miguel Ojeda wrote:> On Fri, Jun 20, 2025 at 3:15?PM Alexandre Courbot <acourbot at nvidia.com> wrote: >> >> Introduce the `num` module, featuring the `PowerOfTwo` unsigned wrapper >> that guarantees (at build-time or runtime) that a value is a power of >> two. >> >> Such a property is often useful to maintain. In the context of the >> kernel, powers of two are often used to align addresses or sizes up and >> down, or to create masks. These operations are provided by this type. > > Before I forget: the other day in a call we discussed powers of two > and I mentioned that there is `Alignment` in the standard library: > > https://doc.rust-lang.org/std/ptr/struct.Alignment.html > > "A type storing a `usize` which is a power of two"Haha, I wasn't aware of this effort, and am quite amazed by how close its API is to my own design. This is reassuring; maybe I am finally starting to grok Rust after all. ;)> > So it would be nice to ask upstream the following if they have plans > to stabilize it, and whether they have considered a generic > `PowerOfTwo<T>` type like this one, rather than one just for alignment > purposes (possibly with an alias or newtype for `Alignment` if > needed).Mmm indeed I don't quite see the fundamental difference between `Alignment` and `PowerOfTwo`, although `Alignment` might better capture what we are doing with our type anyway.> > Similarly, if they stabilize the `Alignment` one (only) and we end up > only using our `PowerOfTwo<T>` for `usize` and those use cases, then > we should consider using the upstream one (and adding any/all methods > that we need).`Alignment` is very close to what we need, so I don't see a reason to not adopt the same name at the very least. This reminds me that I should also check whether upstream Rust would be interested in `prev_multiple_of` and `last_set_bit`. The docs I've read for contributing looked a bit intimidating, with RFCs to write and all. Would you have a pointer for where I should start? Maybe a Zulip thread?> > So I will ask them the next time we meet. I have added > `ptr_alignment_type` to our list (in the "nice to have" section). > > (Apologies if this was already discussed!)I wasn't aware of this, so thanks for bringing it up!
Alice Ryhl
2025-Jun-20 14:02 UTC
[PATCH 1/3] rust: add `num` module with `PowerOfTwo` type
On Fri, Jun 20, 2025 at 3:59?PM Alexandre Courbot <acourbot at nvidia.com> wrote:> > Similarly, if they stabilize the `Alignment` one (only) and we end up > > only using our `PowerOfTwo<T>` for `usize` and those use cases, then > > we should consider using the upstream one (and adding any/all methods > > that we need). > > `Alignment` is very close to what we need, so I don't see a reason to > not adopt the same name at the very least. > > This reminds me that I should also check whether upstream Rust would be > interested in `prev_multiple_of` and `last_set_bit`. The docs I've read > for contributing looked a bit intimidating, with RFCs to write and all. > Would you have a pointer for where I should start? Maybe a Zulip thread?If you want to add a new library function, the correct procedure would be opening an ACP, which is more light-weight than the RFC process: https://std-dev-guide.rust-lang.org/development/feature-lifecycle.html RFCs are mainly for much bigger changes. Alice
Alexandre Courbot
2025-Aug-02 14:02 UTC
[PATCH 1/3] rust: add `num` module with `PowerOfTwo` type
On Fri Jun 20, 2025 at 11:02 PM JST, Alice Ryhl wrote:> On Fri, Jun 20, 2025 at 3:59?PM Alexandre Courbot <acourbot at nvidia.com> wrote: >> > Similarly, if they stabilize the `Alignment` one (only) and we end up >> > only using our `PowerOfTwo<T>` for `usize` and those use cases, then >> > we should consider using the upstream one (and adding any/all methods >> > that we need). >> >> `Alignment` is very close to what we need, so I don't see a reason to >> not adopt the same name at the very least. >> >> This reminds me that I should also check whether upstream Rust would be >> interested in `prev_multiple_of` and `last_set_bit`. The docs I've read >> for contributing looked a bit intimidating, with RFCs to write and all. >> Would you have a pointer for where I should start? Maybe a Zulip thread? > > If you want to add a new library function, the correct procedure would > be opening an ACP, which is more light-weight than the RFC process: > https://std-dev-guide.rust-lang.org/development/feature-lifecycle.html > > RFCs are mainly for much bigger changes.Belated thanks for the suggestion; I have finally opened an ACP for `last_set_bit` (and `first_set_bit` while we are at it): https://github.com/rust-lang/libs-team/issues/631 I am still entangled with how to best leverage `Alignment` for our purposes, but think I am getting close to a v2 of this patchset.