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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#![allow(deprecated)]
#![doc = include_str!("../doc/macros.md")]

#[macro_use]
#[doc(hidden)]
pub mod internal;

mod tests;

#[macro_export]
#[doc = include_str!("../doc/macros/BitArr_type.md")]
macro_rules! BitArr {
	(for $len:expr, in $store:ty, $order:ty $(,)?) => {
		$crate::array::BitArray::<
			[$store; $crate::mem::elts::<$store>($len)], $order
		>
	};

	(for $len:expr, in $store:ty $(,)?) => {
		$crate::BitArr!(for $len, in $store, $crate::order::Lsb0)
	};

	(for $len:expr) => {
		$crate::BitArr!(for $len, in usize)
	};
}

#[macro_export]
#[doc = include_str!("../doc/macros/bitarr_value.md")]
macro_rules! bitarr {
	/* `const`-expression constructors.
	 *
	 * These arms expand to expressions which are guaranteed to be valid in
	 * `const` position: initializing `static` or `const`, or arguments to
	 * `const fn`.
	 *
	 * > Other arms *may* be valid in `const`s, but do not guarantee it.
	 *
	 * They are more restricted than the general variants below, because the
	 * trait system is not yet usable in `const` contexts and thus these
	 * expansions can only use codepaths defined in this module, and cannot use
	 * the rest of `bitvec`’s systems.
	 *
	 * All valid invocations with a leading `const` will remain valid if the
	 * `const` is removed, though their expansion may change to no longer be
	 * valid in `const` contexts.
	 */

	//  Bit-sequencing requires detecting `Cell` separately from other types.
	//  See below.

	(const Cell<$store:ident>, $order:ident; $($val:expr),* $(,)?) => {{
		const ELTS: usize = $crate::__count_elts!($store; $($val),*);
		type Data = [Cell<$store>; ELTS];
		const DATA: Data = $crate::__encode_bits!(Cell<$store>, $order; $($val),*);

		type This = $crate::array::BitArray<Data, $order>;
		This { data: DATA, ..This::ZERO }
	}};
	(const $store:ident, $order:ident; $($val:expr),* $(,)?) => {{
		const ELTS: usize = $crate::__count_elts!($store; $($val),*);
		type Data = [$store; ELTS];
		const DATA: Data = $crate::__encode_bits!($store, $order; $($val),*);

		type This = $crate::array::BitArray<Data, $order>;
		This { data: DATA, ..This::ZERO }
	}};

	//  Bit-repetition is agnostic to types, so it only needs two arms.

	(const $store:ty, $order:ty; $val:expr; $len:expr) => {{
		use $crate::macros::internal::core;
		type Mem = <$store as $crate::store::BitStore>::Mem;

		const ELTS: usize = $crate::mem::elts::<$store>($len);
		const ELEM: Mem = $crate::__extend_bool!($val, $store);
		const DATA: [Mem; ELTS] = [ELEM; ELTS];

		type This = $crate::array::BitArray<[$store; ELTS], $order>;
		unsafe { core::mem::transmute::<_, This>(DATA) }
	}};
	(const $val:expr; $len:expr) => {{
		$crate::bitarr!(const usize, $crate::order::Lsb0; $val; $len)
	}};

	(const $($val:expr),* $(,)?) => {{
		$crate::bitarr!(const usize, Lsb0; $($val),*)
	}};

	/* Non-`const` constructors.
	 *
	 * These expansions are allowed to produce code that does not run in `const`
	 * contexts. While it is *likely* that the expansions will be evaluated at
	 * compile-time, they won’t do so while the `const` engine is active.
	 */

	/* Bit-sequence encoding.
	 *
	 * This requires four arms to the `const` section’s one, because of how both
	 * the ordering and storage arguments may be provided. As macros operate
	 * syntactically, before the type system begins, they have to accept any
	 * syntax that could later be accepted as the name of a satisfying type.
	 *
	 * The `$order:ident` matcher uses the fact that `:ident` matches remain
	 * matchable across deeper macro invocations, so that the bottom of the
	 * macro stack can detect the magic tokens `LocalBits`, `Lsb0`, and `Msb0`,
	 * and operate accordingly. The `$order:path` matcher is always opaque, and
	 * serves as a fallback for complex type-names.
	 *
	 * `Cell<$store>` uses literal detection to extract the interior type width.
	 * This cannot be done by `:ty` or `:path`, as these are opaque, and
	 * `:ident` does not match `Cell<_>`.
	 */

	(Cell<$store:ident>, $order:ident; $($val:expr),* $(,)?) => {{
		use $crate::macros::internal::core;
		type Celled = core::cell::Cell<$store>;

		const ELTS: usize = $crate::__count_elts!($store; $($val),*);
		type Data = [Celled; ELTS];
		type This = $crate::array::BitArray<Data, $order>;

		This::new($crate::__encode_bits!(Cell<$store>, $order; $($val),*))
	}};
	(Cell<$store:ident>, $order:path; $($val:expr),* $(,)?) => {{
		use $crate::macros::internal::core;
		type Celled = core::cell::Cell<$store>;

		const ELTS: usize = $crate::__count_elts!($store; $($val),*);
		type This = $crate::array::BitArray<[Celled; ELTS], $order>;

		This::new($crate::__encode_bits!(Cell<$store>, $order; $($val),*))
	}};

	($store:ident, $order:ident; $($val:expr),* $(,)?) => {{
		const ELTS: usize = $crate::__count_elts!($store; $($val),*);
		type This = $crate::array::BitArray<[$store; ELTS], $order>;

		This::new($crate::__encode_bits!($store, $order; $($val),*))
	}};
	($store:ident, $order:path; $($val:expr),* $(,)?) => {{
		const ELTS: usize = $crate::__count_elts!($store; $($val),*);
		type This = $crate::array::BitArray<[$store; ELTS], $order>;

		This::new($crate::__encode_bits!($store, $order; $($val),*))
	}};


	($store:ty, $order:ty; $val:expr; $len:expr) => {{
		$crate::bitarr!(const $store, $order; $val; $len)
	}};
	($val:expr; $len:expr) => {{
		$crate::bitarr!(const $val; $len)
	}};
	($($val:expr),* $(,)?) => {
		$crate::bitarr!(usize, Lsb0; $($val),*)
	};
}

