You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The benefit here, as opposed to writing .get().count_ones(), is that NonZero::count_ones returns NonZeroU32 and not u32 (a non-zero number must have at least one bit set!). The implementation is very simple and hasn't changed since the original addition in 1.77.
I understand that this returns NonZeroU32 because <integer>::count_ones returns u32.
But why u32 and not u8 or u16?
u8 would work for all current primitive integers, but not for the potential u256 and i256. u16 would first fail when introducing u65536 and i65536 which seems unlikely to ever happen.
@tyilo it's not necessarily that we have to use 32 bits, and more so that u32 is the default integer type for when you don't really care about the width. you may notice that it's not only count_ones, but a lot of things like BITS, ilog, {leading,trailing}_{zeros,ones}, etc.
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!
See this document for info about what commands tagged team members can give me.
Activity
tyilo commentedon Nov 8, 2024
Anything holding this back from getting stabilized?
WaffleLapkin commentedon Nov 9, 2024
@tyilo nothing that I'm aware of.
@rust-lang/libs-api I would like to propose this feature for stabilization. This feature adds a very straightforward function:
The benefit here, as opposed to writing
.get().count_ones(), is thatNonZero::count_onesreturnsNonZeroU32and notu32(a non-zero number must have at least one bit set!). The implementation is very simple and hasn't changed since the original addition in 1.77.tyilo commentedon Nov 9, 2024
I understand that this returns
NonZeroU32because<integer>::count_onesreturnsu32.But why
u32and notu8oru16?u8would work for all current primitive integers, but not for the potentialu256andi256.u16would first fail when introducingu65536andi65536which seems unlikely to ever happen.WaffleLapkin commentedon Nov 9, 2024
@tyilo it's not necessarily that we have to use 32 bits, and more so that
u32is the default integer type for when you don't really care about the width. you may notice that it's not onlycount_ones, but a lot of things likeBITS,ilog,{leading,trailing}_{zeros,ones}, etc.Amanieu commentedon Nov 25, 2024
@rfcbot fcp merge
rfcbot commentedon Nov 25, 2024
Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members:
No concerns currently listed.
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!
See this document for info about what commands tagged team members can give me.
rfcbot commentedon Jan 27, 2025
🔔 This is now entering its final comment period, as per the review above. 🔔
rfcbot commentedon Feb 6, 2025
The final comment period, with a disposition to merge, as per the review above, is now complete.
As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.
This will be merged soon.
Rollup merge of rust-lang#136663 - WaffleLapkin:count-non-zero-ones, …
Rollup merge of rust-lang#136663 - WaffleLapkin:count-non-zero-ones, …
Unrolled build for rust-lang#136663
Rollup merge of rust-lang#136663 - WaffleLapkin:count-non-zero-ones, …
Rollup merge of rust-lang#136663 - WaffleLapkin:count-non-zero-ones, …
cuviper commentedon Mar 21, 2025
Stabilized in #136663