//! Operator trait implementations for bit-vectors. use core::{ mem::ManuallyDrop, ops::{ BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Deref, DerefMut, Index, IndexMut, Not, }, }; use wyz::comu::Mut; use super::BitVec; use crate::{ order::BitOrder, ptr::BitSpan, slice::BitSlice, store::BitStore, }; #[cfg(not(tarpaulin_include))] impl BitAndAssign> for BitSlice where T: BitStore, O: BitOrder, { #[inline] fn bitand_assign(&mut self, rhs: BitVec) { *self &= rhs.as_bitslice() } } #[cfg(not(tarpaulin_include))] impl BitAndAssign<&BitVec> for BitSlice where T: BitStore, O: BitOrder, { #[inline] fn bitand_assign(&mut self, rhs: &BitVec) { *self &= rhs.as_bitslice() } } #[cfg(not(tarpaulin_include))] impl BitAnd for BitVec where T: BitStore, O: BitOrder, BitSlice: BitAndAssign, { type Output = Self; #[inline] fn bitand(mut self, rhs: Rhs) -> Self::Output { self &= rhs; self } } #[cfg(not(tarpaulin_include))] impl BitAndAssign for BitVec where T: BitStore, O: BitOrder, BitSlice: BitAndAssign, { #[inline] fn bitand_assign(&mut self, rhs: Rhs) { *self.as_mut_bitslice() &= rhs; } } #[cfg(not(tarpaulin_include))] impl BitOrAssign> for BitSlice where T: BitStore, O: BitOrder, { #[inline] fn bitor_assign(&mut self, rhs: BitVec) { *self |= rhs.as_bitslice() } } #[cfg(not(tarpaulin_include))] impl BitOrAssign<&BitVec> for BitSlice where T: BitStore, O: BitOrder, { #[inline] fn bitor_assign(&mut self, rhs: &BitVec) { *self |= rhs.as_bitslice() } } #[cfg(not(tarpaulin_include))] impl BitOr for BitVec where T: BitStore, O: BitOrder, BitSlice: BitOrAssign, { type Output = Self; #[inline] fn bitor(mut self, rhs: Rhs) -> Self::Output { self |= rhs; self } } #[cfg(not(tarpaulin_include))] impl BitOrAssign for BitVec where T: BitStore, O: BitOrder, BitSlice: BitOrAssign, { #[inline] fn bitor_assign(&mut self, rhs: Rhs) { *self.as_mut_bitslice() |= rhs; } } #[cfg(not(tarpaulin_include))] impl BitXorAssign> for BitSlice where T: BitStore, O: BitOrder, { #[inline] fn bitxor_assign(&mut self, rhs: BitVec) { *self ^= rhs.as_bitslice() } } #[cfg(not(tarpaulin_include))] impl BitXorAssign<&BitVec> for BitSlice where T: BitStore, O: BitOrder, { #[inline] fn bitxor_assign(&mut self, rhs: &BitVec) { *self ^= rhs.as_bitslice() } } #[cfg(not(tarpaulin_include))] impl BitXor for BitVec where T: BitStore, O: BitOrder, BitSlice: BitXorAssign, { type Output = Self; #[inline] fn bitxor(mut self, rhs: Rhs) -> Self::Output { self ^= rhs; self } } #[cfg(not(tarpaulin_include))] impl BitXorAssign for BitVec where T: BitStore, O: BitOrder, BitSlice: BitXorAssign, { #[inline] fn bitxor_assign(&mut self, rhs: Rhs) { *self.as_mut_bitslice() ^= rhs; } } impl Deref for BitVec where T: BitStore, O: BitOrder, { type Target = BitSlice; #[inline] fn deref(&self) -> &Self::Target { self.as_bitslice() } } impl DerefMut for BitVec where T: BitStore, O: BitOrder, { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { self.as_mut_bitslice() } } impl Drop for BitVec where T: BitStore, O: BitOrder, { #[inline] fn drop(&mut self) { if self.bitspan != BitSpan::::EMPTY { self.with_vec(|slot| unsafe { ManuallyDrop::drop(slot) }); } } } #[cfg(not(tarpaulin_include))] impl Index for BitVec where T: BitStore, O: BitOrder, BitSlice: Index, { type Output = as Index>::Output; #[inline] fn index(&self, index: Idx) -> &Self::Output { &self.as_bitslice()[index] } } #[cfg(not(tarpaulin_include))] impl IndexMut for BitVec where T: BitStore, O: BitOrder, BitSlice: IndexMut, { #[inline] fn index_mut(&mut self, index: Idx) -> &mut Self::Output { &mut self.as_mut_bitslice()[index] } } /** This implementation inverts all elements in the live buffer. You cannot rely on the value of bits in the buffer that are outside the domain of [`BitVec::as_mut_bitslice`]. **/ impl Not for BitVec where T: BitStore, O: BitOrder, { type Output = Self; #[inline] fn not(mut self) -> Self::Output { for elem in self.as_raw_mut_slice() { elem.store_value(!elem.load_value()); } self } }