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
#![doc = include_str!("../doc/domain.md")]

use core::{
	any,
	convert::{
		TryFrom,
		TryInto,
	},
	fmt::{
		self,
		Binary,
		Debug,
		Display,
		Formatter,
		LowerHex,
		Octal,
		UpperHex,
	},
	hash::{
		Hash,
		Hasher,
	},
	iter::FusedIterator,
	marker::PhantomData,
};

use tap::{
	Conv,
	Pipe,
	Tap,
};
use wyz::{
	comu::{
		Address,
		Const,
		Mut,
		Mutability,
		Reference,
		Referential,
		SliceReferential,
	},
	fmt::FmtForward,
};

use crate::{
	access::BitAccess,
	index::{
		BitEnd,
		BitIdx,
		BitMask,
	},
	order::{
		BitOrder,
		Lsb0,
	},
	ptr::BitSpan,
	slice::BitSlice,
	store::BitStore,
};

#[doc = include_str!("../doc/domain/BitDomain.md")]
pub enum BitDomain<'a, M = Const, T = usize, O = Lsb0>
where
	M: Mutability,
	T: 'a + BitStore,
	O: BitOrder,
	Address<M, BitSlice<T, O>>: Referential<'a>,
	Address<M, BitSlice<T::Unalias, O>>: Referential<'a>,
{
	/// Indicates that a bit-slice’s contents are entirely in the interior
	/// indices of a single memory element.
	///
	/// The contained value is always the bit-slice that created this view.
	Enclave(Reference<'a, M, BitSlice<T, O>>),
	/// Indicates that a bit-slice’s contents touch an element edge.
	///
	/// This splits the bit-slice into three partitions, each of which may be
	/// empty: two partially-occupied edge elements, with their original type
	/// status, and one interior span, which is known to not have any other
	/// aliases derived from the bit-slice that created this view.
	Region {
		/// Any bits that partially-fill the first element of the underlying
		/// storage region.
		///
		/// This does not modify its aliasing status, as it will already be
		/// appropriately marked before this view is constructed.
		head: Reference<'a, M, BitSlice<T, O>>,
		/// Any bits that wholly-fill elements in the interior of the bit-slice.
		///
		/// This is marked as unaliased, because it is statically impossible for
		/// any other handle derived from the source bit-slice to have
		/// conflicting access to the region of memory it describes. As such,
		/// even a bit-slice that was marked as `::Alias` can revert this
		/// protection on the known-unaliased interior.
		///
		/// Proofs:
		///
		/// - Rust’s `&`/`&mut` exclusion rules universally apply. If a
		///   reference exists, no other reference has unsynchronized write
		///   capability.
		/// - `BitStore::Unalias` only modifies unsynchronized types. `Cell` and
		///   atomic types unalias to themselves, and retain their original
		///   behavior.
		body: Reference<'a, M, BitSlice<T::Unalias, O>>,
		/// Any bits that partially-fill the last element of the underlying
		/// storage region.
		///
		/// This does not modify its aliasing status, as it will already be
		/// appropriately marked before this view is constructed.
		tail: Reference<'a, M, BitSlice<T, O>>,
	},
}

impl<'a, M, T, O> BitDomain<'a, M, T, O>
where
	M: Mutability,
	T: 'a + BitStore,
	O: BitOrder,
	Address<M, BitSlice<T, O>>: Referential<'a>,
	Address<M, BitSlice<T::Unalias, O>>: Referential<'a>,
{
	/// Attempts to unpack the bit-domain as an [`Enclave`] variant. This is
	/// just a shorthand for explicit destructuring.
	///
	/// [`Enclave`]: Self::Enclave
	#[inline]
	pub fn enclave(self) -> Option<Reference<'a, M, BitSlice<T, O>>> {
		match self {
			Self::Enclave(bits) => Some(bits),
			_ => None,
		}
	}

	/// Attempts to unpack the bit-domain as a [`Region`] variant. This is just
	/// a shorthand for explicit destructuring.
	///
	/// [`Region`]: Self::Region
	#[inline]
	pub fn region(
		self,
	) -> Option<(
		Reference<'a, M, BitSlice<T, O>>,
		Reference<'a, M, BitSlice<T::Unalias, O>>,
		Reference<'a, M, BitSlice<T, O>>,
	)> {
		match self {
			Self::Region { head, body, tail } => Some((head, body, tail)),
			_ => None,
		}
	}
}

