| 1 |
//! Operator trait implementations for bit-arrays. |
| 2 |
|
| 3 |
use core::ops::{ |
| 4 |
BitAnd, |
| 5 |
BitAndAssign, |
| 6 |
BitOr, |
| 7 |
BitOrAssign, |
| 8 |
BitXor, |
| 9 |
BitXorAssign, |
| 10 |
Deref, |
| 11 |
DerefMut, |
| 12 |
Index, |
| 13 |
IndexMut, |
| 14 |
Not, |
| 15 |
}; |
| 16 |
|
| 17 |
use super::BitArray; |
| 18 |
use crate::{ |
| 19 |
order::BitOrder, |
| 20 |
slice::BitSlice, |
| 21 |
store::BitStore, |
| 22 |
view::BitViewSized, |
| 23 |
}; |
| 24 |
|
| 25 |
#[cfg(not(tarpaulin_include))] |
| 26 |
impl<A, O> BitAndAssign<BitArray<A, O>> for BitSlice<A::Store, O> |
| 27 |
where |
| 28 |
A: BitViewSized, |
| 29 |
O: BitOrder, |
| 30 |
{ |
| 31 |
#[inline] |
| 32 |
fn bitand_assign(&mut self, rhs: BitArray<A, O>) { |
| 33 |
*self &= rhs.as_bitslice() |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
#[cfg(not(tarpaulin_include))] |
| 38 |
impl<A, O> BitAndAssign<&BitArray<A, O>> for BitSlice<A::Store, O> |
| 39 |
where |
| 40 |
A: BitViewSized, |
| 41 |
O: BitOrder, |
| 42 |
{ |
| 43 |
#[inline] |
| 44 |
fn bitand_assign(&mut self, rhs: &BitArray<A, O>) { |
| 45 |
*self &= rhs.as_bitslice() |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
impl<A, O, Rhs> BitAnd<Rhs> for BitArray<A, O> |
| 50 |
where |
| 51 |
A: BitViewSized, |
| 52 |
O: BitOrder, |
| 53 |
BitSlice<A::Store, O>: BitAndAssign<Rhs>, |
| 54 |
{ |
| 55 |
type Output = Self; |
| 56 |
|
| 57 |
#[inline] |
| 58 |
fn bitand(mut self, rhs: Rhs) -> Self::Output { |
| 59 |
self &= rhs; |
| 60 |
self |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
impl<A, O, Rhs> BitAndAssign<Rhs> for BitArray<A, O> |
| 65 |
where |
| 66 |
A: BitViewSized, |
| 67 |
O: BitOrder, |
| 68 |
BitSlice<A::Store, O>: BitAndAssign<Rhs>, |
| 69 |
{ |
| 70 |
#[inline] |
| 71 |
fn bitand_assign(&mut self, rhs: Rhs) { |
| 72 |
*self.as_mut_bitslice() &= rhs; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
#[cfg(not(tarpaulin_include))] |
| 77 |
impl<A, O> BitOrAssign<BitArray<A, O>> for BitSlice<A::Store, O> |
| 78 |
where |
| 79 |
A: BitViewSized, |
| 80 |
O: BitOrder, |
| 81 |
{ |
| 82 |
#[inline] |
| 83 |
fn bitor_assign(&mut self, rhs: BitArray<A, O>) { |
| 84 |
*self |= rhs.as_bitslice() |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
#[cfg(not(tarpaulin_include))] |
| 89 |
impl<A, O> BitOrAssign<&BitArray<A, O>> for BitSlice<A::Store, O> |
| 90 |
where |
| 91 |
A: BitViewSized, |
| 92 |
O: BitOrder, |
| 93 |
{ |
| 94 |
#[inline] |
| 95 |
fn bitor_assign(&mut self, rhs: &BitArray<A, O>) { |
| 96 |
*self |= rhs.as_bitslice() |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
impl<A, O, Rhs> BitOr<Rhs> for BitArray<A, O> |
| 101 |
where |
| 102 |
A: BitViewSized, |
| 103 |
O: BitOrder, |
| 104 |
BitSlice<A::Store, O>: BitOrAssign<Rhs>, |
| 105 |
{ |
| 106 |
type Output = Self; |
| 107 |
|
| 108 |
#[inline] |
| 109 |
fn bitor(mut self, rhs: Rhs) -> Self::Output { |
| 110 |
self |= rhs; |
| 111 |
self |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
impl<A, O, Rhs> BitOrAssign<Rhs> for BitArray<A, O> |
| 116 |
where |
| 117 |
A: BitViewSized, |
| 118 |
O: BitOrder, |
| 119 |
BitSlice<A::Store, O>: BitOrAssign<Rhs>, |
| 120 |
{ |
| 121 |
#[inline] |
| 122 |
fn bitor_assign(&mut self, rhs: Rhs) { |
| 123 |
*self.as_mut_bitslice() |= rhs; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
#[cfg(not(tarpaulin_include))] |
| 128 |
impl<A, O> BitXorAssign<BitArray<A, O>> for BitSlice<A::Store, O> |
| 129 |
where |
| 130 |
A: BitViewSized, |
| 131 |
O: BitOrder, |
| 132 |
{ |
| 133 |
#[inline] |
| 134 |
fn bitxor_assign(&mut self, rhs: BitArray<A, O>) { |
| 135 |
*self ^= rhs.as_bitslice() |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
#[cfg(not(tarpaulin_include))] |
| 140 |
impl<A, O> BitXorAssign<&BitArray<A, O>> for BitSlice<A::Store, O> |
| 141 |
where |
| 142 |
A: BitViewSized, |
| 143 |
O: BitOrder, |
| 144 |
{ |
| 145 |
#[inline] |
| 146 |
fn bitxor_assign(&mut self, rhs: &BitArray<A, O>) { |
| 147 |
*self ^= rhs.as_bitslice() |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
impl<A, O, Rhs> BitXor<Rhs> for BitArray<A, O> |
| 152 |
where |
| 153 |
A: BitViewSized, |
| 154 |
O: BitOrder, |
| 155 |
BitSlice<A::Store, O>: BitXorAssign<Rhs>, |
| 156 |
{ |
| 157 |
type Output = Self; |
| 158 |
|
| 159 |
#[inline] |
| 160 |
fn bitxor(mut self, rhs: Rhs) -> Self::Output { |
| 161 |
self ^= rhs; |
| 162 |
self |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
impl<A, O, Rhs> BitXorAssign<Rhs> for BitArray<A, O> |
| 167 |
where |
| 168 |
A: BitViewSized, |
| 169 |
O: BitOrder, |
| 170 |
BitSlice<A::Store, O>: BitXorAssign<Rhs>, |
| 171 |
{ |
| 172 |
#[inline] |
| 173 |
fn bitxor_assign(&mut self, rhs: Rhs) { |
| 174 |
*self.as_mut_bitslice() ^= rhs; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
impl<A, O> Deref for BitArray<A, O> |
| 179 |
where |
| 180 |
A: BitViewSized, |
| 181 |
O: BitOrder, |
| 182 |
{ |
| 183 |
type Target = BitSlice<A::Store, O>; |
| 184 |
|
| 185 |
#[inline] |
| 186 |
fn deref(&self) -> &Self::Target { |
| 187 |
self.as_bitslice() |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
impl<A, O> DerefMut for BitArray<A, O> |
| 192 |
where |
| 193 |
A: BitViewSized, |
| 194 |
O: BitOrder, |
| 195 |
{ |
| 196 |
#[inline] |
| 197 |
fn deref_mut(&mut self) -> &mut Self::Target { |
| 198 |
self.as_mut_bitslice() |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
impl<A, O, Idx> Index<Idx> for BitArray<A, O> |
| 203 |
where |
| 204 |
A: BitViewSized, |
| 205 |
O: BitOrder, |
| 206 |
BitSlice<A::Store, O>: Index<Idx>, |
| 207 |
{ |
| 208 |
type Output = <BitSlice<A::Store, O> as Index<Idx>>::Output; |
| 209 |
|
| 210 |
#[inline] |
| 211 |
fn index(&self, index: Idx) -> &Self::Output { |
| 212 |
&self.as_bitslice()[index] |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
impl<A, O, Idx> IndexMut<Idx> for BitArray<A, O> |
| 217 |
where |
| 218 |
A: BitViewSized, |
| 219 |
O: BitOrder, |
| 220 |
BitSlice<A::Store, O>: IndexMut<Idx>, |
| 221 |
{ |
| 222 |
#[inline] |
| 223 |
fn index_mut(&mut self, index: Idx) -> &mut Self::Output { |
| 224 |
&mut self.as_mut_bitslice()[index] |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
impl<A, O> Not for BitArray<A, O> |
| 229 |
where |
| 230 |
A: BitViewSized, |
| 231 |
O: BitOrder, |
| 232 |
{ |
| 233 |
type Output = Self; |
| 234 |
|
| 235 |
#[inline] |
| 236 |
fn not(mut self) -> Self::Output { |
| 237 |
for elem in self.as_raw_mut_slice() { |
| 238 |
elem.store_value(!elem.load_value()); |
| 239 |
} |
| 240 |
self |
| 241 |
} |
| 242 |
} |