Skip to content

Commit ed3b3be

Browse files
mkroeningwrenger
authored andcommitted
Document custom representations and endianness
Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
1 parent bf90072 commit ed3b3be

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ proc-macro = true
1818
quote = "1.0"
1919
syn = { version = "2.0", features = ["full"] }
2020
proc-macro2 = "1.0"
21+
22+
[dev-dependencies]
23+
endian-num = { version = "0.1", features = ["linux-types"] }

README.md

+54
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,60 @@ let my_byte_msb = MyMsbByte::new()
314314
assert!(my_byte_msb.0 == 0b1010_0_10_1);
315315
```
316316

317+
## Custom Representation and Endianness
318+
319+
The macro supports custom types for the representation of the bitfield struct.
320+
This can be an endian-defining type like in the following examples (from [`endian-num`]) or any other struct that can be converted to and from the main bitfield type.
321+
322+
The representation and its conversion functions can be specified with the following `#[bitfield]` parameters:
323+
- `repr` specifies the bitfield's representation in memory
324+
- `from` to specify a conversion function from repr to the bitfield's integer type
325+
- `into` to specify a conversion function from the bitfield's integer type to repr
326+
327+
[`endian-num`]: https://docs.rs/endian-num
328+
329+
This example has a little-endian byte order even on big-endian machines:
330+
331+
```rust
332+
use bitfield_struct::bitfield;
333+
use endian_num::le16;
334+
335+
#[bitfield(u16, repr = le16, from = le16::from_ne, into = le16::to_ne)]
336+
struct MyLeBitfield {
337+
#[bits(4)]
338+
first_nibble: u8,
339+
#[bits(12)]
340+
other: u16,
341+
}
342+
343+
let my_be_bitfield = MyLeBitfield::new()
344+
.with_first_nibble(0x1)
345+
.with_other(0x234);
346+
347+
assert_eq!(my_be_bitfield.into_bits().to_le_bytes(), [0x41, 0x23]);
348+
```
349+
350+
This example has a big-endian byte order even on little-endian machines:
351+
352+
```rust
353+
use bitfield_struct::bitfield;
354+
use endian_num::be16;
355+
356+
#[bitfield(u16, repr = be16, from = be16::from_ne, into = be16::to_ne)]
357+
struct MyBeBitfield {
358+
#[bits(4)]
359+
first_nibble: u8,
360+
#[bits(12)]
361+
other: u16,
362+
}
363+
364+
let my_be_bitfield = MyBeBitfield::new()
365+
.with_first_nibble(0x1)
366+
.with_other(0x234);
367+
368+
assert_eq!(my_be_bitfield.into_bits().to_be_bytes(), [0x23, 0x41]);
369+
```
370+
317371
## `fmt::Debug` and `Default`
318372

319373
This macro automatically creates a suitable `fmt::Debug` and `Default` implementations similar to the ones created for normal structs by `#[derive(Debug, Default)]`.

0 commit comments

Comments
 (0)