1 //! Operator implementations for `BitVec`. 2 3 use crate::{ 4 devel as dvl, 5 order::BitOrder, 6 slice::BitSlice, 7 store::BitStore, 8 vec::BitVec, 9 }; 10 11 use core::{ 12 mem::ManuallyDrop, 13 ops::{ 14 BitAnd, 15 BitAndAssign, 16 BitOr, 17 BitOrAssign, 18 BitXor, 19 BitXorAssign, 20 Deref, 21 DerefMut, 22 Index, 23 IndexMut, 24 Not, 25 }, 26 }; 27 28 #[cfg(not(tarpaulin_include))] 29 impl<O, T, Rhs> BitAnd<Rhs> for BitVec<O, T> 30 where 31 O: BitOrder, 32 T: BitStore, 33 BitSlice<O, T>: BitAndAssign<Rhs>, 34 { 35 type Output = Self; 36 37 #[inline] bitand(mut self, rhs: Rhs) -> Self::Output38 fn bitand(mut self, rhs: Rhs) -> Self::Output { 39 *self.as_mut_bitslice() &= rhs; 40 self 41 } 42 } 43 44 #[cfg(not(tarpaulin_include))] 45 impl<O, T, Rhs> BitAndAssign<Rhs> for BitVec<O, T> 46 where 47 O: BitOrder, 48 T: BitStore, 49 BitSlice<O, T>: BitAndAssign<Rhs>, 50 { 51 #[inline] bitand_assign(&mut self, rhs: Rhs)52 fn bitand_assign(&mut self, rhs: Rhs) { 53 *self.as_mut_bitslice() &= rhs; 54 } 55 } 56 57 #[cfg(not(tarpaulin_include))] 58 impl<O, T, Rhs> BitOr<Rhs> for BitVec<O, T> 59 where 60 O: BitOrder, 61 T: BitStore, 62 BitSlice<O, T>: BitOrAssign<Rhs>, 63 { 64 type Output = Self; 65 66 #[inline] bitor(mut self, rhs: Rhs) -> Self::Output67 fn bitor(mut self, rhs: Rhs) -> Self::Output { 68 *self.as_mut_bitslice() |= rhs; 69 self 70 } 71 } 72 73 #[cfg(not(tarpaulin_include))] 74 impl<O, T, Rhs> BitOrAssign<Rhs> for BitVec<O, T> 75 where 76 O: BitOrder, 77 T: BitStore, 78 BitSlice<O, T>: BitOrAssign<Rhs>, 79 { 80 #[inline] bitor_assign(&mut self, rhs: Rhs)81 fn bitor_assign(&mut self, rhs: Rhs) { 82 *self.as_mut_bitslice() |= rhs; 83 } 84 } 85 86 #[cfg(not(tarpaulin_include))] 87 impl<O, T, Rhs> BitXor<Rhs> for BitVec<O, T> 88 where 89 O: BitOrder, 90 T: BitStore, 91 BitSlice<O, T>: BitXorAssign<Rhs>, 92 { 93 type Output = Self; 94 95 #[inline] bitxor(mut self, rhs: Rhs) -> Self::Output96 fn bitxor(mut self, rhs: Rhs) -> Self::Output { 97 *self.as_mut_bitslice() ^= rhs; 98 self 99 } 100 } 101 102 #[cfg(not(tarpaulin_include))] 103 impl<O, T, Rhs> BitXorAssign<Rhs> for BitVec<O, T> 104 where 105 O: BitOrder, 106 T: BitStore, 107 BitSlice<O, T>: BitXorAssign<Rhs>, 108 { 109 #[inline] bitxor_assign(&mut self, rhs: Rhs)110 fn bitxor_assign(&mut self, rhs: Rhs) { 111 *self.as_mut_bitslice() ^= rhs; 112 } 113 } 114 115 #[cfg(not(tarpaulin_include))] 116 impl<O, T> Deref for BitVec<O, T> 117 where 118 O: BitOrder, 119 T: BitStore, 120 { 121 type Target = BitSlice<O, T>; 122 123 #[inline(always)] deref(&self) -> &Self::Target124 fn deref(&self) -> &Self::Target { 125 self.as_bitslice() 126 } 127 } 128 129 #[cfg(not(tarpaulin_include))] 130 impl<O, T> DerefMut for BitVec<O, T> 131 where 132 O: BitOrder, 133 T: BitStore, 134 { 135 #[inline(always)] deref_mut(&mut self) -> &mut Self::Target136 fn deref_mut(&mut self) -> &mut Self::Target { 137 self.as_mut_bitslice() 138 } 139 } 140 141 impl<O, T> Drop for BitVec<O, T> 142 where 143 O: BitOrder, 144 T: BitStore, 145 { 146 #[inline] drop(&mut self)147 fn drop(&mut self) { 148 // The buffer elements do not have destructors. 149 self.clear(); 150 // Run the `Vec` destructor to deällocate the buffer. 151 self.with_vec(|vec| unsafe { ManuallyDrop::drop(vec) }); 152 } 153 } 154 155 #[cfg(not(tarpaulin_include))] 156 impl<O, T, Idx> Index<Idx> for BitVec<O, T> 157 where 158 O: BitOrder, 159 T: BitStore, 160 BitSlice<O, T>: Index<Idx>, 161 { 162 type Output = <BitSlice<O, T> as Index<Idx>>::Output; 163 164 #[inline] index(&self, index: Idx) -> &Self::Output165 fn index(&self, index: Idx) -> &Self::Output { 166 self.as_bitslice().index(index) 167 } 168 } 169 170 #[cfg(not(tarpaulin_include))] 171 impl<O, T, Idx> IndexMut<Idx> for BitVec<O, T> 172 where 173 O: BitOrder, 174 T: BitStore, 175 BitSlice<O, T>: IndexMut<Idx>, 176 { 177 #[inline] index_mut(&mut self, index: Idx) -> &mut Self::Output178 fn index_mut(&mut self, index: Idx) -> &mut Self::Output { 179 self.as_mut_bitslice().index_mut(index) 180 } 181 } 182 183 /** This implementation inverts all elements in the live buffer. You cannot rely 184 on the value of bits in the buffer that are outside the domain of 185 `BitVec::as_mit_bitslice`. 186 **/ 187 #[cfg(not(tarpaulin_include))] 188 impl<O, T> Not for BitVec<O, T> 189 where 190 O: BitOrder, 191 T: BitStore, 192 { 193 type Output = Self; 194 195 #[inline] not(mut self) -> Self::Output196 fn not(mut self) -> Self::Output { 197 for elem in self.as_mut_slice().iter_mut().map(dvl::mem_mut) { 198 *elem = !*elem; 199 } 200 self 201 } 202 } 203