impl<'a, M, T, O> Default for BitDomain<'a, M, T, O>
where
	M: Mutability,
	T: 'a + BitStore,
	O: BitOrder,
	Address<M, BitSlice<T, O>>: Referential<'a>,
	Address<M, BitSlice<T::Unalias, O>>: Referential<'a>,
	Reference<'a, M, BitSlice<T, O>>: Default,
	Reference<'a, M, BitSlice<T::Unalias, O>>: Default,
{
	#[inline]
	fn default() -> Self {
		Self::Region {
			head: Default::default(),
			body: Default::default(),
			tail: Default::default(),
		}
	}
}

impl<'a, M, T, O> Debug for BitDomain<'a, M, T, O>
where
	M: Mutability,
	T: 'a + BitStore,
	O: BitOrder,
	Address<M, BitSlice<T, O>>: Referential<'a>,
	Address<M, BitSlice<T::Unalias, O>>: Referential<'a>,
	Reference<'a, M, BitSlice<T, O>>: Debug,
	Reference<'a, M, BitSlice<T::Unalias, O>>: Debug,
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		write!(
			fmt,
			"BitDomain::<{} {}, {}>::",
			M::RENDER,
			any::type_name::<T::Mem>(),
			any::type_name::<O>(),
		)?;
		match self {
			Self::Enclave(elem) => {
				fmt.debug_tuple("Enclave").field(elem).finish()
			},
			Self::Region { head, body, tail } => fmt
				.debug_struct("Region")
				.field("head", head)
				.field("body", body)
				.field("tail", tail)
				.finish(),
		}
	}
}

#[cfg(not(tarpaulin_include))]
impl<T, O> Clone for BitDomain<'_, Const, T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn clone(&self) -> Self {
		*self
	}
}

impl<T, O> Copy for BitDomain<'_, Const, T, O>
where
	T: BitStore,
	O: BitOrder,
{
}

#[doc = include_str!("../doc/domain/Domain.md")]
pub enum Domain<'a, M = Const, T = usize, O = Lsb0>
where
	M: Mutability,
	T: 'a + BitStore,
	O: BitOrder,
	Address<M, T>: Referential<'a>,
	Address<M, [T::Unalias]>: SliceReferential<'a>,
{
	/// Indicates that a bit-slice’s contents are entirely in the interior
	/// indices of a single memory element.
	///
	/// The contained reference is only able to observe the bits governed by the
	/// generating bit-slice. Other handles to the element may exist, and may
	/// write to bits outside the range that this reference can observe.
	Enclave(PartialElement<'a, M, T, O>),
	/// Indicates that a bit-slice’s contents touch an element edge.
	///
	/// This splits the bit-slice into three partitions, each of which may be
	/// empty: two partially-occupied edge elements, with their original type
	/// status, and one interior span, which is known not to have any other
	/// aliases derived from the bit-slice that created this view.
	Region {
		/// The first element in the bit-slice’s underlying storage, if it is
		/// only partially used.
		head: Option<PartialElement<'a, M, T, O>>,
		/// All fully-used elements in the bit-slice’s underlying storage.
		///
		/// This is marked as unaliased, because it is statically impossible for
		/// any other handle derived from the source bit-slice to have
		/// conflicting access to the region of memory it describes. As such,
		/// even a bit-slice that was marked as `::Alias` can revert this
		/// protection on the known-unaliased interior.
		body: Reference<'a, M, [T::Unalias]>,
		/// The last element in the bit-slice’s underlying storage, if it is
		/// only partially used.
		tail: Option<PartialElement<'a, M, T, O>>,
	},
}

impl<'a, M, T, O> Domain<'a, M, T, O>
where
	M: Mutability,
	T: 'a + BitStore,
	O: BitOrder,
	Address<M, T>: Referential<'a>,
	Address<M, [T::Unalias]>: SliceReferential<'a>,
{
	/// Attempts to unpack the bit-domain as an [`Enclave`] variant. This is
	/// just a shorthand for explicit destructuring.
	///
	/// [`Enclave`]: Self::Enclave
	#[inline]
	pub fn enclave(self) -> Option<PartialElement<'a, M, T, O>> {
		match self {
			Self::Enclave(elem) => Some(elem),
			_ => None,
		}
	}

	/// Attempts to unpack the bit-domain as a [`Region`] variant. This is just
	/// a shorthand for explicit destructuring.
	///
	/// [`Region`]: Self::Region
	#[inline]
	pub fn region(
		self,
	) -> Option<(
		Option<PartialElement<'a, M, T, O>>,
		Reference<'a, M, [T::Unalias]>,
		Option<PartialElement<'a, M, T, O>>,
	)> {
		match self {
			Self::Region { head, body, tail } => Some((head, body, tail)),
			_ => None,
		}
	}

	/// Converts the element-wise `Domain` into the equivalent `BitDomain`.
	///
	/// This transform replaces each memory reference with an equivalent
	/// `BitSlice` reference.
	#[inline]
	pub fn into_bit_domain(self) -> BitDomain<'a, M, T, O>
	where
		Address<M, BitSlice<T, O>>: Referential<'a>,
		Address<M, BitSlice<T::Unalias, O>>: Referential<'a>,
		Reference<'a, M, BitSlice<T, O>>: Default,
		Reference<'a, M, BitSlice<T::Unalias, O>>:
			TryFrom<Reference<'a, M, [T::Unalias]>>,
	{
		match self {
			Self::Enclave(elem) => BitDomain::Enclave(elem.into_bitslice()),
			Self::Region { head, body, tail } => BitDomain::Region {
				head: head.map_or_else(
					Default::default,
					PartialElement::into_bitslice,
				),
				body: body.try_into().unwrap_or_else(|_| {
					match option_env!("CARGO_PKG_REPOSITORY") {
						Some(env) => unreachable!(
							"Construction of a slice with length {} should not \
							 be possible. If this assumption is outdated, \
							 please file an issue at {}",
							(isize::MIN as usize) >> 3,
							env,
						),
						None => unreachable!(
							"Construction of a slice with length {} should not \
							 be possible. If this assumption is outdated, \
							 please consider filing an issue",
							(isize::MIN as usize) >> 3
						),
					}
				}),
				tail: tail.map_or_else(
					Default::default,
					PartialElement::into_bitslice,
				),
			},
		}
	}
}

/** Domain constructors.

Only `Domain<Const>` and `Domain<Mut>` are ever constructed, and they of course
are only constructed from `&BitSlice` and `&mut BitSlice`, respectively.

However, the Rust trait system does not have a way to express a closed set, so
**/
impl<'a, M, T, O> Domain<'a, M, T, O>
where
	M: Mutability,
	T: 'a + BitStore,
	O: BitOrder,
	Address<M, T>: Referential<'a>,
	Address<M, [T::Unalias]>:
		SliceReferential<'a, ElementAddr = Address<M, T::Unalias>>,
	Address<M, BitSlice<T, O>>: Referential<'a>,
	Reference<'a, M, [T::Unalias]>: Default,
{
	/// Creates a new `Domain` over a bit-slice.
	///
	/// ## Parameters
	///
	/// - `bits`: Either a `&BitSlice` or `&mut BitSlice` reference, depending
	///   on whether a `Domain<Const>` or `Domain<Mut>` is being produced.
	///
	/// ## Returns
	///
	/// A `Domain` description of the raw memory governed by `bits`.
	pub(crate) fn new(bits: Reference<'a, M, BitSlice<T, O>>) -> Self
	where BitSpan<M, T, O>: From<Reference<'a, M, BitSlice<T, O>>> {
		let bitspan = bits.conv::<BitSpan<M, T, O>>();
		let (head, elts, tail) =
			(bitspan.head(), bitspan.elements(), bitspan.tail());
		let base = bitspan.address();
		let (min, max) = (BitIdx::<T::Mem>::MIN, BitEnd::<T::Mem>::MAX);
		let ctor = match (head, elts, tail) {
			(_, 0, _) => Self::empty,
			(h, _, t) if h == min && t == max => Self::spanning,
			(_, _, t) if t == max => Self::partial_head,
			(h, ..) if h == min => Self::partial_tail,
			(_, 1, _) => Self::minor,
			_ => Self::major,
		};
		ctor(base, elts, head, tail)
	}

	/// Produces the canonical empty `Domain`.
	#[inline]
	fn empty(
		_: Address<M, T>,
		_: usize,
		_: BitIdx<T::Mem>,
		_: BitEnd<T::Mem>,
	) -> Self {
		Default::default()
	}

	/// Produces a `Domain::Region` that contains both `head` and `tail` partial
	/// elements as well as a `body` slice (which may be empty).
	#[inline]
	fn major(
		addr: Address<M, T>,
		elts: usize,
		head: BitIdx<T::Mem>,
		tail: BitEnd<T::Mem>,
	) -> Self {
		let h_elem = addr;
		let t_elem = unsafe { addr.add(elts - 1) };
		let body = unsafe {
			Address::<M, [T::Unalias]>::from_raw_parts(
				addr.add(1).cast::<T::Unalias>(),
				elts - 2,
			)
		};
		Self::Region {
			head: Some(PartialElement::new(h_elem, head, None)),
			body,
			tail: Some(PartialElement::new(t_elem, None, tail)),
		}
	}

	/// Produces a `Domain::Enclave`.
	#[inline]
	fn minor(
		addr: Address<M, T>,
		_: usize,
		head: BitIdx<T::Mem>,
		tail: BitEnd<T::Mem>,
	) -> Self {
		let elem = addr;
		Self::Enclave(PartialElement::new(elem, head, tail))
	}

	/// Produces a `Domain::Region` with a partial `head` and a `body`, but no
	/// `tail`.
	#[inline]
	fn partial_head(
		addr: Address<M, T>,
		elts: usize,
		head: BitIdx<T::Mem>,
		_: BitEnd<T::Mem>,
	) -> Self {
		let elem = addr;
		let body = unsafe {
			Address::<M, [T::Unalias]>::from_raw_parts(
				addr.add(1).cast::<T::Unalias>(),
				elts - 1,
			)
		};
		Self::Region {
			head: Some(PartialElement::new(elem, head, None)),
			body,
			tail: None,
		}
	}

	/// Produces a `Domain::Region` with a partial `tail` and a `body`, but no
	/// `head`.
	#[inline]
	fn partial_tail(
		addr: Address<M, T>,
		elts: usize,
		_: BitIdx<T::Mem>,
		tail: BitEnd<T::Mem>,
	) -> Self {
		let elem = unsafe { addr.add(elts - 1) };
		let body = unsafe {
			Address::<M, [T::Unalias]>::from_raw_parts(
				addr.cast::<T::Unalias>(),
				elts - 1,
			)
		};
		Self::Region {
			head: None,
			body,
			tail: Some(PartialElement::new(elem, None, tail)),
		}
	}

	/// Produces a `Domain::Region` with neither `head` nor `tail`, but only a
	/// `body`.
	#[inline]
	fn spanning(
		addr: Address<M, T>,
		elts: usize,
		_: BitIdx<T::Mem>,
		_: BitEnd<T::Mem>,
	) -> Self {
		Self::Region {
			head: None,
			body: unsafe {
				<Address<M, [T::Unalias]> as SliceReferential>::from_raw_parts(
					addr.cast::<T::Unalias>(),
					elts,
				)
			},
			tail: None,
		}
	}
}

impl<'a, M, T, O> Default for Domain<'a, M, T, O>
where
	M: Mutability,
	T: 'a + BitStore,
	O: BitOrder,
	Address<M, T>: Referential<'a>,
	Address<M, [T::Unalias]>: SliceReferential<'a>,
	Reference<'a, M, [T::Unalias]>: Default,
{
	#[inline]
	fn default() -> Self {
		Self::Region {
			head: None,
			body: Reference::<M, [T::Unalias]>::default(),
			tail: None,
		}
	}
}

impl<'a, M, T, O> Debug for Domain<'a, M, T, O>
where
	M: Mutability,
	T: 'a + BitStore,
	O: BitOrder,
	Address<M, T>: Referential<'a>,
	Address<M, [T::Unalias]>: SliceReferential<'a>,
	Reference<'a, M, [T::Unalias]>: Debug,
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		write!(
			fmt,
			"Domain::<{} {}, {}>::",
			M::RENDER,
			any::type_name::<T>(),
			any::type_name::<O>(),
		)?;
		match self {
			Self::Enclave(elem) => {
				fmt.debug_tuple("Enclave").field(elem).finish()
			},
			Self::Region { head, body, tail } => fmt
				.debug_struct("Region")
				.field("head", head)
				.field("body", body)
				.field("tail", tail)
				.finish(),
		}
	}
}

#[cfg(not(tarpaulin_include))]
impl<T, O> Clone for Domain<'_, Const, T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn clone(&self) -> Self {
		*self
	}
}

impl<T, O> Iterator for Domain<'_, Const, T, O>
where
	T: BitStore,
	O: BitOrder,
{
	type Item = T::Mem;

	#[inline]
	fn next(&mut self) -> Option<Self::Item> {
		match self {
			Self::Enclave(elem) => {
				elem.load_value().tap(|_| *self = Default::default()).into()
			},
			Self::Region { head, body, tail } => {
				if let Some(elem) = head.take() {
					return elem.load_value().into();
				}
				if let Some((elem, rest)) = body.split_first() {
					*body = rest;
					return elem.load_value().into();
				}
				if let Some(elem) = tail.take() {
					return elem.load_value().into();
				}
				None
			},
		}
	}
}

impl<T, O> DoubleEndedIterator for Domain<'_, Const, T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn next_back(&mut self) -> Option<Self::Item> {
		match self {
			Self::Enclave(elem) => {
				elem.load_value().tap(|_| *self = Default::default()).into()
			},
			Self::Region { head, body, tail } => {
				if let Some(elem) = tail.take() {
					return elem.load_value().into();
				}
				if let Some((elem, rest)) = body.split_last() {
					*body = rest;
					return elem.load_value().into();
				}
				if let Some(elem) = head.take() {
					return elem.load_value().into();
				}
				None
			},
		}
	}
}

impl<T, O> ExactSizeIterator for Domain<'_, Const, T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn len(&self) -> usize {
		match self {
			Self::Enclave(_) => 1,
			Self::Region { head, body, tail } => {
				head.is_some() as usize + body.len() + tail.is_some() as usize
			},
		}
	}
}

impl<T, O> FusedIterator for Domain<'_, Const, T, O>
where
	T: BitStore,
	O: BitOrder,
{
}

impl<T, O> Copy for Domain<'_, Const, T, O>
where
	T: BitStore,
	O: BitOrder,
{
}

/// Implements numeric formatting by rendering each element.
macro_rules! fmt {
	($($fmt:ty => $fwd:ident),+ $(,)?) => { $(
		impl<'a, T, O> $fmt for Domain<'a, Const, T, O>
		where
			O: BitOrder,
			T: BitStore,
		{
			#[inline]
			fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
				fmt.debug_list()
					.entries(self.into_iter().map(FmtForward::$fwd))
					.finish()
			}
		}
	)+ };
}

fmt! {
	Binary => fmt_binary,
	Display => fmt_display,
	LowerHex => fmt_lower_hex,
	Octal => fmt_octal,
	UpperHex => fmt_upper_hex,
}

#[doc = include_str!("../doc/domain/PartialElement.md")]
pub struct PartialElement<'a, M, T, O>
where
	M: Mutability,
	T: 'a + BitStore,
	O: BitOrder,
{
	/// The address of the memory element being partially viewed.
	///
	/// This must be stored as a pointer, not a reference, because it must
	/// retain mutability permissions but cannot have an `&mut` reference to
	/// a shared element.
	///
	/// Similarly, it must remain typed as `T`, not `T::Access`, to allow the
	/// `<Const, uN>` case not to inappropriately produce a `<Const, Cell<uN>>`
	/// even if no write is performed.
	elem: Address<M, T>,
	/// Cache the selector mask, so it never needs to be recomputed.
	mask: BitMask<T::Mem>,
	/// The starting index.
	head: BitIdx<T::Mem>,
	/// The ending index.
	tail: BitEnd<T::Mem>,
	/// Preserve the originating bit-order
	_ord: PhantomData<O>,
	/// This type acts as-if it were a shared-mutable reference.
	_ref: PhantomData<&'a T::Access>,
}

impl<'a, M, T, O> PartialElement<'a, M, T, O>
where
	M: Mutability,
	T: 'a + BitStore,
	O: BitOrder,
{
	/// Constructs a new partial-element guarded reference.
	///
	/// ## Parameters
	///
	/// - `elem`: the element to which this partially points.
	/// - `head`: the index at which the partial region begins.
	/// - `tail`: the index at which the partial region ends.
	#[inline]
	fn new(
		elem: Address<M, T>,
		head: impl Into<Option<BitIdx<T::Mem>>>,
		tail: impl Into<Option<BitEnd<T::Mem>>>,
	) -> Self {
		let (head, tail) = (
			head.into().unwrap_or(BitIdx::MIN),
			tail.into().unwrap_or(BitEnd::MAX),
		);
		Self {
			elem,
			mask: O::mask(head, tail),
			head,
			tail,
			_ord: PhantomData,
			_ref: PhantomData,
		}
	}

	/// Fetches the value stored through `self` and masks away extra bits.
	///
	/// ## Returns
	///
	/// A bit-map containing any bits set to `1` in the governed bits. All other
	/// bits are cleared to `0`.
	#[inline]
	pub fn load_value(&self) -> T::Mem {
		self.elem
			.pipe(|addr| unsafe { &*addr.to_const() })
			.load_value()
			& self.mask.into_inner()
	}

	/// Gets the starting index of the live bits in the element.
	#[inline]
	#[cfg(not(tarpaulin_include))]
	pub fn head(&self) -> BitIdx<T::Mem> {
		self.head
	}

	/// Gets the ending index of the live bits in the element.
	#[inline]
	#[cfg(not(tarpaulin_include))]
	pub fn tail(&self) -> BitEnd<T::Mem> {
		self.tail
	}

	/// Gets the semantic head and tail indices that constrain which bits of the
	/// referent element may be accessed.
	#[inline]
	#[cfg(not(tarpaulin_include))]
	pub fn bounds(&self) -> (BitIdx<T::Mem>, BitEnd<T::Mem>) {
		(self.head, self.tail)
	}

	/// Gets the bit-mask over all accessible bits.
	#[inline]
	#[cfg(not(tarpaulin_include))]
	pub fn mask(&self) -> BitMask<T::Mem> {
		self.mask
	}

	/// Converts the partial element into a bit-slice over its governed bits.
	#[inline]
	pub fn into_bitslice(self) -> Reference<'a, M, BitSlice<T, O>>
	where Address<M, BitSlice<T, O>>: Referential<'a> {
		unsafe {
			BitSpan::new_unchecked(
				self.elem,
				self.head,
				(self.tail.into_inner() - self.head.into_inner()) as usize,
			)
		}
		.to_bitslice()
	}
}

impl<'a, T, O> PartialElement<'a, Mut, T, O>
where
	T: BitStore,
	O: BitOrder,
	Address<Mut, T>: Referential<'a>,
{
	/// Stores a value through `self` after masking away extra bits.
	///
	/// ## Parameters
	///
	/// - `&mut self`
	/// - `value`: A bit-map which will be written into the governed bits. This
	///   is a bit-map store, not an integer store; the value will not be
	///   shifted into position and will only be masked directly against the
	///   bits that this partial-element governs.
	///
	/// ## Returns
	///
	/// The previous value of the governed bits.
	#[inline]
	pub fn store_value(&mut self, value: T::Mem) -> T::Mem {
		let this = self.access();
		let prev = this.clear_bits(self.mask);
		this.set_bits(self.mask & value);
		prev & self.mask.into_inner()
	}

	/// Inverts the value of each bit governed by the partial-element.
	///
	/// ## Returns
	///
	/// The previous value of the governed bits.
	#[inline]
	#[cfg(not(tarpaulin_include))]
	pub fn invert(&mut self) -> T::Mem {
		self.access().invert_bits(self.mask) & self.mask.into_inner()
	}

	/// Clears all bits governed by the partial-element to `0`.
	///
	/// ## Returns
	///
	/// The previous value of the governed bits.
	#[inline]
	#[cfg(not(tarpaulin_include))]
	pub fn clear(&mut self) -> T::Mem {
		self.access().clear_bits(self.mask) & self.mask.into_inner()
	}

	/// Sets all bits governed by the partial-element to `1`.
	///
	/// ## Returns
	///
	/// The previous value of the governed bits.
	#[inline]
	#[cfg(not(tarpaulin_include))]
	pub fn set(&mut self) -> T::Mem {
		self.access().set_bits(self.mask) & self.mask.into_inner()
	}

	/// Produces a reference capable of tolerating other handles viewing the
	/// same *memory element*.
	#[inline]
	fn access(&self) -> &T::Access {
		unsafe { &*self.elem.to_const().cast::<T::Access>() }
	}
}

