Skip to content

feat: Add bit_width for unsigned integer types #142328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025

Conversation

sorairolake
Copy link
Contributor

@sorairolake sorairolake commented Jun 11, 2025

This PR adds methods to the primitive unsigned integer types that return the minimum number of bits required to represent an unsigned integer.

@rustbot
Copy link
Collaborator

rustbot commented Jun 11, 2025

r? @tgross35

rustbot has assigned @tgross35.
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

@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 Jun 11, 2025
Comment on lines 227 to 228
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".bit_width(), 0);")]
#[doc = concat!("assert_eq!(7", stringify!($SelfT), ".bit_width(), 3);")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could 7 instead be 0b111? to make the result obvious without mentally converting.

Also small nit, the other sections have a trailing underscore for literals (0b111_u8 rather than 0b111u8)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably also be good to have add an example like 0b1110 to illustrate that it's the width up to the most significant set bit (4 here) rather than the width of only set bits (3 here)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How should the first 0 on line 227 be expressed?

Copy link
Contributor

@tgross35 tgross35 Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving it as 0 or 0b0 is fine, up to you :) I just wanted the user to be able to count the bits themselves when we give a nonzero answer.

Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me with passing CI and a squash

@tgross35
Copy link
Contributor

@sorairolake could you do the squash please?

@sorairolake sorairolake force-pushed the feature/uint-bit-width branch from 7ed2d56 to 199b808 Compare June 11, 2025 02:16
@sorairolake
Copy link
Contributor Author

@tgross35 Done.

@tgross35
Copy link
Contributor

Thank you!

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Jun 11, 2025

📌 Commit 199b808 has been approved by tgross35

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 Jun 11, 2025
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Jun 11, 2025
…, r=tgross35

feat: Add `bit_width` for unsigned integer types

- Accepted ACP: rust-lang/libs-team#598
- Tracking issue: rust-lang#142326

This PR adds methods to the primitive unsigned integer types that return the minimum number of bits required to represent an unsigned integer.
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Jun 11, 2025
…, r=tgross35

feat: Add `bit_width` for unsigned integer types

- Accepted ACP: rust-lang/libs-team#598
- Tracking issue: rust-lang#142326

This PR adds methods to the primitive unsigned integer types that return the minimum number of bits required to represent an unsigned integer.
bors added a commit that referenced this pull request Jun 11, 2025
Rollup of 13 pull requests

Successful merges:

 - #134841 (Look at proc-macro attributes when encountering unknown attribute)
 - #141639 (Expose discriminant values in stable_mir)
 - #141967 (Configure bootstrap backport nominations through triagebot)
 - #142042 (Make E0621 missing lifetime suggestion verbose)
 - #142176 (tests: Split dont-shuffle-bswaps along opt-levels and arches)
 - #142248 (Add supported asm types for LoongArch32)
 - #142272 (tests: Change ABIs in tests to more future-resilient ones)
 - #142282 (Only run `citool` tests on the `auto` branch)
 - #142297 (Implement `//@ needs-target-std` compiletest directive)
 - #142298 (Make loongarch-none target maintainers more easily pingable)
 - #142306 (Dont unwrap and re-wrap typing envs)
 - #142324 (Remove unneeded `FunctionCx` from some codegen methods)
 - #142328 (feat: Add `bit_width` for unsigned integer types)

r? `@ghost`
`@rustbot` modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 11, 2025
…, r=tgross35

feat: Add `bit_width` for unsigned integer types

- Accepted ACP: rust-lang/libs-team#598
- Tracking issue: rust-lang#142326

This PR adds methods to the primitive unsigned integer types that return the minimum number of bits required to represent an unsigned integer.
bors added a commit that referenced this pull request Jun 11, 2025
Rollup of 9 pull requests

Successful merges:

 - #141967 (Configure bootstrap backport nominations through triagebot)
 - #142042 (Make E0621 missing lifetime suggestion verbose)
 - #142272 (tests: Change ABIs in tests to more future-resilient ones)
 - #142282 (Only run `citool` tests on the `auto` branch)
 - #142297 (Implement `//@ needs-target-std` compiletest directive)
 - #142298 (Make loongarch-none target maintainers more easily pingable)
 - #142306 (Dont unwrap and re-wrap typing envs)
 - #142324 (Remove unneeded `FunctionCx` from some codegen methods)
 - #142328 (feat: Add `bit_width` for unsigned integer types)

Failed merges:

 - #141639 (Expose discriminant values in stable_mir)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 583a6e2 into rust-lang:master Jun 11, 2025
10 checks passed
rust-timer added a commit that referenced this pull request Jun 11, 2025
Rollup merge of #142328 - sorairolake:feature/uint-bit-width, r=tgross35

feat: Add `bit_width` for unsigned integer types

- Accepted ACP: rust-lang/libs-team#598
- Tracking issue: #142326

This PR adds methods to the primitive unsigned integer types that return the minimum number of bits required to represent an unsigned integer.
@rustbot rustbot added this to the 1.89.0 milestone Jun 11, 2025
github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Jun 12, 2025
Rollup of 9 pull requests

Successful merges:

 - rust-lang/rust#141967 (Configure bootstrap backport nominations through triagebot)
 - rust-lang/rust#142042 (Make E0621 missing lifetime suggestion verbose)
 - rust-lang/rust#142272 (tests: Change ABIs in tests to more future-resilient ones)
 - rust-lang/rust#142282 (Only run `citool` tests on the `auto` branch)
 - rust-lang/rust#142297 (Implement `//@ needs-target-std` compiletest directive)
 - rust-lang/rust#142298 (Make loongarch-none target maintainers more easily pingable)
 - rust-lang/rust#142306 (Dont unwrap and re-wrap typing envs)
 - rust-lang/rust#142324 (Remove unneeded `FunctionCx` from some codegen methods)
 - rust-lang/rust#142328 (feat: Add `bit_width` for unsigned integer types)

Failed merges:

 - rust-lang/rust#141639 (Expose discriminant values in stable_mir)

r? `@ghost`
`@rustbot` modify labels: rollup
@sorairolake sorairolake deleted the feature/uint-bit-width branch June 12, 2025 07:05
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 Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants