Implementation based on the modular-bitfield crate.
Allows for the implementation of the to_u32()
function to convert structs that implement the Specifier trait to a value.
Features:
-
to_u32()
function to convert structs with length of max 4 bytes to u32 -
to_u16()
function to convert structs with length of max 2 bytes to u16 -
to_u8()
function to convert structs with length of max 1 byte to u8 -
to_bool()
function to convert structs with length of 1 bit to bool
Todo:
- implement little-endian conversion?
- maybe functions to return
u64
andu128
could be implemented...
Fully supported in #![no_std]
environments.
Add this to your cargo.toml
:
[dependencies]
modular-bitfield = "0.11.2"
modular-bitfield-to-value = { git = "https://github.com/hacknus/modular_bitfield_to_value" }
Add the following imports:
use modular_bitfield::bitfield;
use modular_bitfield_to_value::ToValue;
A basic example:
#[bitfield(bits = 32)]
#[derive(ToValue)]
pub struct Field {
pub test: u32,
}
{
let field = Field::from_bytes(0x100C3_u32.to_be_bytes());
let value = field.to_u32().unwrap();
assert!(0x100C3 == value, "constructed = {}, to_u32() = {}", 0x100C3, 0x100C3);
}