impl<'a, M, T, O> PartialElement<'a, M, T, O>
where
	M: Mutability,
	O: BitOrder,
	T: 'a + BitStore + radium::Radium,
{
	/// Performs a store operation on a partial-element whose bits might be
	/// observed by another handle.
	#[inline]
	pub fn store_value_aliased(&self, value: T::Mem) -> T::Mem {
		let this = unsafe { &*self.elem.to_const().cast::<T::Access>() };
		let prev = this.clear_bits(self.mask);
		this.set_bits(self.mask & value);
		prev & self.mask.into_inner()
	}
}

#[cfg(not(tarpaulin_include))]
impl<'a, T, O> Clone for PartialElement<'a, Const, T, O>
where
	T: BitStore,
	O: BitOrder,
	Address<Const, T>: Referential<'a>,
{
	#[inline]
	fn clone(&self) -> Self {
		*self
	}
}

impl<'a, M, T, O> Debug for PartialElement<'a, M, T, O>
where
	M: Mutability,
	T: 'a + BitStore,
	O: BitOrder,
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		write!(
			fmt,
			"PartialElement<{} {}, {}>",
			M::RENDER,
			any::type_name::<T>(),
			any::type_name::<O>(),
		)?;
		fmt.debug_struct("")
			.field("elem", &self.load_value())
			.field("mask", &self.mask.fmt_display())
			.field("head", &self.head.fmt_display())
			.field("tail", &self.tail.fmt_display())
			.finish()
	}
}

#[cfg(not(tarpaulin_include))]
impl<'a, M, T, O> Hash for PartialElement<'a, M, T, O>
where
	M: Mutability,
	T: 'a + BitStore,
	O: BitOrder,
{
	#[inline]
	fn hash<H>(&self, hasher: &mut H)
	where H: Hasher {
		self.load_value().hash(hasher);
		self.mask.hash(hasher);
		self.head.hash(hasher);
		self.tail.hash(hasher);
	}
}

impl<T, O> Copy for PartialElement<'_, Const, T, O>
where
	T: BitStore,
	O: BitOrder,
{
}

#[cfg(test)]
mod tests {
	use rand::random;

	use super::*;
	use crate::prelude::*;

	#[test]
	fn bit_domain() {
		let data = BitArray::<[u32; 3], Msb0>::new(random());

		let bd = data.bit_domain();
		assert!(bd.enclave().is_none());
		let (head, body, tail) = bd.region().unwrap();
		assert_eq!(data, body);
		assert!(head.is_empty());
		assert!(tail.is_empty());

		let bd = data[2 ..].bit_domain();
		let (head, body, tail) = bd.region().unwrap();
		assert_eq!(head, &data[2 .. 32]);
		assert_eq!(body, &data[32 ..]);
		assert!(tail.is_empty());

		let bd = data[.. 94].bit_domain();
		let (head, body, tail) = bd.region().unwrap();
		assert!(head.is_empty());
		assert_eq!(body, &data[.. 64]);
		assert_eq!(tail, &data[64 .. 94]);

		let bd = data[2 .. 94].bit_domain();
		let (head, body, tail) = bd.region().unwrap();
		assert_eq!(head, &data[2 .. 32]);
		assert_eq!(body, &data[32 .. 64]);
		assert_eq!(tail, &data[64 .. 94]);

		let bd = data[34 .. 62].bit_domain();
		assert!(bd.region().is_none());
		assert_eq!(bd.enclave().unwrap(), data[34 .. 62]);

		let (head, body, tail) =
			BitDomain::<Const, usize, Lsb0>::default().region().unwrap();
		assert!(head.is_empty());
		assert!(body.is_empty());
		assert!(tail.is_empty());
	}

