-
Notifications
You must be signed in to change notification settings - Fork 745
Added mdbook entry for bitfields. #1052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Using the Bitfield Types Generated by Bindgen | ||
|
||
## Bitfield Strategy Overview | ||
|
||
As Rust does not support bitfields, Bindgen generates a struct for each with the following characteristics | ||
* Immutable getter functions for each bitfield named ```<bitfield>``` | ||
* Setter functions for each contiguous bsock of bitfields named ```set_<bitfield>``` | ||
* Far each contiguous block of bitfields, Bindgen emits an opaque physical field that contains one or more logical bitfields | ||
* A static constructor ```new_bitfield_{1, 2, ...}``` with a parameter for each bitfield contained within the opaque physical field. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: s/for each bitfield/for each contiguous block of bitfields/ There is a constructor for each of those physical, opaque fields that we end up generating, which is roughly equivalent to sets of contiguous bitfields. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
## Bitfield examples | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. awesome 👍 |
||
|
||
For this discussion, we will use the following C type definitions and functions. | ||
```c | ||
typedef struct { | ||
unsigned int a: 1; | ||
unsigned int b: 1; | ||
unsigned int c: 2; | ||
|
||
} StructWithBitfields; | ||
|
||
// Create a default bitfield | ||
StructWithBitfields create_bitfield(); | ||
|
||
// Print a bitfield | ||
void print_bitfield(StructWithBitfields bfield); | ||
``` | ||
|
||
Bindgen creates a set of field getters and setters for interacting with the bitset. For example, | ||
|
||
```rust,ignore | ||
let mut bfield = unsafe { create_bitfield() }; | ||
|
||
bfield.set_a(1); | ||
println!("a set to {}", bfield.a()); | ||
bfield.set_b(1); | ||
println!("b set to {}", bfield.b()); | ||
bfield.set_c(3); | ||
println!("c set to {}", bfield.c()); | ||
|
||
unsafe { print_bitfield(bfield) }; | ||
``` | ||
|
||
will print out | ||
|
||
```text | ||
a set to 1 | ||
b set to 1 | ||
c set to 3 | ||
StructWithBitfields: a:1, b:1, c:3 | ||
``` | ||
|
||
Overflowing a bitfield will result in the same behavior as in C/C++: the bitfield will be set to 0. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it just happen to set to 0 in this case or is that really what the standard says should happen? My reading of the C++ standard doesn't come up with anything either way. Same with the C11 standard. I think we should leave overflow unspecified as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my googling Stackoverflow that's what the standard says: https://stackoverflow.com/questions/4908300/overflow-in-bit-fields#4908606 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! |
||
|
||
```rust,ignore | ||
let mut bfield = unsafe { create_bitfield() }; | ||
bfield.set_a(1); | ||
bfield.set_b(1); | ||
bfield.set_c(12); | ||
println!("c set to {} due to overflow", bfield.c()); | ||
|
||
unsafe { print_bitfield(bfield) }; | ||
``` | ||
|
||
will print out | ||
|
||
```text | ||
c set to 0 due to overflow | ||
StructWithBitfields: a:1, b:1, c:0 | ||
``` | ||
|
||
To create a new bitfield in Rust, use the bitfield allocation unit constructor. | ||
|
||
Note: This requires the Builder's derive_default to be set to true, otherwise the necessary Default functions won't be generated. | ||
|
||
```rust,ignore | ||
let bfield = StructWithBitfields{ | ||
_bitfield_1: StructWithBitfields::new_bitfield_1(0,0,0), | ||
..Default::default() | ||
}; | ||
|
||
unsafe { print_bitfield(bfield) }; | ||
``` | ||
|
||
This will print out | ||
|
||
```text | ||
StructWithBitfields: a:1, b:0, c:2 | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"bsock" -> "block"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!