-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add associated constant BITS
to all integer types
#76492
Conversation
This is useful, good idea. |
I tried to follow the convention that was already used in this repository: The name |
Bikeshed: |
Assigning to @dtolnay for T-libs hat "unstable API" review. |
Nice, this shows how much different are coding habits ;-) And why I don't like to think as name choice as bikeshedding. Constant/function names of libraries must be chosen carefully if you want those libraries to be ergonomic :) |
☔ The latest upstream changes (presumably #76678) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good. Please resolve the merge conflict in library/alloc/src/lib.rs and library/alloc/tests/string.rs and file a tracking issue, and r=me on the PR.
@bors delegate=m-ou-se |
✌️ @m-ou-se can now approve this pull request |
@bors r=dtolnay rollup |
📌 Commit 1bfe5ef has been approved by |
…r=dtolnay Add associated constant `BITS` to all integer types Recently I've regularly come across this snippet (in a few different crates, including `core` and `std`): ```rust std::mem::size_of<usize>() * 8 ``` I think it's time for a `usize::BITS`.
Rollup of 14 pull requests Successful merges: - rust-lang#73963 (deny(unsafe_op_in_unsafe_fn) in libstd/path.rs) - rust-lang#75099 (lint/ty: move fns to avoid abstraction violation) - rust-lang#75502 (Use implicit (not explicit) rules for promotability by default in `const fn`) - rust-lang#75580 (Add test for checking duplicated branch or-patterns) - rust-lang#76310 (Add `[T; N]: TryFrom<Vec<T>>` (insta-stable)) - rust-lang#76400 (Clean up vec benches bench_in_place style) - rust-lang#76434 (do not inline black_box when building for Miri) - rust-lang#76492 (Add associated constant `BITS` to all integer types) - rust-lang#76525 (Add as_str() to string::Drain.) - rust-lang#76636 (assert ScalarMaybeUninit size) - rust-lang#76749 (give *even better* suggestion when matching a const range) - rust-lang#76757 (don't convert types to the same type with try_into (clippy::useless_conversion)) - rust-lang#76796 (Give a better error message when x.py uses the wrong stage for CI) - rust-lang#76798 (Build fixes for RISC-V 32-bit Linux support) Failed merges: r? `@ghost`
size_of returns an usize, while BITS is a u32, so to replace one with the other I need to introduce a cast. Please consider making BITS a usize. |
Everything that counts bits of an integer type does so as a u32. E.g. leading_zeros, count_ones, the argument to rotate_left and wrapping_shr, etc. |
As another maintainer of a Like leonardo-m, I also would prefer a |
Wait a second, what happened? |
Ah, here's what happened:
Anyway, I also agree that they should be |
I don't agree All the existing functions that work with the number of bits in an integer (shifting, leading zeros, counting ones, etc.) work with I don't think the argument that it's mostly used as Some examples from popular crates:
|
An advantage of it being u8 is that you can convert it to both u32 and usize without "as" and without unwraps. |
`int_bits_const` feature [1] adds `i32::BITS`, etc. We already have `BitInteger` trait, which has `BitInteger::BITS`, and using it triggers `unstable_name_collision` compatibility lint [2] unless `int_bits_const` is explicitly enabled, gets stabilized or gets rejected and removed from the compiler. [1]: rust-lang/rust#76492 [2]: rust-lang/rust#48919
`int_bits_const` feature [1] adds `i32::BITS`, etc. We already have `BitInteger` trait, which has `BitInteger::BITS`, and using it triggers `unstable_name_collision` compatibility lint [2] unless `int_bits_const` is explicitly enabled, gets stabilized or gets rejected and removed from the compiler. [1]: rust-lang/rust#76492 [2]: rust-lang/rust#48919
`int_bits_const` feature [1] adds `i32::BITS`, etc. We already have `BitInteger` trait, which has `BitInteger::BITS`, and using it triggers `unstable_name_collision` compatibility lint [2] unless `int_bits_const` is explicitly enabled, gets stabilized or gets rejected and removed from the compiler. [1]: rust-lang/rust#76492 [2]: rust-lang/rust#48919
This ports rust-lang/rust@1e2dba1e, part of rust-lang/rust#76492. `usize::BITS` was stabilized in Rust 1.53.0.
Recently I've regularly come across this snippet (in a few different crates, including
core
andstd
):I think it's time for a
usize::BITS
.