bitvec 0.21.0

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
/*! A dynamically-allocated buffer containing a [`BitSlice`] region.

You can read the standard library’s [`alloc::vec` module documentation][std]
here.

This module defines the [`BitVec`] buffer, and all of its associated support
code.

[`BitVec`] is equivalent to [`Vec<bool>`], in its operation and in its
relationship to the [`BitSlice`] type. Most of the interesting work to be done
on a bit-sequence is implemented in `BitSlice`, to which `BitVec` dereferences,
and the vector container itself only exists to maintain ownership, implement
dynamic resizing, and provide some specializations that cannot safely be done on
`BitSlice` alone.

[`BitSlice`]: crate::slice::BitSlice
[`BitVec`]: crate::vec::BitVec
[`Vec<bool>`]: alloc::vec::Vec
[std]: mod@alloc::vec
!*/

#![cfg(feature = "alloc")]

#[cfg(not(feature = "std"))]
use alloc::vec;
use alloc::vec::Vec;
use core::{
	mem::{
		self,
		ManuallyDrop,
	},
	slice,
};

use funty::{
	IsInteger,
	IsNumber,
};
use tap::{
	pipe::Pipe,
	tap::Tap,
};

use crate::{
	boxed::BitBox,
	domain::Domain,
	index::BitIdx,
	mem::BitRegister,
	mutability::{
		Const,
		Mut,
	},
	order::{
		BitOrder,
		Lsb0,
	},
	ptr::{
		BitPtr,
		BitSpan,
		BitSpanError,
	},
	slice::BitSlice,
	store::BitStore,
};

/** A contiguous growable array of bits.

This is a managed, heap-allocated, buffer that contains a [`BitSlice`] region.
It is analagous to [`Vec<bool>`], and is written to be very nearly a drop-in
replacement for it. This type contains little interesting behavior in its own
right; most of its behavior is provided by dereferencing to its managed
[`BitSlice`] buffer. It instead serves primarily as an interface to the
allocator, and has some specialized behaviors for its fully-owned memory buffer.

# Documentation

All APIs that mirror something in the standard library will have an `Original`
section linking to the corresponding item. All APIs that have a different
signature or behavior than the original will have an `API Differences` section
explaining what has changed, and how to adapt your existing code to the change.

These sections look like this:

# Original

[`Vec<T>`](alloc::vec::Vec)

# API Differences

The buffer type [`Vec<bool>`] has no type parameters. `BitVec<O, T>` has the
same two type parameters as [`BitSlice<O, T>`][`BitSlice`]. Otherwise, `BitVec`
is able to implement the full API surface of `Vec<bool>`.

# Examples

Because `BitVec` takes type parameters, but has default type arguments for them,
you will need to specify its type parameters when using its associated
functions. The easiest way to do this is to declare bindings type as `: BitVec`,
which uses the default type arguments.

```rust
use bitvec::prelude::*;

let mut bv: BitVec = BitVec::new();
bv.push(false);
bv.push(true);

assert_eq!(bv.len(), 2);
assert_eq!(bv[0], false);

assert_eq!(bv.pop(), Some(true));
assert_eq!(bv.len(), 1);

// `BitVec` cannot yet support `[]=` write indexing.
*bv.get_mut(0).unwrap() = true;
assert_eq!(bv[0], true);

bv.extend(bits![0, 1, 0]);

for bit in &bv {
  println!("{}", bit);
}
assert_eq!(bv, bits![1, 0, 1, 0]);
```

The [`bitvec!`] macro is provided to make initialization more convenient:

```rust
use bitvec::prelude::*;

let mut bv = bitvec![0, 0, 1];
bv.push(true);
assert_eq!(bv, bits![0, 0, 1, 1]);
```

It has the same argument syntax as [`vec!`]. In addition, it can take type
arguments for ordering and storage:

```rust
use bitvec::prelude::*;

let bv = bitvec![Msb0, u16; 1; 30];
assert!(bv.all());
assert_eq!(bv.len(), 30);
```

# Indexing

The `BitVec` type allows you to access bits by index, because it implements the
[`Index`] trait. However, because [`IndexMut`] requires producing an `&mut bool`
reference, it cannot implement `[]=` index assignment syntax. Instead, you must
use [`get_mut`] or [`get_unchecked_mut`] to produce proxy types that can serve
the same purpose.

# Slicing

A `BitVec` is resizable, while [`BitSlice`] is a fixed-size view of a buffer.
Just as with ordinary [`Vec`]s and slices, you can get a `BitSlice` from a
`BitVec` by borrowing it:

```rust
use bitvec::prelude::*;

fn read_bitslice(slice: &BitSlice) {
  // …
}

let bv = bitvec![0; 30];
read_bitslice(&bv);

// … and that’s all!
// you can also do it like this:
let x: &BitSlice = &bv;
```

As with ordinary Rust types, you should prefer passing bit-slices rather than
buffers when you just want to inspect the data, and not manage the underlying
memory region.

# Behavior

Because `BitVec` is a fully-owned buffer, it is able to operate on its memory
without concern for any other views that may alias. This enables it to
specialize some [`BitSlice`] behavior to be faster or more efficient. However,
`BitVec` is *not* restricted to only using unaliased integer storage, and
technically permits the construction of `BitVec<_, AtomicType>`.

This restriction is extremely awkward and constraining to write in the library,
and clients will probably never attempt to construct them, but the possibility
is still present. Be aware of this possibility when using generic code to
convert from `BitSlice` to `BitVec`. Fully-typed code does not need to be
concerned with this possibility.

# Capacity and Reällocation

The capacity of a bit-vector is the amount of space allocated for any future
bits that will be added onto the vector. This is not to be confused with the
*length* of a vector, which specifies the number of actual bits within the
vector. If a vector’s length exceeds its capacity, its capacity will
automatically be increased, but its buffer will have to be reällocated

For example, a bit-vector with capacity 64 and length 0 would be an empty vector
with space for 64 more bits. Pushing 64 or fewer bits onto the vector will not
change its capacity or cause reällocation to occur. However, if the vector’s
length is increased to 65, it *may* have to reällocate, which can be slow. For
this reason, it is recommended to use [`BitVec::with_capacity`] whenever
possible to specify how big the vector is expected to get.

# Safety

Like [`BitSlice`], `BitVec` is exactly equal in size to [`Vec`], and is also
absolutely representation-incompatible with it. You must never attempt to
type-cast between `Vec<T>` and `BitVec` in any way, nor attempt to modify the
memory value of a `BitVec` handle. Doing so will cause allocator and memory
errors in your program, likely inducing a panic.

Everything in the `BitVec` public API, even the `unsafe` parts, are guaranteed
to have no more unsafety than their equivalent items in the standard library.
All `unsafe` APIs will have documentation explicitly detailing what the API
requires you to uphold in order for it to function safely and correctly. All
safe APIs will do so themselves.

# Performance

The choice of [`BitStore`] type parameter can impact your vector’s performance,
as the allocator operates in units of `T` rather than in bits. This means that
larger register types will increase the amount of memory reserved in each call
to the allocator, meaning fewer calls to [`push`] will actually cause a
reällocation. In addition, iteration over the vector is governed by the
[`BitSlice`] characteristics on the type parameter. You are generally better off
using larger types when your vector is a data collection rather than a specific
I/O protocol buffer.

# Macro Construction

Heap allocation can only occur at runtime, but the [`bitvec!`] macro will
construct an appropriate [`BitSlice`] buffer at compile-time, and at run-time,
only copy the buffer into a heap allocation.

[`BitStore`]: crate::store::BitStore
[`BitSlice`]: crate::slice::BitSlice
[`BitVec::with_capacity`]: Self::with_capacity
[`Index`]: core::ops::Index
[`IndexMut`]: core::ops::IndexMut
[`Vec`]: alloc::vec::Vec
[`Vec<bool>`]: alloc::vec::Vec
[`bitvec!`]: macro@crate::bitvec
[`vec!`]: macro@alloc::vec
[`get_mut`]: crate::slice::BitSlice::get_mut
[`get_unchecked_mut`]: crate::slice::BitSlice::get_unchecked_mut
[`push`]: Self::push
**/
#[repr(C)]
pub struct BitVec<O = Lsb0, T = usize>
where
	O: BitOrder,
	T: BitStore,
{
	/// Region pointer describing the live portion of the owned buffer.
	bitspan: BitSpan<Mut, O, T>,
	/// Allocated capacity, in elements `T`, of the owned buffer.
	capacity: usize,
}

