Skip to content
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

ACP: add bitwise/bytewise methods to NonZero #413

Closed
pitaj opened this issue Jul 16, 2024 · 5 comments
Closed

ACP: add bitwise/bytewise methods to NonZero #413

pitaj opened this issue Jul 16, 2024 · 5 comments
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@pitaj
Copy link

pitaj commented Jul 16, 2024

Proposal

Problem statement

There are several integer methods which, when applied to a NonZero, could output a NonZero. If added to the NonZero type, these can make applying those operations to NonZero types much easier and remove the need for unwraps.

Motivating examples or use cases

let mut x: NonZeroU32;
x = NonZeroU32::new(x.get().reverse_bits()).unwrap();
x = NonZeroU32::new(x.get().to_be()).unwrap();

Solution sketch

impl<T> NonZero<T> {
    pub fn reverse_bits(self)  -> Self;
    pub fn rotate_left(self) -> Self;
    pub fn rotate_right(self) -> Self;
    pub fn swap_bytes(self) -> Self;
    pub fn to_be(self) -> Self;
    pub fn to_le(self) -> Self;
}

Alternatives

Do nothing, people continue needing to use new_unchecked or unwrap.

Links and related work

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
@pitaj pitaj added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Jul 16, 2024
@Amanieu
Copy link
Member

Amanieu commented Jul 23, 2024

We discussed this in the libs-api meeting today and these seem fine to add.

@Amanieu Amanieu closed this as completed Jul 23, 2024
@Amanieu Amanieu added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Jul 23, 2024
@programmerjake
Copy link
Member

imo you should add the from_be/from_le variants too.

@pitaj
Copy link
Author

pitaj commented Jul 23, 2024

@programmerjake those would be equivalent to

NonZeroU32::new(u32::from_be(x))

Adding them wouldn't avoid any unwraps or unsafe.

@programmerjake
Copy link
Member

@programmerjake those would be equivalent to

NonZeroU32::new(u32::from_be(x))

Adding them wouldn't avoid any unwraps or unsafe.

no, from_be and to_be behave identically, so if to_be is a good idea, from_be should be too. same with *_le variants.

@pitaj
Copy link
Author

pitaj commented Jul 23, 2024

Oh never mind I was thinking of something else. Good point 👍

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 28, 2024
…bilee

bitwise and bytewise methods on `NonZero`

Implementation for `nonzero_bitwise`
Tracking issue rust-lang#128281
ACP rust-lang/libs-team#413
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 28, 2024
…bilee

bitwise and bytewise methods on `NonZero`

Implementation for `nonzero_bitwise`
Tracking issue rust-lang#128281
ACP rust-lang/libs-team#413
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jul 28, 2024
Rollup merge of rust-lang#128282 - pitaj:nonzero_bitwise, r=workingjubilee

bitwise and bytewise methods on `NonZero`

Implementation for `nonzero_bitwise`
Tracking issue rust-lang#128281
ACP rust-lang/libs-team#413
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

3 participants