Implement bit and set_bit for integral types.#147696
Implement bit and set_bit for integral types.#147696bjoernager wants to merge 1 commit intorust-lang:mainfrom
bit and set_bit for integral types.#147696Conversation
|
rustbot has assigned @Mark-Simulacrum. Use |
|
@rustbot label +T-libs-api -T-compiler -T-libs r? libs-api |
|
Not sure if this detail was discussed on the ACP, but the name |
bit and set_bit for integral types;bit and set_bit for integral types.
Or perhaps just |
|
I glossed over the |
My latter comment should've had the |
This comment has been minimized.
This comment has been minimized.
00e99a8 to
de2e411
Compare
This comment has been minimized.
This comment has been minimized.
|
Thank you @bjoernager for implementing (and probably championing) this 💜. Fwiw I also think |
In the meeting we were specifically considering the version that takes a |
|
You might want to implement more test coverage in |
Agree on both points, but what about Edit: the main reason I’m interested in this naming point is to leave room for the |
|
I'm not a fan of |
I like to view it as In both cases, an original value (integer vs. pointer) has one of its subvalues (bit vs. address) overwritten completely. |
|
@bjoernager ping |
This comment was marked as resolved.
This comment was marked as resolved.
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@rustbot ready |
This comment has been minimized.
This comment has been minimized.
|
Suggestions seemed very rational to me and have all been implemented. Let me know if there are any other issues with the implementation, @dtolnay. :D |
|
@dtolnay friendly ping :))) |
There was a problem hiding this comment.
You might want to implement more test coverage in
library/coretests/tests/num/int_macros.rsand/library/coretests/tests/num/uint_macros.rs. Don't forget to add the feature tolibrary/coretests/tests/lib.rs.
These are also good suggestions that haven't been implemented yet (or not pushed?). The doc tests cover the logic but don't test e.g. const evaluation
| /// If overflow checks are enabled, this method will panic if the provided index is | ||
| /// greater than [`BITS`]. |
There was a problem hiding this comment.
| /// If overflow checks are enabled, this method will panic if the provided index is | |
| /// greater than [`BITS`]. | |
| /// If overflow checks are enabled, this method will panic if the provided index is | |
| /// greater than or equal to [`BITS`]. |
because 1u64 << u64::BITS will panic.
| /// ``` | ||
| /// #![feature(get_set_bit)] | ||
| /// | ||
| #[doc = concat!("let n = 0b1111001", stringify!($SelfT), ";")] |
There was a problem hiding this comment.
| #[doc = concat!("let n = 0b1111001", stringify!($SelfT), ";")] | |
| #[doc = concat!("let n = 0b0111_1001", stringify!($SelfT), ";")] |
might be slightly easier to read?
| /// If overflow checks are enabled, this method will panic if the provided index is | ||
| /// greater than [`BITS`]. |
There was a problem hiding this comment.
| /// If overflow checks are enabled, this method will panic if the provided index is | |
| /// greater than [`BITS`]. | |
| /// If overflow checks are enabled, this method will panic if the provided index is | |
| /// greater than or equal to [`BITS`]. |
| /// ``` | ||
| /// #![feature(get_set_bit)] | ||
| /// | ||
| #[doc = concat!("let mut n = 0b1010101", stringify!($SelfT), ";")] |
There was a problem hiding this comment.
| #[doc = concat!("let mut n = 0b1010101", stringify!($SelfT), ";")] | |
| #[doc = concat!("let mut n = 0b0101_0101", stringify!($SelfT), ";")] |
| /// n = n.set_bit(5, true); | ||
| /// n = n.set_bit(6, false); | ||
| /// | ||
| /// assert_eq!(n, 0b0101010); |
There was a problem hiding this comment.
| /// assert_eq!(n, 0b0101010); | |
| /// assert_eq!(n, 0b0010_1010); |
| /// If overflow checks are enabled, this method will panic if the provided index is | ||
| /// greater than [`BITS`]. |
There was a problem hiding this comment.
same as for signed, also below.
|
Reminder, once the PR becomes ready for a review, use |
View all comments
Tracking issue: #147702
This PR implements the
bitandset_bitmethods for integral types: