//! Support utilities for crate development. use core::any::TypeId; use crate::{ order::BitOrder, store::BitStore, }; /// Constructs formatting-trait implementations by delegating. macro_rules! easy_fmt { ($(impl $fmt:ident)+ for BitArray) => { $( impl core::fmt::$fmt for $crate::array::BitArray where O: $crate::order::BitOrder, A: $crate::view::BitViewSized, { #[inline] #[cfg(not(tarpaulin_include))] fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result { core::fmt::$fmt::fmt(self.as_bitslice(), fmt) } } )+ }; ($(impl $fmt:ident)+ for $this:ident) => { $( impl core::fmt::$fmt for $this where O: $crate::order::BitOrder, T: $crate::store::BitStore, { #[inline] #[cfg(not(tarpaulin_include))] fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result { core::fmt::$fmt::fmt(self.as_bitslice(), fmt) } } )+ }; } /// Implements some `Iterator` functions that have boilerplate behavior. macro_rules! easy_iter { () => { #[inline] fn size_hint(&self) -> (usize, Option) { let len = self.len(); (len, Some(len)) } #[inline] fn count(self) -> usize { self.len() } #[inline] fn last(mut self) -> Option { self.next_back() } }; } /// Tests if two `BitOrder` implementors are the same. #[inline] pub fn match_order() -> bool where O: BitOrder, P: BitOrder, { eq_types::() } /// Tests if two `BitStore` implementors are the same. #[inline] pub fn match_store() -> bool where T: BitStore, U: BitStore, { eq_types::() } /// Tests if two `BitSlice` type parameter pairs match each other. #[inline] pub fn match_types() -> bool where O1: BitOrder, T1: BitStore, O2: BitOrder, T2: BitStore, { match_order::() && match_store::() } /// Tests if a type is known to be an unsigned integer. /// /// Returns `true` for `u{8,16,32,64,128,size}` and `false` for all others. #[inline] pub fn is_unsigned() -> bool where T: 'static { eq_types::() || eq_types::() || eq_types::() || eq_types::() || eq_types::() || eq_types::() } /// Tests if two types are identical, even through different names. #[inline] fn eq_types() -> bool where T: 'static, U: 'static, { TypeId::of::() == TypeId::of::() }