	#[test]
	fn domain() {
		let data: [u32; 3] = random();
		let bits = data.view_bits::<Msb0>();

		let d = bits.domain();
		assert!(d.enclave().is_none());
		let (head, body, tail) = d.region().unwrap();
		assert!(head.is_none());
		assert!(tail.is_none());
		assert_eq!(body, data);

		let d = bits[2 ..].domain();
		let (head, body, tail) = d.region().unwrap();
		assert_eq!(head.unwrap().load_value(), (data[0] << 2) >> 2);
		assert_eq!(body, &data[1 ..]);
		assert!(tail.is_none());

		let d = bits[.. 94].domain();
		let (head, body, tail) = d.region().unwrap();
		assert!(head.is_none());
		assert_eq!(body, &data[.. 2]);
		assert_eq!(tail.unwrap().load_value(), (data[2] >> 2) << 2);

		let d = bits[2 .. 94].domain();
		let (head, body, tail) = d.region().unwrap();
		assert_eq!(head.unwrap().load_value(), (data[0] << 2) >> 2);
		assert_eq!(body, &data[1 .. 2]);
		assert_eq!(tail.unwrap().load_value(), (data[2] >> 2) << 2);

		let d = bits[34 .. 62].domain();
		assert!(d.region().is_none());
		assert_eq!(
			d.enclave().unwrap().load_value(),
			((data[1] << 2) >> 4) << 2,
		);

		assert!(matches!(bits![].domain(), Domain::Region {
			head: None,
			body: &[],
			tail: None,
		}));

		assert!(matches!(
			Domain::<Const, usize, Lsb0>::default(),
			Domain::Region {
				head: None,
				body: &[],
				tail: None,
			},
		));

		let data = core::cell::Cell::new(0u8);
		let partial =
			data.view_bits::<Lsb0>()[2 .. 6].domain().enclave().unwrap();
		assert_eq!(partial.store_value_aliased(!0), 0);
		assert_eq!(data.get(), 0b00_1111_00);
	}

	#[test]
	fn iter() {
		let bits = [0x12u8, 0x34, 0x56].view_bits::<Lsb0>();
		let mut domain = bits[4 .. 12].domain();
		assert_eq!(domain.len(), 2);
		assert_eq!(domain.next().unwrap(), 0x10);
		assert_eq!(domain.next_back().unwrap(), 0x04);

		assert!(domain.next().is_none());
		assert!(domain.next_back().is_none());

		assert_eq!(bits[2 .. 6].domain().len(), 1);
		assert_eq!(bits[18 .. 22].domain().next_back().unwrap(), 0b00_0101_00);

		let mut domain = bits[4 .. 20].domain();
		assert_eq!(domain.next_back().unwrap(), 0x06);
		assert_eq!(domain.next_back().unwrap(), 0x34);
		assert_eq!(domain.next_back().unwrap(), 0x10);
	}

	#[test]
	#[cfg(feature = "alloc")]
	fn render() {
		#[cfg(not(feature = "std"))]
		use alloc::format;

		let data = BitArray::<u32, Msb0>::new(random());

		let render = format!("{:?}", data.bit_domain());
		let expected = format!(
			"BitDomain::<*const u32, {}>::Region {{ head: {:?}, body: {:?}, \
			 tail: {:?} }}",
			any::type_name::<Msb0>(),
			BitSlice::<u32, Msb0>::empty(),
			data.as_bitslice(),
			BitSlice::<u32, Msb0>::empty(),
		);
		assert_eq!(render, expected);

		let render = format!("{:?}", data[2 .. 30].bit_domain());
		let expected = format!(
			"BitDomain::<*const u32, {}>::Enclave({:?})",
			any::type_name::<Msb0>(),
			&data[2 .. 30],
		);
		assert_eq!(render, expected);

		let render = format!("{:?}", data.domain());
		let expected = format!(
			"Domain::<*const u32, {}>::Region {{ head: None, body: {:?}, tail: \
			 None }}",
			any::type_name::<Msb0>(),
			data.as_raw_slice(),
		);
		assert_eq!(render, expected);

		let render = format!("{:?}", data[2 .. 30].domain());
		let expected = format!(
			"Domain::<*const u32, {}>::Enclave",
			any::type_name::<Msb0>(),
		);
		assert!(render.starts_with(&expected));

		let partial = 0x3Cu8.view_bits::<Lsb0>()[2 .. 6]
			.domain()
			.enclave()
			.unwrap();
		let render = format!("{:?}", partial);
		assert_eq!(
			render,
			format!(
				"PartialElement<*const u8, {}> {{ elem: 60, mask: {}, head: \
				 {}, tail: {} }}",
				any::type_name::<Lsb0>(),
				partial.mask,
				partial.head,
				partial.tail,
			),
		);
	}
}