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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#![doc = include_str!("../../doc/slice/traits.md")]

#[cfg(feature = "alloc")]
use alloc::borrow::ToOwned;
use core::{
	cmp,
	convert::TryFrom,
	fmt::{
		self,
		Binary,
		Debug,
		Display,
		Formatter,
		LowerHex,
		Octal,
		Pointer,
		UpperHex,
	},
	hash::{
		Hash,
		Hasher,
	},
	str,
};

use wyz::fmt::FmtForward;

use super::BitSlice;
#[cfg(feature = "alloc")]
use crate::vec::BitVec;
use crate::{
	domain::Domain,
	mem,
	order::{
		BitOrder,
		Lsb0,
		Msb0,
	},
	store::BitStore,
	view::BitView,
};

/// [Original](https://doc.rust-lang.org/std/primitive.slice.html#impl-AsRef%3C%5BT%5D%3E)
impl<T, O> AsRef<Self> for BitSlice<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn as_ref(&self) -> &Self {
		self
	}
}

/// [Original](https://doc.rust-lang.org/std/primitive.slice.html#impl-AsMut%3C%5BT%5D%3E)
impl<T, O> AsMut<Self> for BitSlice<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn as_mut(&mut self) -> &mut Self {
		self
	}
}

/// [Original](https://doc.rust-lang.org/std/primitive.slice.html#impl-Eq)
impl<T, O> Eq for BitSlice<T, O>
where
	T: BitStore,
	O: BitOrder,
{
}

/// [Original](https://doc.rust-lang.org/std/primitive.slice.html#impl-Ord)
impl<T, O> Ord for BitSlice<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn cmp(&self, rhs: &Self) -> cmp::Ordering {
		self.partial_cmp(rhs)
			.expect("BitSlice has a total ordering")
	}
}

/** Tests if two `BitSlice`s are semantically — not representationally — equal.

It is valid to compare slices of different ordering or memory types.

The equality condition requires that they have the same length and that at each
index, the two slices have the same bit value.

[Original](https://doc.rust-lang.org/std/primitive.slice.html#impl-PartialEq%3C%5BB%5D%3E)
**/
impl<T1, T2, O1, O2> PartialEq<BitSlice<T2, O2>> for BitSlice<T1, O1>
where
	T1: BitStore,
	T2: BitStore,
	O1: BitOrder,
	O2: BitOrder,
{
	#[inline]
	fn eq(&self, rhs: &BitSlice<T2, O2>) -> bool {
		if let (Some(this), Some(that)) =
			(self.coerce::<T1, Lsb0>(), rhs.coerce::<T1, Lsb0>())
		{
			this.sp_eq(that)
		}
		else if let (Some(this), Some(that)) =
			(self.coerce::<T1, Msb0>(), rhs.coerce::<T1, Msb0>())
		{
			this.sp_eq(that)
		}
		else {
			self.len() == rhs.len()
				&& self
					.iter()
					.by_vals()
					.zip(rhs.iter().by_vals())
					.all(|(l, r)| l == r)
		}
	}
}

//  ref-to-val equality

impl<T1, T2, O1, O2> PartialEq<BitSlice<T2, O2>> for &BitSlice<T1, O1>
where
	T1: BitStore,
	T2: BitStore,
	O1: BitOrder,
	O2: BitOrder,
{
	#[inline]
	fn eq(&self, rhs: &BitSlice<T2, O2>) -> bool {
		**self == rhs
	}
}

impl<T1, T2, O1, O2> PartialEq<BitSlice<T2, O2>> for &mut BitSlice<T1, O1>
where
	T1: BitStore,
	T2: BitStore,
	O1: BitOrder,
	O2: BitOrder,
{
	#[inline]
	fn eq(&self, rhs: &BitSlice<T2, O2>) -> bool {
		**self == rhs
	}
}

//  val-to-ref equality

impl<T1, T2, O1, O2> PartialEq<&BitSlice<T2, O2>> for BitSlice<T1, O1>
where
	T1: BitStore,
	T2: BitStore,
	O1: BitOrder,
	O2: BitOrder,
{
	#[inline]
	fn eq(&self, rhs: &&BitSlice<T2, O2>) -> bool {
		*self == **rhs
	}
}

impl<T1, T2, O1, O2> PartialEq<&mut BitSlice<T2, O2>> for BitSlice<T1, O1>
where
	T1: BitStore,
	T2: BitStore,
	O1: BitOrder,
	O2: BitOrder,
{
	#[inline]
	fn eq(&self, rhs: &&mut BitSlice<T2, O2>) -> bool {
		*self == **rhs
	}
}

/** Compares two `BitSlice`s by semantic — not representational — ordering.

The comparison sorts by testing at each index if one slice has a high bit where
the other has a low. At the first index where the slices differ, the slice with
the high bit is greater. If the slices are equal until at least one terminates,
then they are compared by length.

[Original](https://doc.rust-lang.org/std/primitive.slice.html#impl-PartialOrd%3C%5BT%5D%3E)
**/
impl<T1, T2, O1, O2> PartialOrd<BitSlice<T2, O2>> for BitSlice<T1, O1>
where
	T1: BitStore,
	T2: BitStore,
	O1: BitOrder,
	O2: BitOrder,
{
	#[inline]
	fn partial_cmp(&self, rhs: &BitSlice<T2, O2>) -> Option<cmp::Ordering> {
		for (l, r) in self.iter().by_vals().zip(rhs.iter().by_vals()) {
			match (l, r) {
				(true, false) => return Some(cmp::Ordering::Greater),
				(false, true) => return Some(cmp::Ordering::Less),
				_ => continue,
			}
		}
		self.len().partial_cmp(&rhs.len())
	}
}

//  ref-to-val ordering

impl<T1, T2, O1, O2> PartialOrd<BitSlice<T2, O2>> for &BitSlice<T1, O1>
where
	T1: BitStore,
	T2: BitStore,
	O1: BitOrder,
	O2: BitOrder,
{
	#[inline]
	fn partial_cmp(&self, rhs: &BitSlice<T2, O2>) -> Option<cmp::Ordering> {
		(*self).partial_cmp(rhs)
	}
}

impl<T1, T2, O1, O2> PartialOrd<BitSlice<T2, O2>> for &mut BitSlice<T1, O1>
where
	T1: BitStore,
	T2: BitStore,
	O1: BitOrder,
	O2: BitOrder,
{
	#[inline]
	fn partial_cmp(&self, rhs: &BitSlice<T2, O2>) -> Option<cmp::Ordering> {
		(**self).partial_cmp(rhs)
	}
}

//  val-to-ref ordering

impl<T1, T2, O1, O2> PartialOrd<&BitSlice<T2, O2>> for BitSlice<T1, O1>
where
	T1: BitStore,
	T2: BitStore,
	O1: BitOrder,
	O2: BitOrder,
{
	#[inline]
	fn partial_cmp(&self, rhs: &&BitSlice<T2, O2>) -> Option<cmp::Ordering> {
		(*self).partial_cmp(&**rhs)
	}
}

impl<T1, T2, O1, O2> PartialOrd<&mut BitSlice<T2, O2>> for BitSlice<T1, O1>
where
	T1: BitStore,
	T2: BitStore,
	O1: BitOrder,
	O2: BitOrder,
{
	#[inline]
	fn partial_cmp(&self, rhs: &&mut BitSlice<T2, O2>) -> Option<cmp::Ordering> {
		(*self).partial_cmp(&**rhs)
	}
}

//  &mut-to-& ordering

impl<T1, T2, O1, O2> PartialOrd<&mut BitSlice<T2, O2>> for &BitSlice<T1, O1>
where
	T1: BitStore,
	T2: BitStore,
	O1: BitOrder,
	O2: BitOrder,
{
	#[inline]
	fn partial_cmp(&self, rhs: &&mut BitSlice<T2, O2>) -> Option<cmp::Ordering> {
		(**self).partial_cmp(&**rhs)
	}
}

impl<T1, T2, O1, O2> PartialOrd<&BitSlice<T2, O2>> for &mut BitSlice<T1, O1>
where
	T1: BitStore,
	T2: BitStore,
	O1: BitOrder,
	O2: BitOrder,
{
	#[inline]
	fn partial_cmp(&self, rhs: &&BitSlice<T2, O2>) -> Option<cmp::Ordering> {
		(**self).partial_cmp(&**rhs)
	}
}

/** Calls [`BitSlice::try_from_slice`], but returns the original Rust slice on
error instead of the failure event.

This only fails if `slice.len()` exceeds `BitSlice::MAX_ELTS`.

[`BitSlice::try_from_slice`]: crate::slice::BitSlice::try_from_slice
**/
impl<'a, T, O> TryFrom<&'a [T]> for &'a BitSlice<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	type Error = &'a [T];

	#[inline]
	fn try_from(slice: &'a [T]) -> Result<Self, Self::Error> {
		BitSlice::try_from_slice(slice).map_err(|_| slice)
	}
}

/** Calls [`BitSlice::try_from_slice_mut`], but returns the original Rust slice
on error instead of the failure event.

This only fails if `slice.len()` exceeds `BitSlice::MAX_ELTS`.

[`BitSlice::try_from_slice_mut`]: crate::slice::BitSlice::try_from_slice_mut
**/
impl<'a, T, O> TryFrom<&'a mut [T]> for &'a mut BitSlice<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	type Error = &'a mut [T];

	#[inline]
	fn try_from(slice: &'a mut [T]) -> Result<Self, Self::Error> {
		let slice_ptr = slice as *mut [T];
		BitSlice::try_from_slice_mut(slice)
			.map_err(|_| unsafe { &mut *slice_ptr })
	}
}

/// [Original](https://doc.rust-lang.org/std/primitive.slice.html#impl-Default-1)
impl<T, O> Default for &BitSlice<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn default() -> Self {
		BitSlice::empty()
	}
}

/// [Original](https://doc.rust-lang.org/std/primitive.slice.html#impl-Default)
impl<T, O> Default for &mut BitSlice<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn default() -> Self {
		BitSlice::empty_mut()
	}
}

/// [Original](https://doc.rust-lang.org/std/primitive.slice.html#impl-Debug)
impl<T, O> Debug for BitSlice<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		self.as_bitspan().render(fmt, "Slice", None)?;
		fmt.write_str(" ")?;
		Display::fmt(self, fmt)
	}
}

impl<T, O> Display for BitSlice<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		fmt.debug_list()
			.entries(self.iter().by_vals().map(|b| if b { 1 } else { 0 }))
			.finish()
	}
}

impl<T, O> Pointer for BitSlice<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		Pointer::fmt(&self.as_bitspan(), fmt)
	}
}

/// Encodes a short bit-slice into an ASCII b36 value.
#[inline(always)]
fn bits_to_ascii<T, O>(bits: &BitSlice<T, O>, alpha: u8) -> u8
where
	T: BitStore,
	O: BitOrder,
{
	let mut val = 0u8;
	for bit in bits.iter().by_vals() {
		val <<= 1;
		val |= bit as u8;
	}
	match val {
		v @ 0 ..= 9 => b'0' + v,
		v @ 10 ..= 35 => alpha - 10 + v,
		_ => unreachable!(
			"bit-slices wider than five bits cannot be rendered to ASCII b36"
		),
	}
}

/** Encodes an arbitrary bit-slice into an ASCII b36 string.

## Parameters

- `bits`: the bit-slice to encode.
- `into`: a provided buffer into which the bit-slice is encoded.
- `radix`: the bit width of each digit (log2 of its radix).
- `skip`: the number of bytes to skip before beginning the write.
- `alpha`: one of `b'a'` or `b'A'`.

## Returns

A subset of `into` that is now initialized to the ASCII encoding.
**/
#[inline(always)]
fn encode_ascii<'a, T, O>(
	bits: &BitSlice<T, O>,
	into: &'a mut [u8],
	radix: usize,
	mut skip: usize,
	alpha: u8,
) -> &'a str
where
	T: BitStore,
	O: BitOrder,
{
	for (chunk, slot) in
		bits.rchunks(radix).rev().zip(into.iter_mut().skip(skip))
	{
		*slot = bits_to_ascii(chunk, alpha);
		skip += 1;
	}
	unsafe { str::from_utf8_unchecked(&into[.. skip]) }
}

/// Constructs the numeric formatting implementations.
macro_rules! fmt {
	($($trait:ident: $alpha:expr, $pfx:expr, $radix:expr;)+) => { $(
		#[doc = include_str!("../../doc/slice/format.md")]
		impl<T, O> $trait for BitSlice<T, O>
		where
			T: BitStore,
			O: BitOrder,
		{
			#[inline]
			#[allow(clippy::modulo_one)] // I know what I’m doing.
			//  TODO(myrrlyn): See if Binary codegen ditches the loops.
			fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
				const D: usize = mem::bits_of::<usize>() / $radix;
				const M: usize = mem::bits_of::<usize>() % $radix;
				const W: usize = D + (M != 0) as usize;
				let mut txt: [u8; W + 2] = [b'0'; W + 2];
				txt[1] = $pfx;

				let start = if fmt.alternate() { 0 } else { 2 };
				let mut seq = fmt.debug_list();
				match self.domain() {
					Domain::Enclave(elem) => {
						seq.entry(&encode_ascii(
							elem.into_bitslice(),
							&mut txt[start ..],
							$radix,
							2 - start,
							$alpha,
						).fmt_display());
					},
					Domain::Region { head, body, tail } => {
						if let Some(elem) = head {
							seq.entry(&encode_ascii(
								elem.into_bitslice(),
								&mut txt[start ..],
								$radix,
								2 - start,
								$alpha,
							).fmt_display());
						}
						for elem in body.iter().map(BitStore::load_value) {
							seq.entry(&encode_ascii(
								elem.view_bits::<O>(),
								&mut txt[start ..],
								$radix,
								2 - start,
								$alpha,
							).fmt_display());
						}
						if let Some(elem) = tail {
							seq.entry(&encode_ascii(
								elem.into_bitslice(),
								&mut txt[start ..],
								$radix,
								2 - start,
								$alpha,
							).fmt_display());
						}
					},
				}
				seq.finish()
			}
		}
	)+ };
}

fmt! {
	Binary: b'0', b'b', 1;
	Octal: b'0', b'o', 3;
	LowerHex: b'a', b'x', 4;
	UpperHex: b'A', b'x', 4;
}

/// [Original](https://doc.rust-lang.org/std/primitive.slice.html#impl-Hash)
#[cfg(not(tarpaulin_include))]
impl<T, O> Hash for BitSlice<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn hash<H>(&self, hasher: &mut H)
	where H: Hasher {
		self.iter().by_vals().for_each(|bit| bit.hash(hasher));
	}
}

#[doc = include_str!("../../doc/slice/threadsafe.md")]
unsafe impl<T, O> Send for BitSlice<T, O>
where
	T: BitStore + Sync,
	O: BitOrder,
{
}

#[doc = include_str!("../../doc/slice/threadsafe.md")]
unsafe impl<T, O> Sync for BitSlice<T, O>
where
	T: BitStore + Sync,
	O: BitOrder,
{
}

/// [Original](https://doc.rust-lang.org/std/primitive.slice.html#impl-Unpin)
impl<T, O> Unpin for BitSlice<T, O>
where
	T: BitStore,
	O: BitOrder,
{
}

/// [Original](https://doc.rust-lang.org/std/primitive.slice.html#impl-ToOwned)
#[cfg(feature = "alloc")]
#[cfg(not(tarpaulin_include))]
impl<T, O> ToOwned for BitSlice<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	type Owned = BitVec<T, O>;

	#[inline]
	fn to_owned(&self) -> Self::Owned {
		BitVec::from_bitslice(self)
	}
}