#[macro_export]
#[doc = include_str!("../doc/macros/bits.md")]
macro_rules! bits {
	/* `&'static` constructors.
	 *
	 * Like the `bitarr!(const …)` arms, these arms must expand to code that is
	 * valid in `const` contexts. As such, they can only accept `$order`
	 * arguments that are one of the `LocalBits`, `Lsb0`, or `Msb0` literals.
	 * Once the underlying `static BitArray` is created,
	 */
	(static mut Cell<$store:ident>, $order:ty; $val:expr; $len:expr) => {{
		use $crate::macros::internal::core;
		type Celled = core::cell::Cell<$store>;
		static mut DATA: $crate::BitArr!(for $len, in Celled, $order) =
			$crate::bitarr!(const Cell<$store>, $order; $val; $len);
		 &mut DATA[.. $len]
	}};
	(static mut $store:ident, $order:ident; $val:expr; $len:expr) => {{
		static mut DATA: $crate::BitArr!(for $len, in $store, $order) =
			$crate::bitarr!(const $store, $order; $val; $len);
		DATA.get_unchecked_mut(.. $len)
	}};

	(static mut Cell<$store:ident>, $order:ident; $($val:expr),* $(,)?) => {{
		use $crate::macros::internal::core;
		type Celled = core::cell::Cell<$store>;
		const BITS: usize = $crate::__count!($($val),*);

		static mut DATA: $crate::BitArr!(for BITS, in $store, $order) =
			$crate::bitarr!(const $store, $order; $($val),*);
		&mut *(
			DATA.get_unchecked_mut(.. BITS)
				as *mut $crate::slice::BitSlice<$store, $order>
				as *mut $crate::slice::BitSlice<Celled, $order>
		)
	}};
	(static mut $store:ident, $order:ident; $($val:expr),* $(,)?) => {{
		const BITS: usize = $crate::__count!($($val),*);
		static mut DATA: $crate::BitArr!(for BITS, in $store, $order) =
			$crate::bitarr!(const $store, $order; $($val),*);
		DATA.get_unchecked_mut(.. BITS)
	}};

	(static mut $val:expr; $len:expr) => {{
		static mut DATA: $crate::BitArr!(for $len) =
			$crate::bitarr!(const usize, $crate::order::Lsb0; $val; $len);
		DATA.get_unchecked_mut(.. $len)
	}};
	(static mut $($val:expr),* $(,)?) => {{
		$crate::bits!(static mut usize, Lsb0; $($val),*)
	}};

	(static Cell<$store:ident>, $order:ty; $val:expr; $len:expr) => {{
		use $crate::macros::internal::core;
		type Celled = core::cell::Cell<$store>;
		static DATA: $crate::BitArr!(for $len, in $store, $order) =
			$crate::bitarr!(const $store, $order; $val; $len);
		unsafe {
			&*(
				DATA.get_unchecked(.. $len)
					as *const $crate::slice::BitSlice<$store, $order>
					as *const $crate::slice::BitSlice<Celled, $order>
			)
		}
	}};
	(static Cell<$store:ident>, $order:ident; $($val:expr),* $(,)?) => {{
		use $crate::macros::internal::core;
		type Celled = core::cell::Cell<$store>;
		const BITS: usize = $crate::__count!($($val),*);

		static DATA: $crate::BitArr!(for BITS, in $store, $order) =
			$crate::bitarr!(const $store, $order; $($val),*);
		unsafe {
			&*(
				DATA.get_unchecked(.. BITS)
					as *const $crate::slice::BitSlice<$store, $order>
					as *const $crate::slice::BitSlice<Celled, $order>
			)
		}
	}};

	(static $store:ident, $order:ident; $val:expr; $len:expr) => {{
		static DATA: $crate::BitArr!(for $len, in $store, $order) =
			$crate::bitarr!(const $store, $order; $val; $len);
		unsafe { DATA.get_unchecked(.. $len) }
	}};
	(static $val:expr; $len:expr) => {{
		static DATA: $crate::BitArr!(for $len) =
			$crate::bitarr!(const usize, $crate::order::Lsb0; $val; $len);
		unsafe { DATA.get_unchecked(.. $len) }
	}};

	(static $store:ident, $order:ident; $($val:expr),* $(,)?) => {{
		const BITS: usize = $crate::__count!($($val),*);
		static DATA: $crate::BitArr!(for BITS, in $store, $order) =
			$crate::bitarr!(const $store, $order; $($val),*);
		unsafe { DATA.get_unchecked(.. BITS) }
	}};
	(static $($val:expr),* $(,)?) => {{
		$crate::bits!(static usize, Lsb0; $($val),*)
	}};

	//  Repetition syntax `[bit ; count]`.
	//  NOTE: `count` must be a `const`, as this is a non-allocating macro.

	//  Sequence syntax `[bit (, bit)*]` or `[(bit ,)*]`.

	//  Explicit order and store.

	(mut Cell<$store:ident>, $order:ident; $($val:expr),* $(,)?) => {{
		const BITS: usize = $crate::__count!($($val),*);
		&mut $crate::bitarr!(Cell<$store>, $order; $($val),*)[.. BITS]
	}};
	(mut Cell<$store:ident>, $order:path; $($val:expr),* $(,)?) => {{
		const BITS: usize = $crate::__count!($($val),*);
		&mut $crate::bitarr!(Cell<$store>, $order; $($val),*)[.. BITS]
	}};

	(mut $store:ident, $order:ident; $($val:expr),* $(,)?) => {{
		const BITS: usize = $crate::__count!($($val),*);
		&mut $crate::bitarr!($store, $order; $($val),*)[.. BITS]
	}};
	(mut $store:ident, $order:path; $($val:expr),* $(,)?) => {{
		const BITS: usize = $crate::__count!($($val),*);
		&mut $crate::bitarr!($store, $order; $($val),*)[.. BITS]
	}};

	//  Explicit order and store.
	(mut $store:ty, $order:ty; $val:expr; $len:expr) => {{
		&mut $crate::bitarr!($store, $order; $val; $len)[.. $len]
	}};
	//  Default order and store.
	(mut $val:expr; $len:expr) => {
		$crate::bits!(mut usize, $crate::order::Lsb0; $val; $len)
	};

	//  Default order and store.
	(mut $($val:expr),* $(,)?) => {
		$crate::bits!(mut usize, Lsb0; $($val),*)
	};

	//  Repeat everything from above, but now immutable.

	($store:ty, $order:ty; $val:expr; $len:expr) => {{
		&$crate::bitarr!($store, $order; $val; $len)[.. $len]
	}};

	(Cell<$store:ident>, $order:ident; $($val:expr),* $(,)?) => {{
		const BITS: usize = $crate::__count!($($val),*);
		&$crate::bitarr!(Cell<$store>, $order; $($val),*)[.. BITS]
	}};
	($store:ident, $order:ident; $($val:expr),* $(,)?) => {{
		const BITS: usize = $crate::__count!($($val),*);
		&$crate::bitarr!($store, $order; $($val),*)[.. BITS]
	}};

	(Cell<$store:ident>, $order:path; $($val:expr),* $(,)?) => {{
		const BITS: usize = $crate::__count!($($val),*);
		&$crate::bitarr!(Cell<$store>, $order; $($val),*)[.. BITS]
	}};
	($store:ident, $order:path; $($val:expr),* $(,)?) => {{
		const BITS: usize = $crate::__count!($($val),*);
		&$crate::bitarr!($store, $order; $($val),*)[.. BITS]
	}};

	//  Default order and store.
	($val:expr; $len:expr) => {
		$crate::bits!(usize, $crate::order::Lsb0; $val; $len)
	};
	($($val:expr),* $(,)?) => {
		$crate::bits!(usize, Lsb0; $($val),*)
	};
}

