Coverage Report

Created: 2025-07-02 06:18

/rust/registry/src/index.crates.io-6f17d22bba15001f/bitvec-1.0.1/src/view.rs
Line
Count
Source (jump to first uncovered line)
1
#![doc = include_str!("../doc/view.md")]
2
3
use core::slice;
4
5
use crate::{
6
  array::BitArray,
7
  order::BitOrder,
8
  ptr::BitSpanError,
9
  slice::BitSlice,
10
  store::BitStore,
11
};
12
13
#[doc = include_str!("../doc/view/BitView.md")]
14
pub trait BitView {
15
  /// The underlying element type.
16
  type Store: BitStore;
17
18
  /// Views a memory region as an immutable bit-slice.
19
  fn view_bits<O>(&self) -> &BitSlice<Self::Store, O>
20
  where O: BitOrder;
21
22
  /// Attempts to view a memory region as an immutable bit-slice.
23
  ///
24
  /// This may return an error if `self` is too long to view as a bit-slice.
25
  fn try_view_bits<O>(
26
    &self,
27
  ) -> Result<&BitSlice<Self::Store, O>, BitSpanError<Self::Store>>
28
  where O: BitOrder;
29
30
  /// Views a memory region as a mutable bit-slice.
31
  fn view_bits_mut<O>(&mut self) -> &mut BitSlice<Self::Store, O>
32
  where O: BitOrder;
33
34
  /// Attempts to view a memory region as a mutable bit-slice.
35
  ///
36
  /// This may return an error if `self` is too long to view as a bit-slice.
37
  fn try_view_bits_mut<O>(
38
    &mut self,
39
  ) -> Result<&mut BitSlice<Self::Store, O>, BitSpanError<Self::Store>>
40
  where O: BitOrder;
41
}
42
43
#[cfg(not(tarpaulin_include))]
44
impl<T> BitView for T
45
where T: BitStore
46
{
47
  type Store = Self;
48
49
0
  fn view_bits<O>(&self) -> &BitSlice<T, O>
50
0
  where O: BitOrder {
51
0
    BitSlice::from_element(self)
52
0
  }
53
54
0
  fn try_view_bits<O>(&self) -> Result<&BitSlice<T, O>, BitSpanError<T>>
55
0
  where O: BitOrder {
56
0
    Ok(BitSlice::from_element(self))
57
0
  }
58
59
0
  fn view_bits_mut<O>(&mut self) -> &mut BitSlice<T, O>
60
0
  where O: BitOrder {
61
0
    BitSlice::from_element_mut(self)
62
0
  }
63
64
0
  fn try_view_bits_mut<O>(
65
0
    &mut self,
66
0
  ) -> Result<&mut BitSlice<T, O>, BitSpanError<T>>
67
0
  where O: BitOrder {
68
0
    Ok(BitSlice::from_element_mut(self))
69
0
  }
70
}
71
72
/// Note that overly-large slices may cause the conversions to fail.
73
#[cfg(not(tarpaulin_include))]
74
impl<T> BitView for [T]
75
where T: BitStore
76
{
77
  type Store = T;
78
79
  #[inline]
80
0
  fn view_bits<O>(&self) -> &BitSlice<T, O>
81
0
  where O: BitOrder {
82
0
    BitSlice::from_slice(self)
83
0
  }
84
85
  #[inline]
86
0
  fn try_view_bits<O>(&self) -> Result<&BitSlice<T, O>, BitSpanError<T>>
87
0
  where O: BitOrder {
88
0
    BitSlice::try_from_slice(self)
89
0
  }
90
91
  #[inline]
92
0
  fn view_bits_mut<O>(&mut self) -> &mut BitSlice<T, O>
93
0
  where O: BitOrder {
94
0
    BitSlice::from_slice_mut(self)
95
0
  }
96
97
  #[inline]
98
0
  fn try_view_bits_mut<O>(
99
0
    &mut self,
100
0
  ) -> Result<&mut BitSlice<T, O>, BitSpanError<T>>
101
0
  where O: BitOrder {
102
0
    BitSlice::try_from_slice_mut(self)
103
0
  }
104
}
105
106
/// Note that overly-large arrays may cause the conversions to fail.
107
#[cfg(not(tarpaulin_include))]
108
impl<T, const N: usize> BitView for [T; N]
109
where T: BitStore
110
{
111
  type Store = T;
112
113
  #[inline]
114
65.8k
  fn view_bits<O>(&self) -> &BitSlice<T, O>
115
65.8k
  where O: BitOrder {
116
65.8k
    BitSlice::from_slice(self)
117
65.8k
  }
<[u8; 16] as bitvec::view::BitView>::view_bits::<bitvec::order::Msb0>
Line
Count
Source
114
42.9k
  fn view_bits<O>(&self) -> &BitSlice<T, O>
115
42.9k
  where O: BitOrder {
116
42.9k
    BitSlice::from_slice(self)
117
42.9k
  }
<[u8; 1] as bitvec::view::BitView>::view_bits::<bitvec::order::Msb0>
Line
Count
Source
114
4.43k
  fn view_bits<O>(&self) -> &BitSlice<T, O>
115
4.43k
  where O: BitOrder {
116
4.43k
    BitSlice::from_slice(self)
117
4.43k
  }
<[u8; 2] as bitvec::view::BitView>::view_bits::<bitvec::order::Msb0>
Line
Count
Source
114
18.4k
  fn view_bits<O>(&self) -> &BitSlice<T, O>
115
18.4k
  where O: BitOrder {
116
18.4k
    BitSlice::from_slice(self)
117
18.4k
  }
Unexecuted instantiation: <[_; _] as bitvec::view::BitView>::view_bits::<_>
118
119
  #[inline]
120
0
  fn try_view_bits<O>(&self) -> Result<&BitSlice<T, O>, BitSpanError<T>>
121
0
  where O: BitOrder {
122
0
    BitSlice::try_from_slice(self)
123
0
  }
124
125
  #[inline]
126
79.2k
  fn view_bits_mut<O>(&mut self) -> &mut BitSlice<T, O>
127
79.2k
  where O: BitOrder {
128
79.2k
    BitSlice::from_slice_mut(self)
129
79.2k
  }
<[u8; 16] as bitvec::view::BitView>::view_bits_mut::<bitvec::order::Msb0>
Line
Count
Source
126
60.6k
  fn view_bits_mut<O>(&mut self) -> &mut BitSlice<T, O>
127
60.6k
  where O: BitOrder {
128
60.6k
    BitSlice::from_slice_mut(self)
129
60.6k
  }
<[u8; 2] as bitvec::view::BitView>::view_bits_mut::<bitvec::order::Msb0>
Line
Count
Source
126
18.6k
  fn view_bits_mut<O>(&mut self) -> &mut BitSlice<T, O>
127
18.6k
  where O: BitOrder {
128
18.6k
    BitSlice::from_slice_mut(self)
129
18.6k
  }
Unexecuted instantiation: <[_; _] as bitvec::view::BitView>::view_bits_mut::<_>
130
131
  #[inline]
132
0
  fn try_view_bits_mut<O>(
133
0
    &mut self,
134
0
  ) -> Result<&mut BitSlice<T, O>, BitSpanError<T>>
135
0
  where O: BitOrder {
136
0
    BitSlice::try_from_slice_mut(self)
137
0
  }
138
}
139
140
/// Helper trait for scalars and arrays, but not slices.
141
pub trait BitViewSized: BitView + Sized {
142
  /// The zero constant.
143
  const ZERO: Self;
144
145
  /// Wraps `self` in a `BitArray`.
146
  #[inline]
147
0
  fn into_bitarray<O>(self) -> BitArray<Self, O>
148
0
  where O: BitOrder {
149
0
    BitArray::new(self)
150
0
  }
151
152
  /// Views the type as a slice of its elements.
153
  fn as_raw_slice(&self) -> &[Self::Store];
154
155
  /// Views the type as a mutable slice of its elements.
156
  fn as_raw_mut_slice(&mut self) -> &mut [Self::Store];
157
}
158
159
impl<T> BitViewSized for T
160
where T: BitStore
161
{
162
  const ZERO: Self = <T as BitStore>::ZERO;
163
164
  #[inline]
165
0
  fn as_raw_slice(&self) -> &[Self::Store] {
166
0
    slice::from_ref(self)
167
0
  }
168
169
  #[inline]
170
0
  fn as_raw_mut_slice(&mut self) -> &mut [Self::Store] {
171
0
    slice::from_mut(self)
172
0
  }
173
}
174
175
impl<T, const N: usize> BitViewSized for [T; N]
176
where T: BitStore
177
{
178
  const ZERO: Self = [T::ZERO; N];
179
180
  #[inline]
181
0
  fn as_raw_slice(&self) -> &[Self::Store] {
182
0
    &self[..]
183
0
  }
184
185
  #[inline]
186
0
  fn as_raw_mut_slice(&mut self) -> &mut [Self::Store] {
187
0
    &mut self[..]
188
0
  }
189
}
190
191
#[doc = include_str!("../doc/view/AsBits.md")]
192
pub trait AsBits<T>
193
where T: BitStore
194
{
195
  /// Views `self` as an immutable bit-slice region with the `O` ordering.
196
  fn as_bits<O>(&self) -> &BitSlice<T, O>
197
  where O: BitOrder;
198
199
  /// Attempts to view `self` as an immutable bit-slice region with the `O`
200
  /// ordering.
201
  ///
202
  /// This may return an error if `self` is too long to view as a bit-slice.
203
  fn try_as_bits<O>(&self) -> Result<&BitSlice<T, O>, BitSpanError<T>>
204
  where O: BitOrder;
205
}
206
207
#[doc = include_str!("../doc/view/AsMutBits.md")]
208
pub trait AsMutBits<T>
209
where T: BitStore
210
{
211
  /// Views `self` as a mutable bit-slice region with the `O` ordering.
212
  fn as_mut_bits<O>(&mut self) -> &mut BitSlice<T, O>
213
  where O: BitOrder;
214
215
  /// Attempts to view `self` as a mutable bit-slice region with the `O`
216
  /// ordering.
217
  ///
218
  /// This may return an error if `self` is too long to view as a bit-slice.
219
  fn try_as_mut_bits<O>(
220
    &mut self,
221
  ) -> Result<&mut BitSlice<T, O>, BitSpanError<T>>
222
  where O: BitOrder;
223
}
224
225
#[cfg(not(tarpaulin_include))]
226
impl<A, T> AsBits<T> for A
227
where
228
  A: AsRef<[T]>,
