Implement bit and set_bit for integral types. - #147696
bjoernager wants to merge 1 commit into
Conversation
|
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 |
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 |
This comment was marked as outdated.
This comment was marked as outdated.
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
|
Reminder, once the PR becomes ready for a review, use |
|
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.
|
The |
|
r? libs |
| without modifying the original"] | ||
| #[track_caller] | ||
| pub const fn bit(self, index: u32) -> bool { | ||
| self & (1 as $SelfT) << index != (0 as $SelfT) |
There was a problem hiding this comment.
I would expect this to just call self.cast_unsigned().bit(index) instead of rewriting the logic, to avoid duplicating the code.
| self &= !((1 as $SelfT) << index); | ||
| self |= (value as $SelfT) << index; | ||
|
|
||
| self |
There was a problem hiding this comment.
Similarly would expect self.cast_unsigned().set_bit(index, value)
| without modifying the original"] | ||
| #[track_caller] | ||
| pub const fn bit(self, index: u32) -> bool { | ||
| self & (1 as $SelfT) << index != (0 as $SelfT) |
There was a problem hiding this comment.
I get that operator precedence is on your side but parentheses help make this clearer:
(self & ((1 as $SelfT) << index)) != (0 as $SelfT)
I'm also not entirely sure the as $SelfT is really necessary here:
| self & (1 as $SelfT) << index != (0 as $SelfT) | |
| (self & (1 << index)) != 0 |
| self &= !((1 as $SelfT) << index); | ||
| self |= (value as $SelfT) << index; | ||
|
|
||
| self |
There was a problem hiding this comment.
Similarly, don't think you need the 1 as $SelfT:
| self &= !((1 as $SelfT) << index); | |
| self |= (value as $SelfT) << index; | |
| self | |
| self &= !(1 << index); | |
| self |= (value as $SelfT) << index; | |
| self |
|
@rustbot author |
View all comments
Tracking issue: #147702
This PR implements the
bitandset_bitmethods for integral types: