112a9d9c8Sopenharmony_ci# Using the Bitfield Types Generated by Bindgen 212a9d9c8Sopenharmony_ci 312a9d9c8Sopenharmony_ci## Bitfield Strategy Overview 412a9d9c8Sopenharmony_ci 512a9d9c8Sopenharmony_ciAs Rust does not support bitfields, Bindgen generates a struct for each with the following characteristics 612a9d9c8Sopenharmony_ci* Immutable getter functions for each bitfield named ```<bitfield>``` 712a9d9c8Sopenharmony_ci* Setter functions for each contiguous block of bitfields named ```set_<bitfield>``` 812a9d9c8Sopenharmony_ci* Far each contiguous block of bitfields, Bindgen emits an opaque physical field that contains one or more logical bitfields 912a9d9c8Sopenharmony_ci* A static constructor ```new_bitfield_{1, 2, ...}``` with a parameter for each bitfield contained within the opaque physical field. 1012a9d9c8Sopenharmony_ci 1112a9d9c8Sopenharmony_ci## Bitfield examples 1212a9d9c8Sopenharmony_ci 1312a9d9c8Sopenharmony_ciFor this discussion, we will use the following C type definitions and functions. 1412a9d9c8Sopenharmony_ci```c 1512a9d9c8Sopenharmony_citypedef struct { 1612a9d9c8Sopenharmony_ci unsigned int a: 1; 1712a9d9c8Sopenharmony_ci unsigned int b: 1; 1812a9d9c8Sopenharmony_ci unsigned int c: 2; 1912a9d9c8Sopenharmony_ci 2012a9d9c8Sopenharmony_ci} StructWithBitfields; 2112a9d9c8Sopenharmony_ci 2212a9d9c8Sopenharmony_ci// Create a default bitfield 2312a9d9c8Sopenharmony_ciStructWithBitfields create_bitfield(); 2412a9d9c8Sopenharmony_ci 2512a9d9c8Sopenharmony_ci// Print a bitfield 2612a9d9c8Sopenharmony_civoid print_bitfield(StructWithBitfields bfield); 2712a9d9c8Sopenharmony_ci``` 2812a9d9c8Sopenharmony_ci 2912a9d9c8Sopenharmony_ciBindgen creates a set of field getters and setters for interacting with the bitset. For example, 3012a9d9c8Sopenharmony_ci 3112a9d9c8Sopenharmony_ci```rust,ignore 3212a9d9c8Sopenharmony_ci let mut bfield = unsafe { create_bitfield() }; 3312a9d9c8Sopenharmony_ci 3412a9d9c8Sopenharmony_ci bfield.set_a(1); 3512a9d9c8Sopenharmony_ci println!("a set to {}", bfield.a()); 3612a9d9c8Sopenharmony_ci bfield.set_b(1); 3712a9d9c8Sopenharmony_ci println!("b set to {}", bfield.b()); 3812a9d9c8Sopenharmony_ci bfield.set_c(3); 3912a9d9c8Sopenharmony_ci println!("c set to {}", bfield.c()); 4012a9d9c8Sopenharmony_ci 4112a9d9c8Sopenharmony_ci unsafe { print_bitfield(bfield) }; 4212a9d9c8Sopenharmony_ci``` 4312a9d9c8Sopenharmony_ci 4412a9d9c8Sopenharmony_ciwill print out 4512a9d9c8Sopenharmony_ci 4612a9d9c8Sopenharmony_ci```text 4712a9d9c8Sopenharmony_cia set to 1 4812a9d9c8Sopenharmony_cib set to 1 4912a9d9c8Sopenharmony_cic set to 3 5012a9d9c8Sopenharmony_ciStructWithBitfields: a:1, b:1, c:3 5112a9d9c8Sopenharmony_ci``` 5212a9d9c8Sopenharmony_ci 5312a9d9c8Sopenharmony_ciOverflowing a bitfield will result in the same behavior as in C/C++: the bitfield will be set to 0. 5412a9d9c8Sopenharmony_ci 5512a9d9c8Sopenharmony_ci```rust,ignore 5612a9d9c8Sopenharmony_ci let mut bfield = unsafe { create_bitfield() }; 5712a9d9c8Sopenharmony_ci bfield.set_a(1); 5812a9d9c8Sopenharmony_ci bfield.set_b(1); 5912a9d9c8Sopenharmony_ci bfield.set_c(12); 6012a9d9c8Sopenharmony_ci println!("c set to {} due to overflow", bfield.c()); 6112a9d9c8Sopenharmony_ci 6212a9d9c8Sopenharmony_ci unsafe { print_bitfield(bfield) }; 6312a9d9c8Sopenharmony_ci``` 6412a9d9c8Sopenharmony_ci 6512a9d9c8Sopenharmony_ciwill print out 6612a9d9c8Sopenharmony_ci 6712a9d9c8Sopenharmony_ci```text 6812a9d9c8Sopenharmony_cic set to 0 due to overflow 6912a9d9c8Sopenharmony_ciStructWithBitfields: a:1, b:1, c:0 7012a9d9c8Sopenharmony_ci``` 7112a9d9c8Sopenharmony_ci 7212a9d9c8Sopenharmony_ciTo create a new bitfield in Rust, use the bitfield allocation unit constructor. 7312a9d9c8Sopenharmony_ci 7412a9d9c8Sopenharmony_ciNote: This requires the Builder's derive_default to be set to true, otherwise the necessary Default functions won't be generated. 7512a9d9c8Sopenharmony_ci 7612a9d9c8Sopenharmony_ci```rust,ignore 7712a9d9c8Sopenharmony_ci let bfield = StructWithBitfields{ 7812a9d9c8Sopenharmony_ci _bitfield_1: StructWithBitfields::new_bitfield_1(0,0,0), 7912a9d9c8Sopenharmony_ci ..Default::default() 8012a9d9c8Sopenharmony_ci }; 8112a9d9c8Sopenharmony_ci 8212a9d9c8Sopenharmony_ci unsafe { print_bitfield(bfield) }; 8312a9d9c8Sopenharmony_ci``` 8412a9d9c8Sopenharmony_ci 8512a9d9c8Sopenharmony_ciThis will print out 8612a9d9c8Sopenharmony_ci 8712a9d9c8Sopenharmony_ci```text 8812a9d9c8Sopenharmony_ciStructWithBitfields: a:0, b:0, c:0 8912a9d9c8Sopenharmony_ci``` 90