proc_bitfield/traits.rs
1mod arr_impls;
2mod int_impls;
3
4pub trait Bitfield {
5 type Storage;
6
7 fn from_storage(storage: Self::Storage) -> Self;
8 fn into_storage(self) -> Self::Storage;
9
10 fn storage(&self) -> &Self::Storage;
11 fn storage_mut(&mut self) -> &mut Self::Storage;
12}
13
14/// Read a range of bits inside a value.
15pub trait Bits<T> {
16 /// Read `self`'s `START..END` bit range (with `END` excluded) as a value of type `T`.
17 fn bits<const START: usize, const END: usize>(&self) -> T;
18}
19
20/// Return a value with a range of bits modified.
21pub trait WithBits<T> {
22 #[must_use]
23 /// Returns `self` with the the `START..END` bit range (with `END` excluded) set to the given
24 /// value of type `T`.
25 fn with_bits<const START: usize, const END: usize>(self, value: T) -> Self;
26}
27
28/// Modify a range of bits inside a value in place.
29pub trait SetBits<T> {
30 /// Sets `self`'s `START..END` bit range (with `END` excluded) to the given value of type `T`
31 /// in place.
32 fn set_bits<const START: usize, const END: usize>(&mut self, value: T);
33}
34
35/// Read a single bit inside a value.
36pub trait Bit {
37 /// Read `self`'s specified bit.
38 fn bit<const BIT: usize>(&self) -> bool;
39}
40
41/// Return a value with a single bit modified.
42pub trait WithBit {
43 /// Returns `self` with the the specified bit set to 1 if `value` is `true`, and 0 otherwise.
44 #[must_use]
45 fn with_bit<const BIT: usize>(self, value: bool) -> Self;
46}
47
48/// Modify a single bit inside a value in place.
49pub trait SetBit {
50 /// Sets `self`'s specified bit to 1 if `value` is `true`, and 0 otherwise.
51 fn set_bit<const BIT: usize>(&mut self, value: bool);
52}