4
4
[ ![ API] ( https://docs.rs/bitfield-struct/badge.svg )] ( https://docs.rs/bitfield-struct )
5
5
6
6
Procedural macro for bitfields that allows specifying bitfields as structs.
7
- As this library provides a procedural- macro it has no runtime dependencies and works for ` no-std ` .
7
+ As this library provides a procedural macro, it has no runtime dependencies and works for ` no-std ` .
8
8
9
9
## Usage
10
10
@@ -43,9 +43,10 @@ struct PageTableEntry {
43
43
The macro generates three accessor functions for each field.
44
44
Each accessor also inherits the documentation of its field.
45
45
46
- The signatures for ` addr ` for example are:
46
+ The signatures for ` addr ` , for example, are:
47
47
48
48
``` rust
49
+ // generated struct
49
50
struct PageTableEntry (u64 );
50
51
impl PageTableEntry {
51
52
const fn new () -> Self { /* ... */ }
@@ -54,25 +55,28 @@ impl PageTableEntry {
54
55
const fn addr (& self ) -> u32 { /* ... */ }
55
56
fn set_addr (& mut self , value : u32 ) { /* ... */ }
56
57
57
- // other members ...
58
+ // other fields ...
58
59
}
60
+ // generated trait implementations
59
61
impl From <u64 > for PageTableEntry { /* ... */ }
60
62
impl From <PageTableEntry > for u64 { /* ... */ }
63
+ impl Debug for PageTableEntry { /* ... */ }
61
64
```
62
65
63
- This generated bitfield then can be used as follows.
66
+ This generated bitfield can then be used as follows.
64
67
65
68
``` rust
66
- let pte = PageTableEntry :: new ()
69
+ let mut pte = PageTableEntry :: new ()
67
70
. with_addr (3 << 31 )
68
71
. with_size (2 )
69
72
. with_present (false )
70
73
. with_negative (- 3 );
71
74
72
- println! (" {}" , pte . addr ());
75
+ println! (" {pte:?}" );
76
+ assert! (pte . addr () == 3 << 31 );
73
77
74
78
pte . set_size (1 );
75
79
76
80
let value : u64 = pte . into ();
77
- println! (" {:b}" , value );
81
+ println! (" {value :b}" );
78
82
```
0 commit comments