#![doc = include_str!("../doc/view.md")] use core::slice; use crate::{ array::BitArray, order::BitOrder, ptr::BitSpanError, slice::BitSlice, store::BitStore, }; #[doc = include_str!("../doc/view/BitView.md")] pub trait BitView { /// The underlying element type. type Store: BitStore; /// Views a memory region as an immutable bit-slice. fn view_bits(&self) -> &BitSlice where O: BitOrder; /// Attempts to view a memory region as an immutable bit-slice. /// /// This may return an error if `self` is too long to view as a bit-slice. fn try_view_bits( &self, ) -> Result<&BitSlice, BitSpanError> where O: BitOrder; /// Views a memory region as a mutable bit-slice. fn view_bits_mut(&mut self) -> &mut BitSlice where O: BitOrder; /// Attempts to view a memory region as a mutable bit-slice. /// /// This may return an error if `self` is too long to view as a bit-slice. fn try_view_bits_mut( &mut self, ) -> Result<&mut BitSlice, BitSpanError> where O: BitOrder; } #[cfg(not(tarpaulin_include))] impl BitView for T where T: BitStore { type Store = Self; fn view_bits(&self) -> &BitSlice where O: BitOrder { BitSlice::from_element(self) } fn try_view_bits(&self) -> Result<&BitSlice, BitSpanError> where O: BitOrder { Ok(BitSlice::from_element(self)) } fn view_bits_mut(&mut self) -> &mut BitSlice where O: BitOrder { BitSlice::from_element_mut(self) } fn try_view_bits_mut( &mut self, ) -> Result<&mut BitSlice, BitSpanError> where O: BitOrder { Ok(BitSlice::from_element_mut(self)) } } /// Note that overly-large slices may cause the conversions to fail. #[cfg(not(tarpaulin_include))] impl BitView for [T] where T: BitStore { type Store = T; #[inline] fn view_bits(&self) -> &BitSlice where O: BitOrder { BitSlice::from_slice(self) } #[inline] fn try_view_bits(&self) -> Result<&BitSlice, BitSpanError> where O: BitOrder { BitSlice::try_from_slice(self) } #[inline] fn view_bits_mut(&mut self) -> &mut BitSlice where O: BitOrder { BitSlice::from_slice_mut(self) } #[inline] fn try_view_bits_mut( &mut self, ) -> Result<&mut BitSlice, BitSpanError> where O: BitOrder { BitSlice::try_from_slice_mut(self) } } /// Note that overly-large arrays may cause the conversions to fail. #[cfg(not(tarpaulin_include))] impl BitView for [T; N] where T: BitStore { type Store = T; #[inline] fn view_bits(&self) -> &BitSlice where O: BitOrder { BitSlice::from_slice(self) } #[inline] fn try_view_bits(&self) -> Result<&BitSlice, BitSpanError> where O: BitOrder { BitSlice::try_from_slice(self) } #[inline] fn view_bits_mut(&mut self) -> &mut BitSlice where O: BitOrder { BitSlice::from_slice_mut(self) } #[inline] fn try_view_bits_mut( &mut self, ) -> Result<&mut BitSlice, BitSpanError> where O: BitOrder { BitSlice::try_from_slice_mut(self) } } /// Helper trait for scalars and arrays, but not slices. pub trait BitViewSized: BitView + Sized { /// The zero constant. const ZERO: Self; /// Wraps `self` in a `BitArray`. #[inline] fn into_bitarray(self) -> BitArray where O: BitOrder { BitArray::new(self) } /// Views the type as a slice of its elements. fn as_raw_slice(&self) -> &[Self::Store]; /// Views the type as a mutable slice of its elements. fn as_raw_mut_slice(&mut self) -> &mut [Self::Store]; } impl BitViewSized for T where T: BitStore { const ZERO: Self = ::ZERO; #[inline] fn as_raw_slice(&self) -> &[Self::Store] { slice::from_ref(self) } #[inline] fn as_raw_mut_slice(&mut self) -> &mut [Self::Store] { slice::from_mut(self) } } impl BitViewSized for [T; N] where T: BitStore { const ZERO: Self = [T::ZERO; N]; #[inline] fn as_raw_slice(&self) -> &[Self::Store] { &self[..] } #[inline] fn as_raw_mut_slice(&mut self) -> &mut [Self::Store] { &mut self[..] } } #[doc = include_str!("../doc/view/AsBits.md")] pub trait AsBits where T: BitStore { /// Views `self` as an immutable bit-slice region with the `O` ordering. fn as_bits(&self) -> &BitSlice where O: BitOrder; /// Attempts to view `self` as an immutable bit-slice region with the `O` /// ordering. /// /// This may return an error if `self` is too long to view as a bit-slice. fn try_as_bits(&self) -> Result<&BitSlice, BitSpanError> where O: BitOrder; } #[doc = include_str!("../doc/view/AsMutBits.md")] pub trait AsMutBits where T: BitStore { /// Views `self` as a mutable bit-slice region with the `O` ordering. fn as_mut_bits(&mut self) -> &mut BitSlice where O: BitOrder; /// Attempts to view `self` as a mutable bit-slice region with the `O` /// ordering. /// /// This may return an error if `self` is too long to view as a bit-slice. fn try_as_mut_bits( &mut self, ) -> Result<&mut BitSlice, BitSpanError> where O: BitOrder; } #[cfg(not(tarpaulin_include))] impl AsBits for A where A: AsRef<[T]>, T: BitStore, { #[inline] fn as_bits(&self) -> &BitSlice where O: BitOrder { self.as_ref().view_bits::() } #[inline] fn try_as_bits(&self) -> Result<&BitSlice, BitSpanError> where O: BitOrder { self.as_ref().try_view_bits::() } } #[cfg(not(tarpaulin_include))] impl AsMutBits for A where A: AsMut<[T]>, T: BitStore, { #[inline] fn as_mut_bits(&mut self) -> &mut BitSlice where O: BitOrder { self.as_mut().view_bits_mut::() } #[inline] fn try_as_mut_bits( &mut self, ) -> Result<&mut BitSlice, BitSpanError> where O: BitOrder { self.as_mut().try_view_bits_mut::() } } #[cfg(test)] mod tests { use static_assertions::*; use super::*; use crate::prelude::*; #[test] fn implementations() { let mut byte = 0u8; let mut bytes = [0u8; 2]; assert!(byte.view_bits::().not_any()); assert!(byte.view_bits_mut::().not_any()); assert!(bytes.view_bits::().not_any()); assert!(bytes.view_bits_mut::().not_any()); assert!(bytes[..].view_bits::().not_any()); assert!(bytes[..].view_bits_mut::().not_any()); let mut blank: [u8; 0] = []; assert!(blank.view_bits::().is_empty()); assert!(blank.view_bits_mut::().is_empty()); assert_eq!([0u32; 2].as_bits::().len(), 64); assert_eq!([0u32; 2].as_mut_bits::().len(), 64); assert_eq!(0usize.as_raw_slice().len(), 1); assert_eq!(0usize.as_raw_mut_slice().len(), 1); assert_eq!(0u32.into_bitarray::().len(), 32); assert_impl_all!( [usize; 10]: AsBits, AsMutBits, BitViewSized, BitView, ); } }