pub trait IsPowerOfTwo {
fn is_power_of_two(self) -> bool;
}
macro_rules! unsigned_integer_is_power_of_two_impl {
($($integer:ty)*) => ($(
impl IsPowerOfTwo for $integer {
#[inline(always)]
fn is_power_of_two(self) -> bool {
<$integer>::is_power_of_two(self)
}
}
impl IsPowerOfTwo for &$integer {
#[inline(always)]
fn is_power_of_two(self) -> bool {
<$integer>::is_power_of_two(*self)
}
}
)*)
}
unsigned_integer_is_power_of_two_impl!(u8 u16 u32 u64 u128 usize);
trait Unsigned {
type Output;
}
impl Unsigned for i8 {
type Output = u8;
}
impl Unsigned for i16 {
type Output = u8;
}
impl Unsigned for i32 {
type Output = u16;
}
impl Unsigned for i64 {
type Output = u32;
}
impl Unsigned for i128 {
type Output = u64;
}
impl Unsigned for isize {
type Output = usize;
}
macro_rules! signed_integer_is_power_of_two_impl {
($($integer:ty)*) => ($(
impl IsPowerOfTwo for $integer {
#[inline(always)]
fn is_power_of_two(self) -> bool {
<$integer>::is_positive(self)
&& (self as <$integer as Unsigned>::Output)
.is_power_of_two()
}
}
impl IsPowerOfTwo for &$integer {
#[inline(always)]
fn is_power_of_two(self) -> bool {
<$integer>::is_positive(*self)
&& ((*self) as <$integer as Unsigned>::Output)
.is_power_of_two()
}
}
)*)
}
signed_integer_is_power_of_two_impl!(i8 i16 i32 i64 i128 isize);