1
- # bit-struct # ![ no_std ]
1
+ # bit-struct
2
2
3
3
[ ![ crates.io] ( https://img.shields.io/crates/v/bit-struct.svg )] ( https://crates.io/crates/bit-struct )
4
4
[ ![ codecov] ( https://codecov.io/gh/andrewgazelka/bit-struct/branch/main/graph/badge.svg?token=60R82VBBVF )] ( https://codecov.io/gh/andrewgazelka/bit-struct )
5
- ![ Minimum rustc version] ( https://img.shields.io/badge/rustc-1.57.0 +-yellow.svg )
5
+ ![ Minimum rustc version] ( https://img.shields.io/badge/rustc-1.62.1 +-yellow.svg )
6
6
7
7
Bit struct is a crate which allows for ergonomic use of C-like bit fields without mediocre IDE support resulting from proc macros.
8
8
In addition, everything is statically typed checked!
@@ -14,12 +14,12 @@ use bit_struct::*;
14
14
15
15
enums! {
16
16
// 2 bits, i.e., 0b00, 0b01, 0b10
17
- HouseKind { Urban , Suburban , Rural }
17
+ pub HouseKind { Urban , Suburban , Rural }
18
18
}
19
19
20
20
bit_struct! {
21
21
// u8 is the base storage type. This can be any multiple of 8
22
- struct HouseConfig (u8 ) {
22
+ pub struct HouseConfig (u8 ) {
23
23
// 2 bits
24
24
kind : HouseKind ,
25
25
@@ -30,47 +30,58 @@ bit_struct! {
30
30
highest_floor : u2 ,
31
31
}
32
32
}
33
- ```
34
33
35
- We can create a new ` HouseConfig ` like such:
36
- ``` rust
34
+ // We can create a new `HouseConfig` like such:
35
+ // where all numbers are statically checked to be in bounds.
37
36
let config = HouseConfig :: new (HouseKind :: Suburban , i3! (- 2 ), u2! (1 ));
38
- ```
39
- where all numbers are statically checked to be in bounds. We can get the
40
- raw ` u8 ` which represents ` config ` :
41
- ``` rust
37
+
38
+ // We can get the raw `u8` which represents `config`:
42
39
let raw : u8 = config . raw ();
43
- ```
44
- or we can get a ` HouseConfig ` from a ` u8 ` like
45
- ``` rust
46
- let config : HouseConfig = HouseConfig :: try_from (123_u8 ). unwrap ();
47
- ```
48
- We need to unwrap because ` HouseConfig ` is not valid for all numbers. For instance, if the
49
- most significant bits are ` 0b11 ` , it encodes an invalid ` HouseKind ` . However,
50
- if all elements of a struct are always valid (suppose we removed the ` kind ` field), the struct will
51
- auto implement a trait which allows calling
52
- ``` rust
53
- let config : HouseConfig = HouseConfig :: exact_from (123_u8 );
54
- ```
55
- which will never panic. We can access values of ` config ` like so:
56
- ``` rust
57
- // get a value
40
+ assert_eq! (114_u8 , raw );
41
+
42
+ // or we can get a `HouseConfig` from a `u8` like:
43
+ let mut config : HouseConfig = HouseConfig :: try_from (114_u8 ). unwrap ();
44
+ assert_eq! (config , HouseConfig :: new (HouseKind :: Suburban , i3! (- 2 ), u2! (1 )));
45
+ // We need to unwrap because `HouseConfig` is not valid for all numbers. For instance, if the
46
+ // most significant bits are `0b11`, it encodes an invalid `HouseKind`. However,
47
+ // if all elements of a struct are always valid (suppose we removed the `kind` field), the struct will
48
+ // auto implement a trait which allows calling the non-panicking:
49
+ // let config: HouseConfig = HouseConfig::exact_from(123_u8);
50
+
51
+ // We can access values of `config` like so:
58
52
let kind : HouseKind = config . kind (). get ();
59
53
60
- // set a value
54
+ // And we can set values like so:
61
55
config . lowest_floor (). set (i3! (0 ));
62
- ```
63
- We can also access bit-level numbers as std-lib values like this
64
- ``` rust
56
+
57
+ // We can also convert the new numeric types for alternate bit-widths into the
58
+ // numeric types provided by the standard library:
65
59
let lowest_floor : i3 = config . lowest_floor (). get ();
66
60
let lowest_floor_std : i8 = lowest_floor . value ();
61
+ assert_eq! (lowest_floor_std , 0_i8 );
67
62
```
68
63
69
64
## Benefits
70
65
- No proc macros
71
66
- Autocompletion fully works (tested in IntelliJ Rust)
72
67
- Fast compile times
73
- - Statically checked bounds (structs cannot be over-filled)
68
+ - Statically checks that structs are not overfilled. For example, these overfilled structs will not compile:
69
+ ``` compile_fail
70
+ bit_struct::bit_struct! {
71
+ struct TooManyFieldsToFit(u16) {
72
+ a: u8,
73
+ b: u8,
74
+ c: bit_struct::u1
75
+ }
76
+ }
77
+ ```
78
+ ``` compile_fail
79
+ bit_struct::bit_struct! {
80
+ struct FieldIsTooBig(u16) {
81
+ a: u32
82
+ }
83
+ }
84
+ ```
74
85
- Statically checked types
75
86
76
87
## Further Documentation
0 commit comments