1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#![doc = include_str!("../../doc/slice/ops.md")]

use core::ops::{
	BitAnd,
	BitAndAssign,
	BitOr,
	BitOrAssign,
	BitXor,
	BitXorAssign,
	Index,
	IndexMut,
	Not,
	Range,
	RangeFrom,
	RangeFull,
	RangeInclusive,
	RangeTo,
	RangeToInclusive,
};

use super::{
	BitSlice,
	BitSliceIndex,
};
use crate::{
	domain::Domain,
	order::{
		BitOrder,
		Lsb0,
		Msb0,
	},
	store::BitStore,
};

impl<T1, T2, O1, O2> BitAndAssign<&BitSlice<T2, O2>> for BitSlice<T1, O1>
where
	T1: BitStore,
	T2: BitStore,
	O1: BitOrder,
	O2: BitOrder,
{
	#[inline]
	#[doc = include_str!("../../doc/slice/bitop_assign.md")]
	fn bitand_assign(&mut self, rhs: &BitSlice<T2, O2>) {
		if let (Some(this), Some(that)) =
			(self.coerce_mut::<T1, Lsb0>(), rhs.coerce::<T1, Lsb0>())
		{
			return this.sp_bitop_assign(that, BitAnd::bitand, BitAnd::bitand);
		}
		if let (Some(this), Some(that)) =
			(self.coerce_mut::<T1, Msb0>(), rhs.coerce::<T1, Msb0>())
		{
			return this.sp_bitop_assign(that, BitAnd::bitand, BitAnd::bitand);
		}
		for (this, that) in self.as_mut_bitptr_range().zip(rhs.as_bitptr_range())
		{
			unsafe {
				this.write(this.read() & that.read());
			}
		}
		if let Some(rem) = self.get_mut(rhs.len() ..) {
			rem.fill(false);
		}
	}
}

impl<T1, T2, O1, O2> BitOrAssign<&BitSlice<T2, O2>> for BitSlice<T1, O1>
where
	T1: BitStore,
	T2: BitStore,
	O1: BitOrder,
	O2: BitOrder,
{
	#[inline]
	#[doc = include_str!("../../doc/slice/bitop_assign.md")]
	fn bitor_assign(&mut self, rhs: &BitSlice<T2, O2>) {
		if let (Some(this), Some(that)) =
			(self.coerce_mut::<T1, Lsb0>(), rhs.coerce::<T1, Lsb0>())
		{
			return this.sp_bitop_assign(that, BitOr::bitor, BitOr::bitor);
		}
		if let (Some(this), Some(that)) =
			(self.coerce_mut::<T1, Msb0>(), rhs.coerce::<T1, Msb0>())
		{
			return this.sp_bitop_assign(that, BitOr::bitor, BitOr::bitor);
		}
		for (this, that) in self.as_mut_bitptr_range().zip(rhs.as_bitptr_range())
		{
			unsafe {
				this.write(this.read() | that.read());
			}
		}
	}
}

impl<T1, T2, O1, O2> BitXorAssign<&BitSlice<T2, O2>> for BitSlice<T1, O1>
where
	T1: BitStore,
	T2: BitStore,
	O1: BitOrder,
	O2: BitOrder,
{
	#[inline]
	#[doc = include_str!("../../doc/slice/bitop_assign.md")]
	fn bitxor_assign(&mut self, rhs: &BitSlice<T2, O2>) {
		if let (Some(this), Some(that)) =
			(self.coerce_mut::<T1, Lsb0>(), rhs.coerce::<T1, Lsb0>())
		{
			return this.sp_bitop_assign(that, BitXor::bitxor, BitXor::bitxor);
		}
		if let (Some(this), Some(that)) =
			(self.coerce_mut::<T1, Msb0>(), rhs.coerce::<T1, Msb0>())
		{
			return this.sp_bitop_assign(that, BitXor::bitxor, BitXor::bitxor);
		}
		for (this, that) in self.as_mut_bitptr_range().zip(rhs.as_bitptr_range())
		{
			unsafe {
				this.write(this.read() ^ that.read());
			}
		}
	}
}

impl<T, O> Index<usize> for BitSlice<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	type Output = bool;

	/// Looks up a single bit by its semantic index.
	///
	/// ## Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let bits = bits![u8, Msb0; 0, 1, 0];
	/// assert!(!bits[0]); // -----^  |  |
	/// assert!( bits[1]); // --------^  |
	/// assert!(!bits[2]); // -----------^
	/// ```
	///
	/// If the index is greater than or equal to the length, indexing will
	/// panic.
	///
	/// The below test will panic when accessing index 1, as only index 0 is
	/// valid.
	///
	/// ```rust,should_panic
	/// use bitvec::prelude::*;
	///
	/// let bits = bits![0,  ];
	/// bits[1]; // --------^
	/// ```
	#[inline]
	fn index(&self, index: usize) -> &Self::Output {
		match *index.index(self) {
			true => &true,
			false => &false,
		}
	}
}

/// Implements `Index` and `IndexMut` with the given type.
macro_rules! index {
	($($t:ty),+ $(,)?) => { $(
		impl<T, O> Index<$t> for BitSlice<T, O>
		where
			O: BitOrder,
			T: BitStore,
		{
			type Output = Self;

			#[inline]
			#[track_caller]
			fn index(&self, index: $t) -> &Self::Output {
				index.index(self)
			}
		}

		impl<T, O> IndexMut<$t> for BitSlice<T, O>
		where
			O: BitOrder,
			T: BitStore,
		{
			#[inline]
			#[track_caller]
			fn index_mut(&mut self, index: $t) -> &mut Self::Output {
				index.index_mut(self)
			}
		}
	)+ };
}

index! {
	Range<usize>,
	RangeFrom<usize>,
	RangeFull,
	RangeInclusive<usize>,
	RangeTo<usize>,
	RangeToInclusive<usize>,
}

/** Inverts each bit in the bit-slice.

Unlike the `&`, `|`, and `^` operators, this implementation is guaranteed to
update each memory element only once, and is not required to traverse every live
bit in the underlying region.
**/
impl<'a, T, O> Not for &'a mut BitSlice<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	type Output = Self;

	#[inline]
	fn not(self) -> Self::Output {
		match self.domain_mut() {
			Domain::Enclave(mut elem) => {
				elem.invert();
			},
			Domain::Region { head, body, tail } => {
				if let Some(mut elem) = head {
					elem.invert();
				}
				for elem in body {
					elem.store_value(!elem.load_value());
				}
				if let Some(mut elem) = tail {
					elem.invert();
				}
			},
		}
		self
	}
}