229
  T: BitStore,
230
{
231
  #[inline]
232
0
  fn as_bits<O>(&self) -> &BitSlice<T, O>
233
0
  where O: BitOrder {
234
0
    self.as_ref().view_bits::<O>()
235
0
  }
236
237
  #[inline]
238
0
  fn try_as_bits<O>(&self) -> Result<&BitSlice<T, O>, BitSpanError<T>>
239
0
  where O: BitOrder {
240
0
    self.as_ref().try_view_bits::<O>()
241
0
  }
242
}
243
244
#[cfg(not(tarpaulin_include))]
245
impl<A, T> AsMutBits<T> for A
246
where
247
  A: AsMut<[T]>,
248
  T: BitStore,
249
{
250
  #[inline]
251
0
  fn as_mut_bits<O>(&mut self) -> &mut BitSlice<T, O>
252
0
  where O: BitOrder {
253
0
    self.as_mut().view_bits_mut::<O>()
254
0
  }
255
256
  #[inline]
257
0
  fn try_as_mut_bits<O>(
258
0
    &mut self,
259
0
  ) -> Result<&mut BitSlice<T, O>, BitSpanError<T>>
260
0
  where O: BitOrder {
261
0
    self.as_mut().try_view_bits_mut::<O>()
262
0
  }
263
}
264
265
#[cfg(test)]
266
mod tests {
267
  use static_assertions::*;
268
269
  use super::*;
270
  use crate::prelude::*;
271
272
  #[test]
273
  fn implementations() {
274
    let mut byte = 0u8;
275
    let mut bytes = [0u8; 2];
276
    assert!(byte.view_bits::<LocalBits>().not_any());
277
    assert!(byte.view_bits_mut::<LocalBits>().not_any());
278
    assert!(bytes.view_bits::<LocalBits>().not_any());
279
    assert!(bytes.view_bits_mut::<LocalBits>().not_any());
280
    assert!(bytes[..].view_bits::<LocalBits>().not_any());
281
    assert!(bytes[..].view_bits_mut::<LocalBits>().not_any());
282
283
    let mut blank: [u8; 0] = [];
284
    assert!(blank.view_bits::<LocalBits>().is_empty());
285
    assert!(blank.view_bits_mut::<LocalBits>().is_empty());
286
287
    assert_eq!([0u32; 2].as_bits::<LocalBits>().len(), 64);
288
    assert_eq!([0u32; 2].as_mut_bits::<LocalBits>().len(), 64);
289
290
    assert_eq!(0usize.as_raw_slice().len(), 1);
291
    assert_eq!(0usize.as_raw_mut_slice().len(), 1);
292
    assert_eq!(0u32.into_bitarray::<LocalBits>().len(), 32);
293
294
    assert_impl_all!(
295
      [usize; 10]: AsBits<usize>,
296
      AsMutBits<usize>,
297
      BitViewSized,
298
      BitView,
299
    );
300
  }
301
}