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

implement .trailing_ones(), .leading_ones() #55715

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,27 @@ $EndFeature, "
}
}

doc_comment! {
concat!("Returns the number of leading ones in the binary representation of `self`.

# Examples

Basic usage:

```
", $Feature, "let n = 0b10110011", stringify!($SelfT), ";

assert_eq!(n.leading_ones(), 1);",
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this will pass -- this will have no leading ones for any type size but u8.

$EndFeature, "
```"),
#[unstable(feature = "leading_ones_trailing_ones", issue = "0")]
Copy link
Member

Choose a reason for hiding this comment

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

Not on the libs team but it might be a good idea to open an issue so you have a non-zero number you can refer to here :)

#[rustc_const_unstable(feature = "const_int_ops")]
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd just remove this; I think it's fairly certain that we'll want it to be const fn when stabilizing.

#[inline]
pub const fn leading_ones(self) -> u32 {
(!self as $UnsignedT).leading_zeros()
}
}

doc_comment! {
concat!("Returns the number of trailing zeros in the binary representation of `self`.

Expand All @@ -350,6 +371,27 @@ $EndFeature, "
}
}

doc_comment! {
concat!("Returns the number of trailing ones in the binary representation of `self`.

# Examples

Basic usage:

```
", $Feature, "let n = 0b10110011", stringify!($SelfT), ";
Copy link
Member

Choose a reason for hiding this comment

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

Tidy is saying that you need #![feature(leading_ones_trailing_ones)] in the doctests.


assert_eq!(n.trailing_ones(), 2);",
$EndFeature, "
```"),
#[unstable(feature = "leading_ones_trailing_ones", issue = "0")]
#[rustc_const_unstable(feature = "const_int_ops")]
Copy link
Contributor

Choose a reason for hiding this comment

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

Same, I'd remove this line.

#[inline]
pub const fn trailing_ones(self) -> u32 {
(!self as $UnsignedT).trailing_zeros()
}
}

doc_comment! {
concat!("Shifts the bits to the left by a specified amount, `n`,
wrapping the truncated bits to the end of the resulting integer.
Expand Down Expand Up @@ -2260,6 +2302,26 @@ assert_eq!(n.leading_zeros(), 2);", $EndFeature, "
}
}

doc_comment! {
concat!("Returns the number of leading ones in the binary representation of `self`.

# Examples

Basic usage:

```
", $Feature, "let n = 0b11001100", stringify!($SelfT), ";

assert_eq!(n.leading_ones(), 2);", $EndFeature, "
```"),
#[unstable(feature = "leading_ones_trailing_ones", issue = "0")]
#[rustc_const_unstable(feature = "const_int_ops")]
Copy link
Contributor

Choose a reason for hiding this comment

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

and here...

#[inline]
pub const fn leading_ones(self) -> u32 {
(!self).leading_zeros()
}
}

doc_comment! {
concat!("Returns the number of trailing zeros in the binary representation
of `self`.
Expand All @@ -2281,6 +2343,27 @@ assert_eq!(n.trailing_zeros(), 3);", $EndFeature, "
}
}

doc_comment! {
concat!("Returns the number of trailing ones in the binary representation
of `self`.

# Examples

Basic usage:

```
", $Feature, "let n = 0b0101011", stringify!($SelfT), ";

assert_eq!(n.trailing_ones(), 2);", $EndFeature, "
```"),
#[unstable(feature = "leading_ones_trailing_ones", issue = "0")]
#[rustc_const_unstable(feature = "const_int_ops")]
Copy link
Contributor

Choose a reason for hiding this comment

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

and here.

#[inline]
pub const fn trailing_ones(self) -> u32 {
(!self).trailing_zeros()
}
}

doc_comment! {
concat!("Shifts the bits to the left by a specified amount, `n`,
wrapping the truncated bits to the end of the resulting integer.
Expand Down