#[macro_export]
#[cfg(feature = "alloc")]
#[doc = include_str!("../doc/macros/bitvec.md")]
macro_rules! bitvec {
	//  First, capture the repetition syntax, as it is permitted to use runtime
	//  values for the repetition count.
	($store:ty, $order:ty; $val:expr; $len:expr) => {
		$crate::vec::BitVec::<$store, $order>::repeat($val != 0, $len)
	};
	// Capture `Cell<T>` patterns and prevent them from being parsed as
	// comparisons. Guess we didn't escape Most Vexing Parse after all.
	(Cell<$store:ident>, $order:ident $($rest:tt)*) => {
		$crate::vec::BitVec::from_bitslice($crate::bits!(Cell<$store>, $order $($rest)*))
	};
	($val:expr; $len:expr) => {
		$crate::bitvec!(usize, $crate::order::Lsb0; $val; $len)
	};

	//  Delegate all others to the `bits!` macro.
	($($arg:tt)*) => {
		$crate::vec::BitVec::from_bitslice($crate::bits!($($arg)*))
	};
}

#[macro_export]
#[cfg(feature = "alloc")]
#[doc = include_str!("../doc/macros/bitbox.md")]
macro_rules! bitbox {
	($($arg:tt)*) => {
		$crate::bitvec!($($arg)*).into_boxed_bitslice()
	};
}