-
Notifications
You must be signed in to change notification settings - Fork 107
/
bits.rs
133 lines (120 loc) · 2.89 KB
/
bits.rs
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
/*! Permit use of Rust native types as bit collections.
This module exposes a trait, `AsBits`, which functions similarly to the `AsRef`
and `AsMut` traits in the standard library. This trait allows an implementor to
express the means by which it can be interpreted as a collection of bits.
!*/
use crate::{
order::BitOrder,
slice::BitSlice,
store::BitStore,
};
/** Allows a type to be used as a sequence of immutable bits.
# Requirements
This trait can only be implemented by contiguous structures: individual
fundamentals, and sequences (arrays or slices) of them.
**/
pub trait AsBits {
/// The underlying fundamental type of the implementor.
type Store: BitStore;
/// Constructs a `BitSlice` reference over data.
///
/// # Type Parameters
///
/// - `O: BitOrder`: The `BitOrder` type used to index within the slice.
///
/// # Parameters
///
/// - `&self`
///
/// # Returns
///
/// A `BitSlice` handle over `self`’s data, using the provided `BitOrder`
/// type and using `Self::Store` as the data type.
///
/// # Examples
///
/// ```rust
/// use bitvec::prelude::*;
///
/// let src = 8u8;
/// let bits = src.bits::<Msb0>();
/// assert!(bits[4]);
/// ```
fn bits<O>(&self) -> &BitSlice<O, Self::Store>
where O: BitOrder;
/// Constructs a mutable `BitSlice` reference over data.
///
/// # Type Parameters
///
/// - `O: BitOrder`: The `BitOrder` type used to index within the slice.
///
/// # Parameters
///
/// - `&mut self`
///
/// # Returns
///
/// A `BitSlice` handle over `self`’s data, using the provided `BitOrder`
/// type and using `Self::Store` as the data type.
///
/// # Examples
///
/// ```rust
/// use bitvec::prelude::*;
///
/// let mut src = 8u8;
/// let bits = src.bits_mut::<Lsb0>();
/// assert!(bits[3]);
/// *bits.at(3) = false;
/// assert!(!bits[3]);
/// ```
fn bits_mut<O>(&mut self) -> &mut BitSlice<O, Self::Store>
where O: BitOrder;
}
impl<T> AsBits for T
where T: BitStore {
type Store = T;
fn bits<O>(&self) -> &BitSlice<O, T>
where O: BitOrder {
BitSlice::from_element(self)
}
fn bits_mut<O>(&mut self) -> &mut BitSlice<O, T>
where O: BitOrder {
BitSlice::from_element_mut(self)
}
}
impl<T> AsBits for [T]
where T: BitStore {
type Store = T;
fn bits<O>(&self) -> &BitSlice<O, T>
where O: BitOrder {
BitSlice::from_slice(self)
}
fn bits_mut<O>(&mut self) -> &mut BitSlice<O, T>
where O: BitOrder {
BitSlice::from_slice_mut(self)
}
}
macro_rules! impl_bits_for {
($( $n:expr ),* ) => { $(
impl<T> AsBits for [T; $n]
where T: BitStore {
type Store = T;
fn bits<O>(&self) -> &BitSlice<O, T>
where O: BitOrder {
BitSlice::from_slice(self)
}
fn bits_mut<O>(&mut self) -> &mut BitSlice<O, T>
where O: BitOrder {
BitSlice::from_slice_mut(self)
}
}
)* };
}
impl_bits_for![
0, 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
];