From a12788a0d77f1ad906bc1d51c4684dd8723278e0 Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Thu, 22 Aug 2019 19:57:48 +0200 Subject: [PATCH] make is_power_of_two a const function --- src/libcore/num/mod.rs | 4 ++-- src/test/ui/consts/const-int-pow-rpass.rs | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/consts/const-int-pow-rpass.rs diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index ebde82de834..82c1943b7d0 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -3749,8 +3749,8 @@ assert!(!10", stringify!($SelfT), ".is_power_of_two());", $EndFeature, " ```"), #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn is_power_of_two(self) -> bool { - (self.wrapping_sub(1)) & self == 0 && !(self == 0) + pub const fn is_power_of_two(self) -> bool { + ((self.wrapping_sub(1)) & self == 0) & !(self == 0) } } diff --git a/src/test/ui/consts/const-int-pow-rpass.rs b/src/test/ui/consts/const-int-pow-rpass.rs new file mode 100644 index 00000000000..8e84a900605 --- /dev/null +++ b/src/test/ui/consts/const-int-pow-rpass.rs @@ -0,0 +1,11 @@ +// run-pass + +const IS_POWER_OF_TWO_A: bool = 0u32.is_power_of_two(); +const IS_POWER_OF_TWO_B: bool = 32u32.is_power_of_two(); +const IS_POWER_OF_TWO_C: bool = 33u32.is_power_of_two(); + +fn main() { + assert!(!IS_POWER_OF_TWO_A); + assert!(IS_POWER_OF_TWO_B); + assert!(!IS_POWER_OF_TWO_C); +}