bitvec/boxed/
ops.rs

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