Skip to content

Conversation

@sorairolake
Copy link
Contributor

@sorairolake sorairolake commented Nov 10, 2025

This pull request adds a method to the unsigned NonZero<T> that return the minimum number of bits required to represent a value.

This can be achieved by using the get method and the methods added in #142328, but I think adding the NonZero::bit_width method is useful because it accomplishes the same thing a little more succinctly.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 10, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 10, 2025

r? @scottmcm

rustbot has assigned @scottmcm.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@sorairolake
Copy link
Contributor Author

@rustbot label +T-libs-api -T-libs

r? libs-api

@rustbot rustbot added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 14, 2025
@rustbot rustbot assigned the8472 and unassigned scottmcm Nov 14, 2025
@hanna-kruppe
Copy link
Contributor

Can’t this return NonZeroU32? That would be a good argument for a dedicated NonZero method instead of delegating to get().

@scottmcm
Copy link
Member

but I think adding the NonZero::bit_width method is useful because it accomplishes the same thing a little more succinctly.

Note that we generally don't add things to NonZero just to avoid a .get(). You'll note, for example, that NonZero::count_ones exists -- because it can return NonZero<u32> -- but NonZero::count_zeroes doesn't, despite it existing on the underlying types, since there's nothing special for NonZero to do on it.

If, as @hanna-kruppe mentions, this can return NonZero, that might be a reason to justify its existence.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 17, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 17, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Return `NonZero<u32>` instead of `u32`.
@sorairolake
Copy link
Contributor Author

@scottmcm In safe Rust, the minimum number of bits required to represent unsigned NonZero<T> is guaranteed to be greater than or equal to one. Therefore, this method can return NonZero<u32>.

Based on the comments, I changed the return type of this method from u32 to NonZero<u32>. This type is suitable to indicate that this method can never return zero. Also, other NonZero<T> methods may be easier to use with this type than with u32.

@sorairolake
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 18, 2025
@scottmcm
Copy link
Member

Sure; for unstable seems close enough to fit under the existing unstable library feature.

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Nov 19, 2025

📌 Commit a25950d has been approved by scottmcm

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 19, 2025
bors added a commit that referenced this pull request Nov 19, 2025
Rollup of 7 pull requests

Successful merges:

 - #147171 (recommend using a HashMap if a HashSet's second generic parameter doesn't implement BuildHasher)
 - #147421 (Add check if span is from macro expansion)
 - #147521 (Make SIMD intrinsics available in `const`-contexts)
 - #148201 (Start documenting autodiff activities)
 - #148797 (feat: Add `bit_width` for unsigned `NonZero<T>`)
 - #148798 (Match <OsString as Debug>::fmt to that of str)
 - #149082 (autodiff: update formating, improve examples for the unstable-book)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 8b74790 into rust-lang:main Nov 19, 2025
11 checks passed
@rustbot rustbot added this to the 1.93.0 milestone Nov 19, 2025
rust-timer added a commit that referenced this pull request Nov 19, 2025
Rollup merge of #148797 - sorairolake:feature/non-zero-uint-bit-width, r=scottmcm

feat: Add `bit_width` for unsigned `NonZero<T>`

- Tracking issue: #142326

This pull request adds a method to the unsigned `NonZero<T>` that return the minimum number of bits required to represent a value.

This can be achieved by using the `get` method and the methods added in #142328, but I think adding the `NonZero::bit_width` method is useful because it accomplishes the same thing a little more succinctly.
@sorairolake sorairolake deleted the feature/non-zero-uint-bit-width branch November 19, 2025 13:53
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Nov 20, 2025
Rollup of 7 pull requests

Successful merges:

 - rust-lang/rust#147171 (recommend using a HashMap if a HashSet's second generic parameter doesn't implement BuildHasher)
 - rust-lang/rust#147421 (Add check if span is from macro expansion)
 - rust-lang/rust#147521 (Make SIMD intrinsics available in `const`-contexts)
 - rust-lang/rust#148201 (Start documenting autodiff activities)
 - rust-lang/rust#148797 (feat: Add `bit_width` for unsigned `NonZero<T>`)
 - rust-lang/rust#148798 (Match <OsString as Debug>::fmt to that of str)
 - rust-lang/rust#149082 (autodiff: update formating, improve examples for the unstable-book)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants