bitvec 0.19.6

A crate for manipulating memory, bit by bit
Documentation
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
/*! Typed metadata of registers.

This module provides types which guarantee certain properties about working with
individual bits of registers.

The main advantage of the types in this module is that they provide
type-dependent range constrictions for index values, making it impossible to
have an index out of bounds for a register, and creating a sequence of type
transformations that give assurance about the continued validity of each value
in its surrounding context.

By eliminating public constructors from arbitrary integers, `bitvec` can
guarantee that only it can produce seed values, and only trusted functions can
transform their numeric values or types, until the program reaches the property
it requires. This chain of assurance means that operations that interact with
memory can be confident in the correctness of their actions and effects.

# Type Sequence

The library produces `BitIdx` values from region computation. These types cannot
be publicly constructed, and are only ever the result of pointer analysis. As
such, they rely on correctness of the memory regions provided to library entry
points, and those entry points can leverage the Rust type system to ensure
safety there.

`BitIdx` is transformed to `BitPos` through the `BitOrder` trait, which has an
associated verification function to prove that implementations are correct.
`BitPos` is the only type that can describe memory operations, and is used to
create selection masks of `BitSel` and `BitMask`.
!*/

use crate::{
	mem::BitMemory,
	order::BitOrder,
	store::BitStore,
};

use core::{
	any::type_name,
	fmt::{
		self,
		Binary,
		Debug,
		Display,
		Formatter,
	},
	iter::{
		FusedIterator,
		Sum,
	},
	marker::PhantomData,
	ops::{
		BitAnd,
		BitOr,
		Not,
	},
};

use radium::marker::BitOps;

#[cfg(feature = "serde")]
use core::convert::TryFrom;

macro_rules! make {
	(idx $e:expr) => {
		BitIdx {
			idx: $e,
			_ty: PhantomData,
		}
	};
	(tail $e:expr) => {
		BitTail {
			end: $e,
			_ty: PhantomData,
		}
	};
	(pos $e:expr) => {
		BitPos {
			pos: $e,
			_ty: PhantomData,
		}
	};
	(sel $e:expr) => {
		BitSel { sel: $e }
	};
	(mask $e:expr) => {
		BitMask { mask: $e }
	};
}

/// Marks that an integer can be used in a processor register.
pub trait BitRegister: BitMemory + BitOps + BitStore {}

macro_rules! register {
	($($t:ty),+ $(,)?) => { $(
		impl BitRegister for $t {
		}
	)* };
}

register!(u8, u16, u32);

/** `u64` can only be used as a register on processors whose word size is at
least 64 bits.

This implementation is not present on targets with 32-bit processor words.
**/
#[cfg(target_pointer_width = "64")]
impl BitRegister for u64 {
}

register!(usize);

/** A semantic index of a single bit within a register `R`.

This type is a counter in the range `0 .. R::BITS`, and marks the semantic
position of a bit according to some [`BitOrder`] implementation. As an abstract
counter, it can be used in arithmetic without having to go through `BitOrder`
translation to an electrical position.

# Type Parameters

- `R`: The register type that these values govern.

# Validity

Values of this type are required to be in the range `0 .. R::BITS`. Any value
outside this range will cause the program state to become invalid, and the
library’s behavior is unspecified. The library will never produce such an
invalid value.

# Construction

This type cannot be constructed outside the `bitvec` crate. `bitvec` will
construct safe values of this type, and allows users to view them and use them
to construct other index types from them. All values of this type constructed by
`bitvec` are known to be correct based on user input to the crate.
**/
// #[rustc_layout_scalar_valid_range_end(R::BITS)]
#[repr(transparent)]
#[derive(Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct BitIdx<R>
where R: BitRegister
{
	/// Semantic index counter within a register, constrained to `0 .. R::BITS`.
	idx: u8,
	/// Marker for the indexed type.
	_ty: PhantomData<R>,
}

impl<R> BitIdx<R>
where R: BitRegister
{
	/// The inclusive-maximum index.
	pub(crate) const LAST: Self = make!(idx R::MASK);
	/// The zero index.
	pub(crate) const ZERO: Self = make!(idx 0);

	/// Wraps a counter value as a known-good index into an `R` register.
	///
	/// # Parameters
	///
	/// - `idx`: A semantic index of a bit within an `R` register.
	///
	/// # Returns
	///
	/// If `idx` is outside the valid range `0 .. R::BITS`, this returns `None`;
	/// otherwise, it returns a `BitIdx` wrapping the `idx` value.
	#[inline]
	#[doc(hidden)]
	pub(crate) fn new(idx: u8) -> Option<Self> {
		if idx >= R::BITS {
			return None;
		}
		Some(make!(idx idx))
	}

	/// Wraps a counter value as an assumed-good index into an `R` register.
	///
	/// # Parameters
	///
	/// - `idx`: A semantic index of a bit within an `R` register.
	///
	/// # Returns
	///
	/// `idx` wrapped in a `BitIdx`.
	///
	/// # Safety
	///
	/// `idx` **must** be within the valid range `0 .. R::BITS`. In debug
	/// builds, invalid `idx` values cause a panic; release builds do not check
	/// the input.
	#[inline]
	#[doc(hidden)]
	pub unsafe fn new_unchecked(idx: u8) -> Self {
		debug_assert!(
			idx < R::BITS,
			"Bit index {} cannot exceed type width {}",
			idx,
			R::BITS
		);
		make!(idx idx)
	}

	/// Increments an index counter, wrapping at the back edge of the register.
	///
	/// # Parameters
	///
	/// - `self`: The index to increment.
	///
	/// # Returns
	///
	/// - `.0`: The next index after `self`.
	/// - `.1`: Indicates that the new index is in the next register.
	#[inline]
	pub(crate) fn incr(self) -> (Self, bool) {
		let next = self.idx + 1;
		(make!(idx next & R::MASK), next == R::BITS)
	}

	/// Decrements an index counter, wrapping at the front edge of the register.
	///
	/// # Parameters
	///
	/// - `self`: The inedx to decrement.
	///
	/// # Returns
	///
	/// - `.0`: The previous index before `self`.
	/// - `.1`: Indicates that the new index is in the previous register.
	#[inline]
	pub(crate) fn decr(self) -> (Self, bool) {
		let next = self.idx.wrapping_sub(1);
		(make!(idx next & R::MASK), self.idx == 0)
	}

	/// Computes the bit position corresponding to `self` under some ordering.
	///
	/// This forwards to `O::at::<R>`, and is the only public, safe, constructor
	/// for a position counter.
	#[inline(always)]
	pub fn position<O>(self) -> BitPos<R>
	where O: BitOrder {
		O::at::<R>(self)
	}

	/// Computes the bit selector corresponding to `self` under an ordering.
	///
	/// This forwards to `O::select::<R>`, and is the only public, safe,
	/// constructor for a bit selector.
	#[inline(always)]
	pub fn select<O>(self) -> BitSel<R>
	where O: BitOrder {
		O::select::<R>(self)
	}

	/// Computes the bit selector for `self` as an accessor mask.
	///
	/// This is a type-cast over `Self::select`. It is one of the few public,
	/// safe, constructors of a multi-bit mask.
	#[inline]
	pub fn mask<O>(self) -> BitMask<R>
	where O: BitOrder {
		self.select::<O>().mask()
	}

	/// Views the internal index value.
	#[inline(always)]
	#[cfg(not(tarpaulin_include))]
	pub fn value(self) -> u8 {
		self.idx
	}

	/// Ranges over all possible index values.
	#[inline]
	pub(crate) fn range_all() -> impl Iterator<Item = Self>
	+ DoubleEndedIterator
	+ ExactSizeIterator
	+ FusedIterator {
		(Self::ZERO.idx ..= Self::LAST.idx).map(|val| make!(idx val))
	}

	/// Constructs a range over all indices between a start and end point.
	///
	/// Because implementation details of the `RangeOps` family are not yet
	/// stable, and heterogenous ranges are not supported, this must be an
	/// opaque iterator rather than a direct `Range<BitIdx<R>>`.
	///
	/// # Parameters
	///
	/// - `from`: The inclusive low bound of the range. This will be the first
	///   index produced by the iterator.
	/// - `upto`: The exclusive high bound of the range. The iterator will halt
	///   before yielding an index of this value.
	///
	/// # Returns
	///
	/// An opaque iterator that is equivalent to the range `from .. upto`.
	///
	/// # Requirements
	///
	/// `from` must be no greater than `upto`.
	#[inline]
	pub fn range(
		from: Self,
		upto: BitTail<R>,
	) -> impl Iterator<Item = Self>
	+ DoubleEndedIterator
	+ ExactSizeIterator
	+ FusedIterator
	{
		debug_assert!(
			from.value() <= upto.value(),
			"Ranges must run from low to high"
		);
		(from.value() .. upto.value()).map(|val| make!(idx val))
	}

	/// Computes the the jump distance for a number of bits away from a start.
	///
	/// This produces the number of elements to move from the starting point,
	/// and then the bit index of the destination bit in the destination
	/// element.
	///
	/// # Parameters
	///
	/// - `self`: A bit index in some memory element, used as the starting
	///   position for the offset calculation.
	/// - `by`: The number of bits by which to move. Negative values go towards
	///   the zero bit index and element address; positive values go towards the
	///   maximal bit index and element address.
	///
	/// # Returns
	///
	/// - `.0`: The number of elements by which to offset the caller’s element
	///   address. This value can be passed directly into [`ptr::offset`].
	/// - `.1`: The bit index of the destination bit in the element selected by
	///   applying the `.0` pointer offset.
	///
	/// [`ptr::offset`]: https://doc.rust-lang.org/std/primitive.pointer.html#method.offset
	#[inline]
	pub(crate) fn offset(self, by: isize) -> (isize, Self) {
		let val = self.value();

		/* Signed-add `*self` and the jump distance. Overflowing is the unlikely
		branch. The result is a bit index, and an overflow marker. `far` is
		permitted to be negative; this means that it is lower in memory than the
		origin bit. The number line has its origin at the front edge of the
		origin element, so `-1` is the *last* bit of the prior memory element.
		*/
		let (far, ovf) = by.overflowing_add(val as isize);
		//  If the `isize` addition does not overflow, then the sum can be used
		//  directly.
		if !ovf {
			//  If `far` is in the origin element, then the jump moves zero
			//  elements and produces `far` as an absolute index directly.
			if (0 .. R::BITS as isize).contains(&far) {
				(0, make!(idx far as u8))
			}
			/* Otherwise, downshift the bit distance to compute the number of
			elements moved in either direction, and mask to compute the absolute
			bit index in the destination element.
			*/
			else {
				(far >> R::INDX, make!(idx far as u8 & R::MASK))
			}
		}
		else {
			/* Overflowing `isize` addition happens to produce ordinary `usize`
			addition. In point of fact, `isize` addition and `usize` addition
			are the same machine instruction to perform the sum; it is merely
			the signed interpretation of the sum that differs. The sum can be
			recast back to `usize` without issue.
			*/
			let far = far as usize;
			//  This is really only needed in order to prevent sign-extension of
			//  the downshift; once shifted, the value can be safely re-signed.
			((far >> R::INDX) as isize, make!(idx far as u8 & R::MASK))
		}
	}

	/// Computes the span information for a region beginning at `self` for `len`
	/// bits.
	///
	/// The span information is the number of elements in the region that hold
	/// live bits, and the position of the tail marker after the live bits.
	///
	/// This forwards to [`BitTail::span`], as the computation is identical for
	/// the two types. Beginning a span at any `Idx` is equivalent to beginning
	/// it at the tail of a previous span.
	///
	/// # Parameters
	///
	/// - `self`: The start bit of the span.
	/// - `len`: The number of bits in the span.
	///
	/// # Returns
	///
	/// - `.0`: The number of elements, starting in the element that contains
	///   `self`, that contain live bits of the span.
	/// - `.1`: The tail counter of the span’s end point.
	#[inline]
	pub(crate) fn span(self, len: usize) -> (usize, BitTail<R>) {
		make!(tail self.value()).span(len)
	}
}

#[cfg(not(tarpaulin_include))]
impl<R> Binary for BitIdx<R>
where R: BitRegister
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		write!(fmt, "{:0>1$b}", self.idx, R::INDX as usize)
	}
}

#[cfg(not(tarpaulin_include))]
impl<R> Debug for BitIdx<R>
where R: BitRegister
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		write!(fmt, "BitIdx<{}>(", type_name::<R>())?;
		Display::fmt(&self.idx, fmt)?;
		fmt.write_str(")")
	}
}

#[cfg(not(tarpaulin_include))]
impl<R> Display for BitIdx<R>
where R: BitRegister
{
	#[inline(always)]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		Display::fmt(&self.idx, fmt)
	}
}

/// Represents an error encountered in `TryFrom<u8> for BitIdx<R>`.
#[repr(transparent)]
#[cfg(feature = "serde")]
pub struct BitIdxErr<R>
where R: BitRegister
{
	/// The value that cannot be wrapped into a `BitIdx`.
	err: u8,
	/// The register type marker.
	_ty: PhantomData<R>,
}

#[cfg(feature = "serde")]
impl<R> TryFrom<u8> for BitIdx<R>
where R: BitRegister
{
	type Error = BitIdxErr<R>;

	#[inline]
	fn try_from(idx: u8) -> Result<Self, Self::Error> {
		Self::new(idx).ok_or(BitIdxErr {
			err: idx,
			_ty: PhantomData,
		})
	}
}

#[cfg(feature = "serde")]
#[cfg(not(tarpaulin_include))]
impl<R> Debug for BitIdxErr<R>
where R: BitRegister
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		write!(fmt, "BitIdxErr<{}>(", type_name::<R>())?;
		Display::fmt(&self.err, fmt)?;
		fmt.write_str(")")
	}
}

#[cfg(feature = "serde")]
#[cfg(not(tarpaulin_include))]
impl<R> Display for BitIdxErr<R>
where R: BitRegister
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		write!(
			fmt,
			"The value {} is too large to index into {}",
			self.err,
			core::any::type_name::<R>()
		)
	}
}

#[cfg(all(feature = "serde", feature = "std"))]
#[cfg(not(tarpaulin_include))]
impl<R> std::error::Error for BitIdxErr<R> where R: BitRegister
{
}

/** Semantic index of a dead bit *after* a live region.

Like `BitIdx<R>`, this type indicates a semantic counter within a register `R`.
However, it marks the position of a *dead* bit *after* a live range. This means
that it is permitted to have the value of `R::BITS`, to indicate that a live
region touches the semantic back edge of the register `R`.

Instances of this type will only contain the value `0` when the span that
created them is empty. Otherwise, they will have the range `1 ..= R::BITS`.

This type cannot be used for indexing into a register `R`, and does not
translate to a `BitPos<R>`. It has no behavior other than viewing its internal
counter for region arithmetic.

# Type Parameters

- `R`: The register type that these values govern.

# Validity

Values of this type are required to be in the range `0 ..= R::BITS`. Any value
outside this range will cause the program state to become invalid, and the
library’s behavior is unspecified. The library will never produce such an
invalid value.

# Construction

This type cannot be directly constructed outside the `bitvec` crate. `bitvec`
will construct safe values of this type, and allows users to view them and use
them for region computation. All values of this type constructed by `bitvec` are
known to be correct based on user input to the crate.
**/
// #[rustc_layout_scalar_valid_range_end(R::BITS + 1)]
#[repr(transparent)]
#[derive(Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct BitTail<R>
where R: BitRegister
{
	/// Semantic tail counter of a register, constrained to `0 ..= R::BITS`.
	end: u8,
	/// Marker for the tailed type.
	_ty: PhantomData<R>,
}

impl<R> BitTail<R>
where R: BitRegister
{
	/// The inclusive-maximum tail counter.
	pub(crate) const END: Self = make!(tail R::BITS);
	/// The zero tail.
	pub(crate) const ZERO: Self = make!(tail 0);

	/// Wraps a counter value as an assumed-good tail of an `R` register.
	///
	/// # Parameters
	///
	/// - `end`: A semantic index of a dead bit in or after an `R` register.
	///
	/// # Returns
	///
	/// `end` wrapped in a `BitTail`.
	///
	/// # Safety
	///
	/// `end` **must** be within the valid range `0 ..= R::BITS`. In debug
	/// builds, invalid `end` values cause a panic; release builds do not check
	/// the input.
	#[inline]
	pub(crate) unsafe fn new_unchecked(end: u8) -> Self {
		debug_assert!(
			end <= R::BITS,
			"Bit tail {} cannot exceed type width {}",
			end,
			R::BITS
		);
		make!(tail end)
	}

	/// Views the internal tail value.
	#[inline]
	#[cfg(not(tarpaulin_include))]
	pub fn value(self) -> u8 {
		self.end
	}

	/// Ranges over all valid tails for a starting index.
	#[inline]
	#[cfg(test)]
	pub(crate) fn range_from(
		start: BitIdx<R>,
	) -> impl Iterator<Item = Self>
	+ DoubleEndedIterator
	+ ExactSizeIterator
	+ FusedIterator {
		(start.idx ..= Self::END.end).map(|val| make!(tail val))
	}

	/// Computes span information for a region beginning immediately after a
	/// preceding region.
	///
	/// The computed region of `len` bits has its start at the *live* bit that
	/// corresponds to the `self` dead tail. The return value is the number of
	/// memory elements containing live bits of the computed span and its tail
	/// marker.
	///
	/// # Parameters
	///
	/// - `self`
	/// - `len`: The number of live bits in the span starting after `self`.
	///
	/// # Returns
	///
	/// - `.0`: The number of elements `R` that contain live bits in the
	///   computed region.
	/// - `.1`: The tail counter of the first dead bit after the new span.
	///
	/// # Behavior
	///
	/// If `len` is `0`, this returns `(0, self)`, as the span has no live bits.
	/// If `self` is `BitTail::END`, then the new region starts at
	/// `BitIdx::ZERO` in the next element.
	pub(crate) fn span(self, len: usize) -> (usize, Self) {
		if len == 0 {
			return (0, self);
		}

		let val = self.end;

		let head = val & R::MASK;
		let bits_in_head = (R::BITS - head) as usize;

		if len <= bits_in_head {
			return (1, make!(tail head + len as u8));
		}

		let bits_after_head = len - bits_in_head;
		let elts = bits_after_head >> R::INDX;
		let tail = bits_after_head as u8 & R::MASK;

		let is_zero = (tail == 0) as u8;
		let edges = 2 - is_zero as usize;
		(elts + edges, make!(tail(is_zero << R::INDX) | tail))
	}
}

#[cfg(not(tarpaulin_include))]
impl<R> Debug for BitTail<R>
where R: BitRegister
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		write!(fmt, "BitTail<{}>(", type_name::<R>())?;
		Display::fmt(&self.end, fmt)?;
		fmt.write_str(")")
	}
}

#[cfg(not(tarpaulin_include))]
impl<R> Display for BitTail<R>
where R: BitRegister
{
	#[inline(always)]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		Display::fmt(&self.end, fmt)
	}
}

/** An electrical position of a single bit within a register `R`.

This type is used as the shift distance in the expression `1 << shamt`. It is
only produced by the translation of a semantic `BitIdx<R>` according to some
[`BitOrder`] implementation using `BitOrder::at`. It can only be used for the
construction of bit masks used to manipulate a register value during memory
access, and serves no other purpose.

# Type Parameters

- `R`: The register type that these values govern.

# Validity

Values of this type are required to be in the range `0 .. R::BITS`. Any value
outside this range will cause the program state to become invalid, and the
library’s behavior is unspecified. The library will never produce such an
invalid value, and users are required to do the same.

# Construction

This type offers public unsafe constructors. `bitvec` does not offer any public
APIs that take values of this type directly; it always routes through `BitOrder`
implementations. As `BitIdx` will only be constructed from safe, correct,
values, and `BitOrder::at` is the only `BitIdx -> BitPos` transform function,
all constructed `BitPos` values are known to be memory-correct.
**/
// #[rustc_layout_scalar_valid_range_end(R::BITS)]
#[repr(transparent)]
#[derive(Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct BitPos<R>
where R: BitRegister
{
	/// Electrical position within a register, constrained to `0 .. R::BITS`.
	pos: u8,
	/// Marker for the positioned type.
	_ty: PhantomData<R>,
}

impl<R> BitPos<R>
where R: BitRegister
{
	/// Wraps a value as a known-good position within an `R` register.
	///
	/// # Parameters
	///
	/// - `pos`: An electrical position of a bit within an `R` register.
	///
	/// # Returns
	///
	/// If `pos` is outside the valid range `0 .. R::BITS`, this returns `None`;
	/// otherwise, it returns a `BitPos` wrapping the `pos` value.
	///
	/// # Safety
	///
	/// This function must only be called within a `BitOrder::at` implementation
	/// which is verified to be correct.
	#[inline]
	pub unsafe fn new(pos: u8) -> Option<Self> {
		//  Reject a position value that is not within the range `0 .. R::BITS`.
		if pos >= R::BITS {
			return None;
		}
		Some(make!(pos pos))
	}

	/// Wraps a value as an assumed-good position within an `R` register.
	///
	/// # Parameters
	///
	/// - `pos`: An electrical position within an `R` register.
	///
	/// # Returns
	///
	/// `pos` wrapped in a `BitPos`.
	///
	/// # Safety
	///
	/// `pos` **must** be within the valid range `0 .. R::BITS`. In debug
	/// builds, invalid `pos` values cause a panic; release builds do not check
	/// the input.
	///
	/// This function must only be called in a correct `BitOrder::at`
	/// implementation.
	#[inline]
	pub unsafe fn new_unchecked(pos: u8) -> Self {
		debug_assert!(
			pos < R::BITS,
			"Bit position {} cannot exceed type width {}",
			pos,
			R::BITS
		);
		make!(pos pos)
	}

	/// Constructs a one-hot selection mask from the position counter.
	///
	/// This is a well-typed `1 << pos`.
	///
	/// # Parameters
	///
	/// - `self`
	///
	/// # Returns
	///
	/// A one-hot mask for `R` selecting the bit specified by `self`.
	#[inline]
	pub fn select(self) -> BitSel<R> {
		make!(sel R::ONE << self.pos)
	}

	/// Constructs an untyped bitmask from the position counter.
	///
	/// This removes the one-hot requirement from the selection mask.
	///
	/// # Parameters
	///
	/// - `self`
	///
	/// # Returns
	///
	/// A mask for `R` selecting only the bit specified by `self`.
	#[inline]
	pub fn mask(self) -> BitMask<R> {
		make!(mask self.select().sel)
	}

	/// Views the internal position value.
	#[inline]
	pub fn value(self) -> u8 {
		self.pos
	}
}

#[cfg(not(tarpaulin_include))]
impl<R> Debug for BitPos<R>
where R: BitRegister
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		write!(fmt, "BitPos<{}>(", type_name::<R>())?;
		Display::fmt(&self.pos, fmt)?;
		fmt.write_str(")")
	}
}

#[cfg(not(tarpaulin_include))]
impl<R> Display for BitPos<R>
where R: BitRegister
{
	#[inline(always)]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		Display::fmt(&self.pos, fmt)
	}
}

/** A one-hot selection mask, to be applied to a register `R`.

This type selects exactly one bit, and is produced by the conversion of a
semantic [`BitIdx`] to a [`BitPos`] through a [`BitOrder`] implementation, and
then applying `1 << pos`. Values of this type are used to select only the bit
specified by a `BitIdx` when performing memory operations.

# Type Parameters

- `R`: The register type that these values govern.

# Validity

Values of this type are required to have exactly one bit set to `1` and all
other bits set to `0`.

# Construction

This type is only constructed from `BitPos` values, which are themselves only
constructed by a chain of known-good `BitIdx` values passed into known-correct
`BitOrder` implementations. As such, `bitvec` can use `BitSel` values with full
confidence that they are correct in the surrounding context.
**/
#[repr(transparent)]
#[derive(Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct BitSel<R>
where R: BitRegister
{
	/// The one-hot selector mask.
	sel: R,
}

impl<R> BitSel<R>
where R: BitRegister
{
	/// Wraps a selector value as a known-good selection of an `R` register.
	///
	/// # Parameters
	///
	/// - `sel`: A one-hot selection mask of a bit in an `R` register.
	///
	/// # Returns
	///
	/// If `sel` does not have exactly one bit set, this returns `None`;
	/// otherwise, it returns a `BitSel` wrapping the `sel` value.
	///
	/// # Safety
	///
	/// This function must only be called within a `BitOrder::select`
	/// implementation that is verified to be correct.
	#[inline]
	pub unsafe fn new(sel: R) -> Option<Self> {
		if sel.count_ones() != 1 {
			return None;
		}
		Some(make!(sel sel))
	}

	/// Wraps a selector value as an assumed-good selection of an `R` register.
	///
	/// # Parameters
	///
	/// - `sel`: A one-hot selection mask of a bit in an `R` register.
	///
	/// # Returns
	///
	/// `sel` wrapped in a `BitSel`.
	///
	/// # Safety
	///
	/// `sel` **must** have exactly one bit set high and all others low. In
	/// debug builds, invalid `sel` values cause a panic; release builds do not
	/// check the input.
	///
	/// This function must only be called in a correct `BitOrder::select`
	/// implementation.
	#[inline]
	pub unsafe fn new_unchecked(sel: R) -> Self {
		debug_assert!(
			sel.count_ones() == 1,
			"Selections are required to have exactly one set bit: {:0>1$b}",
			sel,
			R::BITS as usize
		);
		make!(sel sel)
	}

	/// Converts the selector into a bit mask.
	///
	/// This is a type-cast.
	#[inline]
	pub fn mask(self) -> BitMask<R>
	where R: BitRegister {
		make!(mask self.sel)
	}

	/// Views the internal selector value.
	#[inline]
	pub fn value(self) -> R {
		self.sel
	}

	/// Ranges over all possible selector values.
	pub fn range_all() -> impl Iterator<Item = Self>
	+ DoubleEndedIterator
	+ ExactSizeIterator
	+ FusedIterator {
		BitIdx::<R>::range_all().map(|i| make!(pos i.idx).select())
	}
}

#[cfg(not(tarpaulin_include))]
impl<R> Binary for BitSel<R>
where R: BitRegister
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		write!(fmt, "{:0>1$b}", self.sel, R::BITS as usize)
	}
}

#[cfg(not(tarpaulin_include))]
impl<R> Debug for BitSel<R>
where R: BitRegister
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		write!(fmt, "BitSel<{}>(", type_name::<R>())?;
		Binary::fmt(&self, fmt)?;
		fmt.write_str(")")
	}
}

#[cfg(not(tarpaulin_include))]
impl<R> Display for BitSel<R>
where R: BitRegister
{
	#[inline(always)]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		Display::fmt(&self.sel, fmt)
	}
}

/** A multi-bit selection mask.

Unlike [`BitSel`], which enforces a strict one-hot mask encoding, this mask type
permits any number of bits to be set or unset. This is used to accumulate
selections for a batch operation on a register.

# Construction

It is only constructed by accumulating `BitSel` values. The chain of custody for
safe construction in this module and in `order` ensures that all masks that are
applied to register values can be trusted to not cause memory unsafety.
**/
#[repr(transparent)]
#[derive(Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct BitMask<R>
where R: BitRegister
{
	/// A mask of any number of bits to select.
	mask: R,
}

impl<R> BitMask<R>
where R: BitRegister
{
	/// A full mask.
	pub const ALL: Self = make!(mask R::ALL);
	/// An empty mask.
	pub const ZERO: Self = make!(mask R::ZERO);

	/// Wraps any `R` value as a bit-mask.
	///
	/// This constructor is provided to explicitly declare that an operation is
	/// discarding the numeric value of an integer and reading it only as a
	/// bit-mask.
	///
	/// # Parameters
	///
	/// - `mask`: Some integer value
	///
	/// # Returns
	///
	/// `mask` wrapped as a bit-mask, with its numeric context discarded.
	///
	/// # Safety
	///
	/// This function must only be called within a `BitOrder::mask`
	/// implementation which is verified to be correct.
	///
	/// Prefer accumulating `BitSel` values using the `Sum` implementation.
	#[inline]
	pub unsafe fn new(mask: R) -> Self {
		make!(mask mask)
	}

	/// Creates a new mask with a selector bit activated.
	///
	/// # Parameters
	///
	/// - `self`
	/// - `sel`: The selector bit to activate in the new mask.
	///
	/// # Returns
	///
	/// A copy of `self`, with the selector at `sel` activated.
	#[inline]
	pub fn combine(mut self, sel: BitSel<R>) -> Self {
		self.insert(sel);
		self
	}

	/// Inserts a selector into an existing mask.
	///
	/// # Parameters
	///
	/// - `&mut self`
	/// - `sel`: The selector bit to insert into the mask.
	///
	/// # Effects
	///
	/// The selector’s bit in the `self` mask is activated.
	#[inline]
	pub fn insert(&mut self, sel: BitSel<R>) {
		self.mask |= sel.sel;
	}

	/// Tests whether a mask contains a given selector bit.
	///
	/// # Paramters
	///
	/// - `self`
	/// - `sel`: The selector bit to test in the `self` mask.
	///
	/// # Returns
	///
	/// Whether `self` has set the bit at `sel`.
	#[inline]
	pub fn test(self, sel: BitSel<R>) -> bool {
		self.mask & sel.sel != R::ZERO
	}

	/// Views the internal mask value.
	#[inline]
	pub fn value(self) -> R {
		self.mask
	}
}

#[cfg(not(tarpaulin_include))]
impl<R> Binary for BitMask<R>
where R: BitRegister
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		write!(fmt, "{:0>1$b}", self.mask, R::BITS as usize)
	}
}

#[cfg(not(tarpaulin_include))]
impl<R> Debug for BitMask<R>
where R: BitRegister
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		write!(fmt, "BitMask<{}>(", type_name::<R>())?;
		Binary::fmt(&self, fmt)?;
		fmt.write_str(")")
	}
}

#[cfg(not(tarpaulin_include))]
impl<R> Display for BitMask<R>
where R: BitRegister
{
	#[inline(always)]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		Display::fmt(&self.mask, fmt)
	}
}

impl<R> Sum<BitSel<R>> for BitMask<R>
where R: BitRegister
{
	fn sum<I>(iter: I) -> Self
	where I: Iterator<Item = BitSel<R>> {
		iter.fold(Self::ZERO, Self::combine)
	}
}

impl<R> BitAnd<R> for BitMask<R>
where R: BitRegister
{
	type Output = Self;

	fn bitand(self, rhs: R) -> Self {
		make!(mask self.mask & rhs)
	}
}

impl<R> BitOr<R> for BitMask<R>
where R: BitRegister
{
	type Output = Self;

	fn bitor(self, rhs: R) -> Self {
		make!(mask self.mask | rhs)
	}
}

impl<R> Not for BitMask<R>
where R: BitRegister
{
	type Output = Self;

	fn not(self) -> Self::Output {
		make!(mask !self.mask)
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::order::{
		Lsb0,
		Msb0,
	};

	#[test]
	fn index_fns() {
		assert!(BitIdx::<u8>::new(8).is_none());

		for n in 0 .. 8 {
			assert_eq!(
				BitIdx::<u8>::new(n).unwrap().position::<Lsb0>().value(),
				n
			);
		}

		for n in 0 .. 8 {
			assert_eq!(
				BitIdx::<u8>::new(n).unwrap().position::<Msb0>().value(),
				7 - n
			);
		}

		for n in 0 .. 8 {
			assert_eq!(
				BitIdx::<u8>::new(n).unwrap().mask::<Lsb0>().value(),
				1 << n
			);
		}

		for n in 0 .. 8 {
			assert_eq!(
				BitIdx::<u8>::new(n).unwrap().mask::<Msb0>().value(),
				128 >> n
			);
		}

		for n in 0 .. 8 {
			assert_eq!(BitIdx::<u8>::new(n).unwrap().value(), n);
		}
	}

	#[test]
	fn tail_fns() {
		for n in 0 .. 8 {
			let tail: BitTail<u8> = make!(tail n);
			assert_eq!(tail.value(), n);
		}
	}

	#[test]
	fn position_fns() {
		assert!(unsafe { BitPos::<u8>::new(8) }.is_none());

		for n in 0 .. 8 {
			let pos: BitPos<u8> = make!(pos n);
			let mask: BitMask<u8> = make!(mask 1 << n);
			assert_eq!(pos.mask(), mask);
		}
	}

	#[test]
	fn select_fns() {
		assert!(unsafe { BitSel::<u8>::new(1) }.is_some());
		assert!(unsafe { BitSel::<u8>::new(3) }.is_none());

		for (n, sel) in BitSel::<u8>::range_all().enumerate() {
			assert_eq!(sel, make!(sel(1 << n) as u8));
		}
	}

	#[test]
	fn fold_masks() {
		assert_eq!(
			BitSel::<u8>::range_all()
				.map(BitSel::mask)
				.fold(BitMask::<u8>::ZERO, |accum, mask| accum | mask.value()),
			BitMask::<u8>::ALL
		);

		assert_eq!(!BitMask::<u8>::ALL, BitMask::ZERO);
	}

	#[test]
	fn offset() {
		let (elts, idx) =
			BitIdx::<u32>::new(31).unwrap().offset(isize::max_value());
		assert_eq!(elts, (isize::max_value() >> 5) + 1);
		assert_eq!(idx, BitIdx::new(30).unwrap());
	}

	#[test]
	fn span() {
		let start: BitTail<u8> = make!(tail 4);
		assert_eq!(start.span(0), (0, start));

		assert_eq!(start.span(4), (1, make!(tail 8)));
		assert_eq!(start.span(8), (2, start));
	}

	#[test]
	fn walk() {
		let end: BitIdx<u8> = make!(idx 7);
		assert_eq!(end.incr(), (make!(idx 0), true));
		assert_eq!(end.decr(), (make!(idx 6), false));
	}
}