close
Skip to content

Implement bit and set_bit for integral types.#147696

Open
bjoernager wants to merge 1 commit into
rust-lang:mainfrom
bjoernager:bit
Open

Implement bit and set_bit for integral types.#147696
bjoernager wants to merge 1 commit into
rust-lang:mainfrom
bjoernager:bit

Conversation

@bjoernager
Copy link
Copy Markdown
Contributor

@bjoernager bjoernager commented Oct 14, 2025

View all comments

Tracking issue: #147702

This PR implements the bit and set_bit methods for integral types:

impl u8 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl u16 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl u32 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl u64 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl u128 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl usize {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl i8 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl i16 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl i32 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl i64 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl i128 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl isize {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 14, 2025
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Oct 14, 2025

r? @Mark-Simulacrum

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

@bjoernager
Copy link
Copy Markdown
Contributor Author

bjoernager commented Oct 14, 2025

@rustbot label +T-libs-api -T-compiler -T-libs

r? libs-api

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Oct 14, 2025
@rustbot rustbot assigned dtolnay and unassigned Mark-Simulacrum Oct 14, 2025
@rustbot rustbot removed T-libs Relevant to the library team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 14, 2025
@hanna-kruppe
Copy link
Copy Markdown
Contributor

hanna-kruppe commented Oct 14, 2025

Not sure if this detail was discussed on the ACP, but the name set_bit reads to me like its signature should be (&mut self, i: u32, value: bool) -> () and not (as implemented here) (self, i: u32, value: bool) -> Self, which I'd call with_bit_set or with_bit.

@bjoernager bjoernager changed the title Implement bit and set_bit for integral types; Implement bit and set_bit for integral types. Oct 14, 2025
Comment thread library/core/src/num/int_macros.rs Outdated
Comment thread library/core/src/num/int_macros.rs Outdated
@bjoernager
Copy link
Copy Markdown
Contributor Author

Not sure if this detail was discussed on the ACP, but the name set_bit reads to me like its signature should be (&mut self, i: u32) -> () and not (as implemented here) (self, i: u32) -> Self, which I'd call with_bit_set.

Or perhaps just with_bit (given that value does not need to be set)? But I'm inclined to agree.

@hanna-kruppe
Copy link
Copy Markdown
Contributor

hanna-kruppe commented Oct 14, 2025

I glossed over the value argument entirely when I wrote my first comment (edited now). Once I've noticed value I also have questions about it. To be honest, skimming the ACP discussion again, it seems like the setter half wasn't really clearly defined before being given a name and stamp of approval (e.g., two comments by @bjoernager disagreed over whether set_bit has a value parameter or not).

@bjoernager
Copy link
Copy Markdown
Contributor Author

bjoernager commented Oct 14, 2025

two comments by @bjoernager disagreed over whether set_bit has a value parameter or not).

My latter comment should've had the value parameter as well (fixed now).

Comment thread library/core/src/num/int_macros.rs Outdated
@rust-log-analyzer

This comment has been minimized.

@bjoernager bjoernager force-pushed the bit branch 2 times, most recently from 00e99a8 to de2e411 Compare October 14, 2025 19:41
@rust-log-analyzer

This comment has been minimized.

@AljoschaMeyer
Copy link
Copy Markdown
Contributor

Thank you @bjoernager for implementing (and probably championing) this 💜.

Fwiw I also think with_bit is probably the better name. If the doc comments of bit included a "see also with_bit" (and vice versa, for consistency), that could help people find the setter despite the perhaps initially unexpected name.

Comment thread library/core/src/num/int_macros.rs Outdated
Comment thread library/core/src/num/int_macros.rs
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 4, 2025
@Filiprogrammer
Copy link
Copy Markdown

@bjoernager ping

@bjoernager

This comment was marked as resolved.

@rustbot

This comment has been minimized.

@bjoernager

This comment was marked as outdated.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 29, 2025
@bjoernager bjoernager requested a review from dtolnay December 29, 2025 09:55
@rust-log-analyzer

This comment has been minimized.

@bjoernager
Copy link
Copy Markdown
Contributor Author

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

@bjoernager

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor

@folkertdev folkertdev left a comment

Choose a reason for hiding this comment

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

You might want to implement more test coverage in library/coretests/tests/num/int_macros.rs and /library/coretests/tests/num/uint_macros.rs. Don't forget to add the feature to library/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

View changes since this review

Comment thread library/core/src/num/int_macros.rs Outdated
Comment thread library/core/src/num/int_macros.rs Outdated
Comment thread library/core/src/num/int_macros.rs Outdated
Comment thread library/core/src/num/int_macros.rs Outdated
Comment thread library/core/src/num/int_macros.rs Outdated
Comment thread library/core/src/num/uint_macros.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 18, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 18, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 20, 2026

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.

@bjoernager
Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 20, 2026
@rust-log-analyzer

This comment has been minimized.

@bebecue
Copy link
Copy Markdown
Contributor

bebecue commented May 26, 2026

The <$int>::bit() function is useful on its own and has received concerns so far. Shall we split this feature into two, like int_get_bit and int_set_bit? while stabilizing int_get_bit, int_set_bit could still receive further improvements. What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.