c0nst::c0nst! {
pub c0nst trait Parity {
fn is_odd(self) -> bool;
fn is_even(self) -> bool;
}
}
macro_rules! parity_impl {
($($t:ty)*) => {$(
c0nst::c0nst! {
c0nst impl Parity for $t {
#[inline]
fn is_odd(self) -> bool {
self & 1 == 1
}
#[inline]
fn is_even(self) -> bool {
self & 1 == 0
}
}
}
)*};
}
parity_impl!(usize u8 u16 u32 u64 u128);
parity_impl!(isize i8 i16 i32 i64 i128);
c0nst::c0nst! {
c0nst impl<T: [c0nst] Parity + Copy> Parity for &T {
#[inline]
fn is_odd(self) -> bool {
(*self).is_odd()
}
#[inline]
fn is_even(self) -> bool {
(*self).is_even()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parity() {
assert!(Parity::is_odd(1u8));
assert!(Parity::is_even(0u8));
assert!(Parity::is_even(u128::MAX - 1));
assert!(Parity::is_odd(u128::MAX));
assert!(Parity::is_odd(-1i8));
assert!(Parity::is_odd(i8::MIN.wrapping_sub(1))); assert!(Parity::is_even(i8::MIN));
assert!(Parity::is_odd(-3i64));
assert!(Parity::is_even(-4i64));
}
}