-
-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Implement bit and set_bit for integral types.
#147696
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
base: main
Are you sure you want to change the base?
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 |
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. |
| #[doc = concat!("let n = 0b1000001", stringify!($SelfT), ";")] | ||
| /// | ||
| /// assert!(n.bit(0)); | ||
| /// assert!(!n.bit(1)); |
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.
Choosing a binary palindrome for the example misses the opportunity to orient the reader about the meaning of index.
| #[doc = concat!("let n = 0b1000001", stringify!($SelfT), ";")] | |
| /// | |
| /// assert!(n.bit(0)); | |
| /// assert!(!n.bit(1)); | |
| #[doc = concat!("let n = 0b1000011", stringify!($SelfT), ";")] | |
| /// | |
| /// assert!(n.bit(0)); | |
| /// assert!(n.bit(1)); |
| #[inline] | ||
| #[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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to rust-lang/libs-team#667 (comment) the API that was accepted involves checking index in builds that contain overflow checking (debug mode or -C overflow-checks). I tested this and it did not panic:
#![feature(get_set_bit)]
fn main() {
println!("{}", 0i32.bit(40));
}whereas this does panic:
#![allow(arithmetic_overflow)]
fn main() {
let index = 40;
println!("{}", 0i32 & (1 as i32) << index != (0 as i32));
}I think this attribute does the thing:
rust/library/core/src/num/int_macros.rs
Line 3289 in 5f9dd05
| #[rustc_inherit_overflow_checks] |
Tracking issue: #147702
This PR implements the
bitandset_bitmethods for integral types: