devela 0.27.0

A development layer of coherence.
Documentation
// devela::num::fin::bit::wise
//
//! Defines the [`Bitwise`] namespace.
//

mod impls;

#[cfg(test)]
mod tests;

#[doc = crate::_tags!(num namespace)]
/// Provides constant bitwise operations on `T`.
#[doc = crate::_doc_location!("num/fin")]
///
/// It's implemented for: [`u8`], [`u16`], [`u32`], [`u64`], [`u128`] and [`usize`].
///
/// ## Panic behavior
/// Unchecked bit operations panic in debug builds when a bit index or range
/// is out of bounds. In release builds they do not panic; the invalid
/// index produces a wrapped shift and a non-meaningful result.
///
/// Checked variants never panic and return an error instead.
///
/// # Related items
/// See also [`BitOps`][crate::BitOps] for the related trait.
///
/// [`u8`]: Self#implementations-for-u8
/// [`u16`]: Self#implementations-for-u16
/// [`u32`]: Self#implementations-for-u32
/// [`u64`]: Self#implementations-for-u64
/// [`u128`]: Self#implementations-for-u128
/// [`usize`]: Self#implementations-for-usize
#[must_use]
#[repr(transparent)]
pub struct Bitwise<T>(pub T);

#[rustfmt::skip]
mod core_impls {
    use crate::{impl_trait, Bitwise, Hash, Hasher, Ordering};

    impl<T: Clone> Clone for Bitwise<T> { fn clone(&self) -> Self { Self(self.0.clone()) } }
    impl<T: Copy> Copy for Bitwise<T> {}

    impl_trait![fmt::Debug for Bitwise[T][T] where T |self, f|
        f.debug_tuple("Bitwise").field(&self.0).finish()
    ];
    impl_trait![fmt::Display for Bitwise[T][T] where T |self, f| self.0.fmt(f)];
    impl_trait![fmt::Binary for Bitwise[T][T] where T |self, f| self.0.fmt(f)];
    impl_trait![fmt::Octal for Bitwise[T][T] where T |self, f| self.0.fmt(f)];
    impl_trait![fmt::LowerHex for Bitwise[T][T] where T |self, f| self.0.fmt(f)];
    impl_trait![fmt::UpperHex for Bitwise[T][T] where T |self, f| self.0.fmt(f)];
    impl_trait![fmt::LowerExp for Bitwise[T][T] where T |self, f| self.0.fmt(f)];
    impl_trait![fmt::UpperExp for Bitwise[T][T] where T |self, f| self.0.fmt(f)];

    impl<T: PartialEq> PartialEq for Bitwise<T> {
        fn eq(&self, other: &Self) -> bool { self.0.eq(&other.0) }
    }
    impl<T: Eq> Eq for Bitwise<T> {}
    impl<T: PartialOrd> PartialOrd for Bitwise<T> {
        fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.0.partial_cmp(&other.0) }
    }
    impl<T: Ord> Ord for Bitwise<T> {
        fn cmp(&self, other: &Self) -> Ordering { self.0.cmp(&other.0) }
    }
    impl<T: Hash> Hash for Bitwise<T> {
        fn hash<H: Hasher>(&self, state: &mut H) { self.0.hash(state); }
    }
    impl<T: Hasher> Hasher for Bitwise<T> {
        fn finish(&self) -> u64 { self.0.finish() }
        fn write(&mut self, bytes: &[u8]) { self.0.write(bytes); }
    }
}