/rust/registry/src/index.crates.io-6f17d22bba15001f/bitvec-1.0.1/src/array.rs
Line | Count | Source (jump to first uncovered line) |
1 | | #![doc = include_str!("../doc/array.md")] |
2 | | |
3 | | use core::marker::PhantomData; |
4 | | |
5 | | use crate::{ |
6 | | mem, |
7 | | order::{ |
8 | | BitOrder, |
9 | | Lsb0, |
10 | | }, |
11 | | slice::BitSlice, |
12 | | view::BitViewSized, |
13 | | }; |
14 | | |
15 | | mod api; |
16 | | mod iter; |
17 | | mod ops; |
18 | | mod tests; |
19 | | mod traits; |
20 | | |
21 | | pub use self::iter::IntoIter; |
22 | | |
23 | | #[repr(transparent)] |
24 | | #[doc = include_str!("../doc/array/BitArray.md")] |
25 | | pub struct BitArray<A = [usize; 1], O = Lsb0> |
26 | | where |
27 | | A: BitViewSized, |
28 | | O: BitOrder, |
29 | | { |
30 | | /// The ordering of bits within an `A::Store` element. |
31 | | pub _ord: PhantomData<O>, |
32 | | /// The wrapped data buffer. |
33 | | pub data: A, |
34 | | } |
35 | | |
36 | | impl<A, O> BitArray<A, O> |
37 | | where |
38 | | A: BitViewSized, |
39 | | O: BitOrder, |
40 | | { |
41 | | /// A bit-array with all bits initialized to zero. |
42 | | pub const ZERO: Self = Self { |
43 | | _ord: PhantomData, |
44 | | data: A::ZERO, |
45 | | }; |
46 | | |
47 | | /// Wraps an existing buffer as a bit-array. |
48 | | /// |
49 | | /// ## Examples |
50 | | /// |
51 | | /// ```rust |
52 | | /// use bitvec::prelude::*; |
53 | | /// |
54 | | /// let data = [0u16, 1, 2, 3]; |
55 | | /// let bits = BitArray::<_, Msb0>::new(data); |
56 | | /// assert_eq!(bits.len(), 64); |
57 | | /// ``` |
58 | | #[inline] |
59 | 3.41k | pub fn new(data: A) -> Self { |
60 | 3.41k | Self { data, ..Self::ZERO } |
61 | 3.41k | } <bitvec::array::BitArray<[u8; 1], bitvec::order::Msb0>>::new Line | Count | Source | 59 | 3.41k | pub fn new(data: A) -> Self { | 60 | 3.41k | Self { data, ..Self::ZERO } | 61 | 3.41k | } |
Unexecuted instantiation: <bitvec::array::BitArray<_, _>>::new |
62 | | |
63 | | /// Removes the bit-array wrapper, returning the contained buffer. |
64 | | /// |
65 | | /// ## Examples |
66 | | /// |
67 | | /// ```rust |
68 | | /// use bitvec::prelude::*; |
69 | | /// |
70 | | /// let bits = bitarr![0; 30]; |
71 | | /// let native: [usize; 1] = bits.into_inner(); |
72 | | /// ``` |
73 | | #[inline] |
74 | 0 | pub fn into_inner(self) -> A { |
75 | 0 | self.data |
76 | 0 | } |
77 | | |
78 | | /// Explicitly views the bit-array as a bit-slice. |
79 | | #[inline] |
80 | 5.55k | pub fn as_bitslice(&self) -> &BitSlice<A::Store, O> { |
81 | 5.55k | self.data.view_bits::<O>() |
82 | 5.55k | } <bitvec::array::BitArray<[u8; 1], bitvec::order::Msb0>>::as_bitslice Line | Count | Source | 80 | 5.55k | pub fn as_bitslice(&self) -> &BitSlice<A::Store, O> { | 81 | 5.55k | self.data.view_bits::<O>() | 82 | 5.55k | } |
Unexecuted instantiation: <bitvec::array::BitArray<_, _>>::as_bitslice |
83 | | |
84 | | /// Explicitly views the bit-array as a mutable bit-slice. |
85 | | #[inline] |
86 | 0 | pub fn as_mut_bitslice(&mut self) -> &mut BitSlice<A::Store, O> { |
87 | 0 | self.data.view_bits_mut::<O>() |
88 | 0 | } |
89 | | |
90 | | /// Views the bit-array as a slice of its underlying memory elements. |
91 | | #[inline] |
92 | 0 | pub fn as_raw_slice(&self) -> &[A::Store] { |
93 | 0 | self.data.as_raw_slice() |
94 | 0 | } |
95 | | |
96 | | /// Views the bit-array as a mutable slice of its underlying memory |
97 | | /// elements. |
98 | | #[inline] |
99 | 0 | pub fn as_raw_mut_slice(&mut self) -> &mut [A::Store] { |
100 | 0 | self.data.as_raw_mut_slice() |
101 | 0 | } |
102 | | |
103 | | /// Gets the length (in bits) of the bit-array. |
104 | | /// |
105 | | /// This method is a compile-time constant. |
106 | | #[inline] |
107 | 0 | pub fn len(&self) -> usize { |
108 | 0 | mem::bits_of::<A>() |
109 | 0 | } |
110 | | |
111 | | /// Tests whether the array is empty. |
112 | | /// |
113 | | /// This method is a compile-time constant. |
114 | | #[inline] |
115 | 0 | pub fn is_empty(&self) -> bool { |
116 | 0 | mem::bits_of::<A>() == 0 |
117 | 0 | } |
118 | | } |