Skip to content

Tracking issue for integer methods for Wrapping #32463

Open
@SimonSapin

Description

@SimonSapin
Contributor

u32 and other primitive integer types implement a number of bit-manipulation methods like rotate_left, but Wrapping<_> does not. At the moment this can be worked around with code like Wrapping(x.0.rotate_left(n)) instead of x.rotate_left(n).

It would be nice to implement:

  • count_ones
    count_zeroes
    leading_zeroes
    trailing_zeroes
    rotate_left
    rotate_right
    swap_bytes
    from_be (?)
    from_le (?)
    to_be
    to_le
    pow (?)

Edit: Others added after #32463 (comment)

and maybe other methods, for:

  • Wrapping<u8>
  • Wrapping<u16>
  • Wrapping<u32>
  • Wrapping<u64>
  • Wrapping<usize>
  • Wrapping<i8>
  • Wrapping<i16>
  • Wrapping<i32>
  • Wrapping<i64>
  • Wrapping<isize>

Edit: From #50465

  • Decide on correct behavior for wrapping_next_power_of_two

Activity

srinivasreddy

srinivasreddy commented on Mar 24, 2016

@srinivasreddy
Contributor

I want to take this. Would you review my PR?

SimonSapin

SimonSapin commented on Mar 25, 2016

@SimonSapin
ContributorAuthor

I can have a look, but I’m not one of the reviewers who can approve PRs for merging into this repository.

srinivasreddy

srinivasreddy commented on Mar 25, 2016

@srinivasreddy
Contributor

sure that helps. thanks.

added
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
and removed on Mar 24, 2017
added
C-feature-acceptedCategory: A feature request that has been accepted pending implementation.
and removed
C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
on Nov 17, 2017
dtolnay

dtolnay commented on Nov 17, 2017

@dtolnay
Member

I would be open to considering a PR that adds these methods.

added a commit that references this issue on Mar 7, 2018
7e346df
added a commit that references this issue on Mar 10, 2018

Rollup merge of rust-lang#48810 - Phlosioneer:32463-impl-integer-for-…

de827e5
added a commit that references this issue on Mar 12, 2018

Rollup merge of rust-lang#48810 - Phlosioneer:32463-impl-integer-for-…

4bd0ba4

28 remaining items

added
Libs-TrackedLibs issues that are tracked on the team's project board.
on Jul 29, 2020
JohnBSmith

JohnBSmith commented on Nov 24, 2020

@JohnBSmith

There is a further argument for wrapping_next_power_of_two to be zero on overflow. One might use

m.wrapping_next_power_of_two().wrapping_sub(1)

to obtain the bitmask of all numbers in 0..m.

scottmcm

scottmcm commented on Nov 24, 2020

@scottmcm
Member

... to be zero on overflow

Note that next_power_of_two is already stable as wrapping to 0 in release mode, so I think it would be very surprising for wrapping_next_power_of_two to do anything different. (Regardless of which answer is actually better.)

... to obtain the bitmask of ...

If people want that, I would encourage them to propose it as a separate method. It's nicer than next_power_of_two since it has no overflow hazards, so doesn't need the full checked/wrapping/saturating set.

Strawman for a bikeshed: fill_lower_bits.

mjbshaw

mjbshaw commented on Mar 17, 2021

@mjbshaw
Contributor

Is there still debate around the behavior of wrapping_next_power_of_two? I think that next_power_of_two evaluates to 0 (in release mode) on overflow pretty much requires that wrapping_next_power_of_two does the same; it'd be insanely surprising to behave differently.

If there is a debate around this, how can I get involved to push for stabilization?

added a commit that references this issue on May 13, 2021

Rollup merge of rust-lang#85177 - tspiteri:wrapping-bits, r=joshtriplett

16c8254
gendx

gendx commented on Jul 21, 2023

@gendx
Contributor

Seconding @briansmith 's comment from soon 5 years ago, is there anything blocking stabilization of the basic and uncontroversial methods like rotate_left|right, count_zeroes|ones, etc.?

It doesn't seem to me that requiring the nightly compiler is justified for trivial 1-line methods that have been around for many years now.

Given the lack of discussion here, can one directly send a PR to stabilize some methods?

dlight

dlight commented on May 27, 2024

@dlight

How can this be driven forward? Right now Wrapping is less useful than it should be.

Given the lack of discussion here, can one directly send a PR to stabilize some methods?

I hope so!

bjoernager

bjoernager commented on Feb 24, 2025

@bjoernager
Contributor

Would it make sense to make this also include the recently-stabilised cast_unsigned and cast_signed (tracking issue #125882)?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCLibs-SmallLibs issues that are considered "small" or self-containedLibs-TrackedLibs issues that are tracked on the team's project board.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @briansmith@fbstj@steveklabnik@jonhoo@dlight

        Issue actions

          Tracking issue for integer methods for Wrapping · Issue #32463 · rust-lang/rust