/// General-purpose functions not present on `Vec<T>`.
impl<O, T> BitVec<O, T>
where
	O: BitOrder,
	T: BitStore,
{
	/// Constructs a `BitVec` from a value repeated many times.
	///
	/// This function is equivalent to the `bitvec![O, T; bit; len]` [macro]
	/// call, and is in fact the implementation of that macro syntax.
	///
	/// # Parameters
	///
	/// - `bit`: The bit value to which all `len` allocated bits will be set.
	/// - `len`: The number of live bits in the constructed `BitVec`.
	///
	/// # Returns
	///
	/// A `BitVec` with `len` live bits, all set to `bit`.
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let bv = BitVec::<Msb0, u8>::repeat(true, 20);
	/// assert_eq!(bv, bits![1; 20]);
	/// ```
	///
	/// [macro]: macro@crate::bitvec
	#[inline]
	pub fn repeat(bit: bool, len: usize) -> Self {
		let mut out = Self::with_capacity(len);
		unsafe {
			out.set_len(len);
		}
		out.set_elements(if bit { T::Mem::ALL } else { T::Mem::ZERO });
		out
	}

	/// Copies the contents of a [`BitSlice`] into a new allocation.
	///
	/// This is an exact copy: the newly-created bit-vector is initialized with
	/// a direct copy of the `slice`’s underlying contents, and its handle is
	/// set to use `slice`’s head index. Slices that do not begin at the zeroth
	/// bit of the base element will thus create misaligned vectors.
	///
	/// You can move the bit-vector contents down to begin at the zero index of
	/// the bit-vector’s buffer with [`force_align`].
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let bits = bits![0, 1, 0, 1, 1, 0, 1, 1];
	/// let bv = BitVec::from_bitslice(&bits[2 ..]);
	/// assert_eq!(bv, bits[2 ..]);
	/// assert_eq!(bits.as_raw_slice(), bv.as_raw_slice());
	/// ```
	///
	/// [`BitSlice`]: crate::slice::BitSlice
	/// [`force_align`]: Self::force_align
	#[inline]
	pub fn from_bitslice(slice: &BitSlice<O, T>) -> Self {
		let mut bitspan = slice.as_bitspan();

		let mut vec = bitspan
			.elements()
			.pipe(Vec::with_capacity)
			.pipe(ManuallyDrop::new);

		match slice.domain() {
			Domain::Enclave { elem, .. } => vec.push(elem.load_value()),
			Domain::Region { head, body, tail } => {
				if let Some((_, elem)) = head {
					vec.push(elem.load_value());
				}
				vec.extend(body.iter().map(BitStore::load_value));
				if let Some((elem, _)) = tail {
					vec.push(elem.load_value());
				}
			},
		}

		let bitspan = unsafe {
			bitspan.set_address(vec.as_ptr() as *const T);
			bitspan.assert_mut()
		};

		let capacity = vec.capacity();
		Self { bitspan, capacity }
	}

	/// Constructs a new `BitVec` from the bit-pattern of a single element.
	///
	/// This function copies `elem` into a new vector, then views that vector as
	/// bits.
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// assert_eq!(BitVec::<Msb0, _>::from_element(0xABBAu16).count_ones(), 10);
	/// ```
	#[inline]
	pub fn from_element(elem: T) -> Self {
		vec![elem].pipe(Self::from_vec)
	}

	/// Constructs a new `BitVec` from the bit-pattern of an element slice.
	///
	/// This function copies `slice` into a new vector, then views that vector
	/// as bits.
	///
	/// # Parameters
	///
	/// - `slice`: A slice of elements. It should not exceed [`BitSlice::<O,
	///   T>::MAX_ELTS`].
	///
	/// # Returns
	///
	/// This returns an error if [`BitSlice::<O, T>::from_slice`] fails;
	/// otherwise, it returns the newly allocated and initialized bit-vector.
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let slice = &[0u8, 1, 2, 3];
	/// let bv = BitVec::<Lsb0, _>::from_slice(slice);
	/// assert!(bv.is_ok());
	/// assert_eq!(bv.unwrap().len(), 32);
	/// ```
	///
	/// [`BitSlice::<O, T>::MAX_ELTS`]: crate::slice::BitSlice::MAX_ELTS
	/// [`BitSlice::<O, T>::from_slice`]: crate::slice::BitSlice::from_slice
	pub fn from_slice(slice: &[T]) -> Result<Self, BitSpanError<T>> {
		slice.pipe(BitSlice::from_slice).map(Self::from_bitslice)
	}

	/// Converts a [`Vec<T>`] into a `BitVec<O, T>` without copying its buffer.
	///
	/// # Parameters
	///
	/// - `vec`: A vector to view as bits.
	///
	/// # Returns
	///
	/// A `BitVec` over the `vec` buffer.
	///
	/// # Panics
	///
	/// This panics if `vec` is too long to convert into a `BitVec`. See
	/// [`BitSlice::MAX_ELTS`].
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let vec = vec![0u8; 4];
	/// let bv = BitVec::<LocalBits, _>::from_vec(vec);
	/// assert_eq!(bv, bits![0; 32]);
	/// ```
	///
	/// [`BitSlice::MAX_ELTS`]: crate::slice::BitSlice::MAX_ELTS
	/// [`Vec<T>`]: alloc::vec::Vec
	#[inline]
	pub fn from_vec(vec: Vec<T>) -> Self {
		Self::try_from_vec(vec)
			.expect("Vector was too long to be converted into a `BitVec`")
	}

	/// Converts a [`Vec<T>`] into a `BitVec<O, T>` without copying its buffer.
	///
	/// This method takes ownership of a memory buffer and enables it to be used
	/// as a bit-vector. Because [`Vec`] can be longer than `BitVec`s, this is a
	/// fallible method, and the original vector will be returned if it cannot
	/// be converted.
	///
	/// # Parameters
	///
	/// - `vec`: Some vector of memory, to be viewed as bits.
	///
	/// # Returns
	///
	/// If `vec` is short enough to be viewed as a `BitVec`, then this returns
	/// a `BitVec` over the `vec` buffer. If `vec` is too long, then this
	/// returns `vec` unmodified.
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let vec = vec![0u8; 4];
	/// let bv = BitVec::<LocalBits, _>::try_from_vec(vec).unwrap();
	/// assert_eq!(bv, bits![0; 32]);
	/// ```
	///
	/// An example showing this function failing would require an allocation
	/// exceeding `!0usize >> 3` bytes in size, which is infeasible to produce.
	///
	/// [`Vec`]: alloc::vec::Vec
	/// [`Vec<T>`]: alloc::vec::Vec
	#[inline]
	pub fn try_from_vec(vec: Vec<T>) -> Result<Self, Vec<T>> {
		let mut vec = ManuallyDrop::new(vec);
		let capacity = vec.capacity();

		BitPtr::from_mut_slice(vec.as_mut_slice())
			.span(vec.len() * T::Mem::BITS as usize)
			.map(|bitspan| Self { bitspan, capacity })
			.map_err(|_| ManuallyDrop::into_inner(vec))
	}

	/// Copies all bits in a [`BitSlice`] into the `BitVec`.
	///
	/// # Original
	///
	/// [`Vec::extend_from_slice`](alloc::vec::Vec::extend_from_slice)
	///
	/// # Type Parameters
	///
	/// This can extend from a [`BitSlice`] of any type arguments. Where the
	/// source `&BitSlice` matches `self`’s type parameters, the implementation
	/// is able to attempt to accelerate the copy; however, if the type
	/// parameters do not match, then the implementation falls back to a
	/// bit-by-bit iteration and is equivalent to the `Extend` implementation.
	///
	/// You should only use this method when the type parameters match and there
	/// is a possibility of copy acceleration. Otherwise, `.extend()` is the
	/// correct API.
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let mut bv = bitvec![0, 1];
	/// bv.extend_from_bitslice(bits![1, 1, 0, 1]);
	///
	/// assert_eq!(bv, bits![0, 1, 1, 1, 0, 1]);
	/// ```
	///
	/// [`BitSlice`]: crate::slice::BitSlice
	//  Implementation note: per #85, users want this method to stay generic.
	#[inline]
	pub fn extend_from_bitslice<O2, T2>(&mut self, other: &BitSlice<O2, T2>)
	where
		O2: BitOrder,
		T2: BitStore,
	{
		let len = self.len();
		let olen = other.len();
		self.resize(len + olen, false);
		unsafe { self.get_unchecked_mut(len ..) }.clone_from_bitslice(other);
	}

	/// Appends a slice of elements `T` to the `BitVec`.
	///
	/// The `slice` is interpreted as a `BitSlice<O, T>`, then appended directly
	/// to the bit-vector.
	///
	/// # Original
	///
	/// [`Vec::extend_from_slice`](alloc::vec::Vec::extend_from_slice)
	#[inline]
	pub fn extend_from_raw_slice(&mut self, slice: &[T]) {
		self.extend_from_bitslice(
			BitSlice::<O, T>::from_slice(slice)
				.expect("Slice is too long to encode as a BitSlice"),
		);
	}

	/// Gets the number of elements `T` that contain live bits of the
	/// bit-vector.
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let bv = bitvec![LocalBits, u16; 1; 50];
	/// assert_eq!(bv.elements(), 4);
	/// ```
	#[inline]
	pub fn elements(&self) -> usize {
		self.as_bitspan().elements()
	}

	/// Converts the bit-vector into [`BitBox<O, T>`].
	///
	/// Note that this will drop any excess capacity.
	///
	/// # Original
	///
	/// [`Vec::into_boxed_slice`](alloc::vec::Vec::into_boxed_slice)
	///
	/// # API Differences
	///
	/// This returns a `bitvec` boxed bit-slice, not a standard boxed slice. To
	/// convert the underlying buffer into a boxed element slice, use
	/// `.into_boxed_bitslice().into_boxed_slice()`.
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let bv = bitvec![0, 1, 0, 0, 1];
	/// let bitslice = bv.into_boxed_slice();
	/// ```
	///
	/// Any excess capacity is removed:
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let mut bv: BitVec = BitVec::with_capacity(100);
	/// bv.extend([0, 1, 0, 0, 1].iter().copied());
	///
	/// assert!(bv.capacity() >= 100);
	/// let bs = bv.into_boxed_bitslice();
	/// assert!(bs.into_bitvec().capacity() >= 5);
	/// ```
	///
	/// [`BitBox<O, T>`]: crate::boxed::BitBox
	#[inline]
	pub fn into_boxed_bitslice(mut self) -> BitBox<O, T> {
		let mut bitspan = self.as_mut_bitspan();
		let mut boxed =
			self.into_vec().into_boxed_slice().pipe(ManuallyDrop::new);
		unsafe {
			bitspan.set_address(boxed.as_mut_ptr());
			BitBox::from_raw(bitspan.to_bitslice_ptr_mut())
		}
	}

	/// Removes the bit-precision view, returning the underlying [`Vec`].
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let bv = bitvec![Lsb0, u8; 0, 1, 0, 0, 1];
	/// let vec = bv.into_vec();
	/// assert_eq!(vec, &[18]);
	/// ```
	///
	/// [`Vec`]: alloc::vec::Vec
	#[inline]
	pub fn into_vec(self) -> Vec<T> {
		let (bitspan, capacity) = (self.bitspan, self.capacity);
		mem::forget(self);
		unsafe {
			Vec::from_raw_parts(
				bitspan.address().to_mut(),
				bitspan.elements(),
				capacity,
			)
		}
	}

	/// Writes a value into every element that the bit-vector considers live.
	///
	/// This unconditionally writes `element` into each live location in the
	/// backing buffer, without altering the `BitVec`’s length or capacity.
	///
	/// It is unspecified what effects this has on the allocated but dead
	/// elements in the buffer. You may not rely on them being zeroed *or* being
	/// set to the `value` integer.
	///
	/// # Parameters
	///
	/// - `&mut self`
	/// - `element`: The value which will be written to each live location in
	///   the bit-vector’s buffer.
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let mut bv = bitvec![LocalBits, u8; 0; 10];
	/// assert_eq!(bv.as_raw_slice(), [0, 0]);
	/// bv.set_elements(0xA5);
	/// assert_eq!(bv.as_raw_slice(), [0xA5, 0xA5]);
	/// ```
	#[inline]
	pub fn set_elements(&mut self, element: T::Mem) {
		self.as_mut_raw_slice()
			.iter_mut()
			.for_each(|elt| elt.store_value(element));
	}

	/// Sets the uninitialized bits of the bit-vector to a fixed value.
	///
	/// This method modifies all bits in the allocated buffer that are outside
	/// the [`as_bitslice`] view so that they have a consistent value. This can
	/// be used to zero the uninitialized memory so that when viewed as a raw
	/// memory slice, bits outside the live region have a predictable value.
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let mut bv = 220u8.view_bits::<Lsb0>().to_bitvec();
	/// assert_eq!(bv.as_raw_slice(), &[220u8]);
	///
	/// bv.truncate(4);
	/// assert_eq!(bv.count_ones(), 2);
	/// assert_eq!(bv.as_raw_slice(), &[220u8]);
	///
	/// bv.set_uninitialized(false);
	/// assert_eq!(bv.as_raw_slice(), &[12u8]);
	///
	/// bv.set_uninitialized(true);
	/// assert_eq!(bv.as_raw_slice(), &[!3u8]);
	/// ```
	///
	/// [`as_bitslice`]: Self::as_bitslice
	#[inline]
	pub fn set_uninitialized(&mut self, value: bool) {
		let head = self.as_bitspan().head().value() as usize;
		let tail = head + self.len();
		let capa = self.capacity();
		let mut bp = self.as_mut_bitspan();
		unsafe {
			bp.set_head(BitIdx::ZERO);
			bp.set_len(capa);
			let bits = bp.to_bitslice_mut();
			bits.get_unchecked_mut(.. head).set_all(value);
			bits.get_unchecked_mut(tail ..).set_all(value);
		}
	}

	/// Ensures that the live region of the bit-vector’s contents begins at the
	/// leading edge of the buffer.
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let data = 0x3Cu8;
	/// let bits = data.view_bits::<Msb0>();
	///
	/// let mut bv = bits[2 .. 6].to_bitvec();
	/// assert_eq!(bv, bits[2 .. 6]);
	/// assert_eq!(bv.as_raw_slice()[0], data);
	///
	/// bv.force_align();
	/// assert_eq!(bv, bits[2 .. 6]);
	/// // It is not specified what happens
	/// // to bits that are no longer used.
	/// assert_eq!(bv.as_raw_slice()[0] & 0xF0, 0xF0);
	/// ```
	#[inline]
	pub fn force_align(&mut self) {
		let bitspan = self.as_mut_bitspan();
		let head = bitspan.head().value() as usize;
		if head == 0 {
			return;
		}
		let last = bitspan.len() + head;
		unsafe {
			self.bitspan = bitspan.tap_mut(|bp| bp.set_head(BitIdx::ZERO));
			self.copy_within_unchecked(head .. last, 0);
		}
	}

	/// Writes a new length value into the pointer without any checks.
	///
	/// # Safety
	///
	/// `new_len` must not exceed `self.capacity() - self.bitspan.head()`.
	#[cfg_attr(not(tarpaulin_include), inline(always))]
	pub(crate) unsafe fn set_len_unchecked(&mut self, new_len: usize) {
		self.bitspan.set_len(new_len);
	}

	/// Extracts a bit-slice containing the entire bit-vector.
	///
	/// Equivalent to `&bv[..]`.
	///
	/// # Original
	///
	/// [`Vec::as_slice`](alloc::vec::Vec::as_slice)
	///
	/// # API Differences
	///
	/// This returns a `bitvec` bit-slice, not a standard slice. To view the
	/// underlying element buffer, use [`as_raw_slice`].
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let bv = bitvec![0, 1, 0, 0, 1];
	/// let bits = bv.as_bitslice();
	/// ```
	///
	/// [`as_raw_slice`]: Self::as_raw_slice
	#[cfg_attr(not(tarpaulin_include), inline(always))]
	pub fn as_bitslice(&self) -> &BitSlice<O, T> {
		self.bitspan.to_bitslice_ref()
	}

	/// Extracts a mutable bit-slice of the entire bit-vector.
	///
	/// Equivalent to `&mut bv[..]`.
	///
	/// # Original
	///
	/// [`Vec::as_mut_slice`](alloc::vec::Vec::as_mut_slice)
	///
	/// # API Differences
	///
	/// This returns a `bitvec` bit-slice, not a standard slice. To view the
	/// underlying element buffer, use [`as_mut_raw_slice`].
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let mut bv = bitvec![0, 1, 0, 0, 1];
	/// let bits = bv.as_mut_bitslice();
	/// ```
	///
	/// [`as_mut_raw_slice`]: Self::as_mut_raw_slice
	#[cfg_attr(not(tarpaulin_include), inline(always))]
	pub fn as_mut_bitslice(&mut self) -> &mut BitSlice<O, T> {
		self.bitspan.to_bitslice_mut()
	}

	/// Returns a raw pointer to the bit-vector’s buffer.
	///
	/// The caller must ensure that the bit-vector outlives the bit-pointer this
	/// function returns, or else it will end up pointing to garbage. Modifying
	/// the bit-vector may cause its buffer to be reällocated, which would also
	/// make any bit-pointers to it invalid.
	///
	/// The caller must also ensure that the memory the bit-pointer
	/// (non-transitively) points to is never written to (except inside an
	/// [`UnsafeCell`]) using this bit-pointer or any bit-pointer derived from
	/// it. If you need to mutate the contents of the buffer, use
	/// [`as_mut_bitptr`].
	///
	/// # Original
	///
	/// [`Vec::as_ptr`](alloc::vec::Vec::as_ptr)
	///
	/// # API Differences
	///
	/// This returns a `bitvec` bit-pointer, not a standard pointer. To take the
	/// address of the underlying element buffer, use [`as_raw_ptr`].
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let bv = bitvec![0, 1, 0, 0, 1];
	/// let bp = bv.as_bitptr();
	///
	/// unsafe {
	///   for i in 0 .. bv.len() {
	///     assert_eq!(bp.add(i).read(), bv[i]);
	///   }
	/// }
	/// ```
	///
	/// [`UnsafeCell`]: core::cell::UnsafeCell
	/// [`as_raw_ptr`]: Self::as_raw_ptr
	/// [`as_mut_bitptr`]: Self::as_mut_bitptr
	#[inline]
	pub fn as_bitptr(&self) -> BitPtr<Const, O, T> {
		self.bitspan.as_bitptr().immut()
	}

	/// Returns an unsafe mutable bit-pointer to the bit-vector’s region.
	///
	/// The caller must ensure that the bit-vector outlives the bit-pointer this
	/// function returns, or else it will end up pointing to garbage. Modifying
	/// the bit-vector may cause its buffer to be reällocated, which would also
	/// make any bit-pointers to it invalid.
	///
	/// # Original
	///
	/// [`Vec::as_mut_ptr`](alloc::vec::Vec::as_mut_ptr)
	///
	/// # API Differences
	///
	/// This returns a `bitvec` bit-pointer, not a standard pointer. To take the
	/// address of the underlying element buffer, use [`as_mut_raw_ptr`].
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let mut bv = BitVec::<Msb0, u8>::with_capacity(4);
	/// let bp = bv.as_mut_bitptr();
	/// unsafe {
	///   for i in 0 .. 4 {
	///     bp.add(i).write(true);
	///   }
	///   bv.set_len(4);
	/// }
	/// assert_eq!(bv, bits![1; 4]);
	/// ```
	///
	/// [`as_mut_raw_ptr`]: Self::as_mut_raw_ptr
	#[cfg_attr(not(tarpaulin_include), inline(always))]
	pub fn as_mut_bitptr(&mut self) -> BitPtr<Mut, O, T> {
		self.bitspan.as_bitptr()
	}

	/// Views the underlying buffer as a shared element slice.
	///
	/// # Original
	///
	/// [`Vec::as_slice`](alloc::vec::Vec::as_slice)
	///
	/// # API Differences
	///
	/// This method is renamed in order to emphasize the semantic distinction
	/// between borrowing the bit-vector contents, and borrowing the memory that
	/// implements the collection contents.
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let bv = bitvec![Msb0, u8; 0, 1, 0, 0, 1, 1, 0, 1];
	/// let raw = bv.as_raw_slice();
	/// assert_eq!(raw, &[0x4D]);
	/// ```
	#[inline]
	pub fn as_raw_slice(&self) -> &[T] {
		unsafe {
			slice::from_raw_parts(
				self.bitspan.address().to_const(),
				self.bitspan.elements(),
			)
		}
	}

	/// Views the underlying buffer as an exclusive element slice.
	///
	/// # Original
	///
	/// [`Vec::as_mut_slice`](alloc::vec::Vec::as_mut_slice)
	///
	/// # API Differences
	///
	/// This method is renamed in order to emphasize the semantic distinction
	/// between borrowing the bit-vector contents, and borrowing the memory that
	/// implements the collection contents.
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let mut bv = bitvec![Msb0, u8; 0, 1, 0, 0, 1, 1, 0, 1];
	/// let raw = bv.as_mut_raw_slice();
	/// assert_eq!(raw, &[0x4D]);
	/// raw[0] = 0xD4;
	/// assert_eq!(bv, bits![1, 1, 0, 1, 0, 1, 0, 0]);
	/// ```
	#[inline]
	pub fn as_mut_raw_slice(&mut self) -> &mut [T] {
		unsafe {
			slice::from_raw_parts_mut(
				self.bitspan.address().to_mut(),
				self.bitspan.elements(),
			)
		}
	}

	/// Returns a raw pointer to the bit-vector’s buffer.
	///
	/// # Original
	///
	/// [`Vec::as_ptr`](alloc::vec::Vec::as_ptr)
	///
	/// # API Differences
	///
	/// This method is renamed in order to emphasize the semantic distinction
	/// between taking a pointer to the start of the bit-vector contents, and
	/// taking a pointer to the underlying memory that implements the collection
	/// contents.
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let bv = bitvec![Msb0, u8; 0, 1, 0, 0, 1];
	/// let addr = bv.as_raw_ptr();
	/// ```
	#[inline]
	pub fn as_raw_ptr(&self) -> *const T {
		self.bitspan.address().to_const()
	}

	/// Returns an unsafe mutable pointer to the bit-vector’s buffer.
	///
	/// # Original
	///
	/// [`Vec::as_mut_ptr`](alloc::vec::Vec::as_mut_ptr)
	///
	/// # API Differences
	///
	/// This method is renamed in order to emphasize the semantic distinction
	/// between taking a pointer to the start of the bit-vector contents, and
	/// taking a pointer to the underlying memory that implements the collection
	/// contents.
	///
	/// # Examples
	///
	/// ```rust
	/// use bitvec::prelude::*;
	///
	/// let mut bv = bitvec![0, 1, 0, 0, 1];
	/// let addr = bv.as_mut_raw_ptr();
	/// ```
	#[inline]
	pub fn as_mut_raw_ptr(&mut self) -> *mut T {
		self.bitspan.address().to_mut()
	}

	/// Construct a `BitVec` from its exact fields, rather than using a formal
	/// constructor.
	///
	/// This is used for handle construction elsewhere in the crate, where a
	/// vector allocation and `BitSpan` descriptor exist, and need to be bundled
	/// into a `BitVec` without going through the ordinary construction process.
	///
	/// # Parameters
	///
	/// - `bitspan`: A span descriptor.
	/// - `capacity`: An allocation capacity, measured in `T` elements rather
	///   than in bits.
	///
	/// # Returns
	///
	/// `BitVec { bitspan, capacity }`
	///
	/// # Safety
	///
	/// The arguments must be derived from a known-good buffer allocation and
	/// span description. They will be directly used to construct the returned
	/// bit-vector, and drive all future memory access and allocation control.
	pub(crate) unsafe fn from_fields(
		bitspan: BitSpan<Mut, O, T>,
		capacity: usize,
	) -> Self {
		Self { bitspan, capacity }
	}

	/// Removes the `::Unalias` marker from a bit-vector’s type signature.
	fn strip_unalias(this: BitVec<O, T::Unalias>) -> Self {
		let (bitspan, capacity) = (this.bitspan.cast::<T>(), this.capacity);
		core::mem::forget(this);
		Self { bitspan, capacity }
	}

	/// Combines the logic for `BitVec::reserve` and `BitVec::reserve_exact`.
	#[inline]
	fn do_reservation(
		&mut self,
		additional: usize,
		func: impl FnOnce(&mut Vec<T>, usize),
	) {
		let len = self.len();
		let new_len = len
			.checked_add(additional)
			.expect("Bit-Vector capacity exceeded");
		assert!(
			new_len <= BitSlice::<O, T>::MAX_BITS,
			"Bit-Vector capacity exceeded: {} > {}",
			new_len,
			BitSlice::<O, T>::MAX_BITS,
		);
		let bitspan = self.bitspan;
		let head = bitspan.head();
		let elts = bitspan.elements();
		let new_elts = crate::mem::elts::<T>(head.value() as usize + new_len);
		let extra = new_elts - elts;
		self.with_vec(|vec| {
			func(&mut **vec, extra);
			//  Initialize any newly-allocated elements to zero, without
			//  initializing leftover dead capacity.
			vec.resize_with(new_elts, || unsafe { mem::zeroed() });
		});
	}

	/// Permits manipulation of the underlying vector allocation.
	///
	/// The caller receives a mutable borrow of a `Vec<T>` with its destructor
	/// disarmed. The caller may modify the buffer controls, including its
	/// location and its capacity, and these changes will be committed back into
	/// `self`. Modifications to the referent `[T]` handle, such as length
	/// changes, will not be preserved.
	fn with_vec<F, R>(&mut self, func: F) -> R
	where F: FnOnce(&mut ManuallyDrop<Vec<T>>) -> R {
		let capacity = self.capacity;
		let (ptr, length) =
			(self.bitspan.address().to_mut(), self.bitspan.elements());

		let mut vec = unsafe { Vec::from_raw_parts(ptr, length, capacity) }
			.pipe(ManuallyDrop::new);
		let out = func(&mut vec);

		unsafe {
			self.bitspan.set_address(vec.as_mut_ptr());
		}
		self.capacity = vec.capacity();
		out
	}
}

mod api;
mod iter;
mod ops;
mod traits;

pub use self::iter::{
	Drain,
	IntoIter,
	Splice,
};

#[cfg(test)]
mod tests;