Crates.io | bit-by-bit |
lib.rs | bit-by-bit |
version | 0.1.0 |
source | src |
created_at | 2021-10-30 09:26:44.412053 |
updated_at | 2021-10-30 09:26:44.412053 |
description | Convenient proc-macro for defining structs with bitfields |
homepage | |
repository | |
max_upload_size | |
id | 474329 |
size | 25,290 |
This crates allow you to define structs bitfields and safely work with them.
Current limitations:
use bit_by_bit::bit_by_bit;
#[bit_by_bit]
#[derive(Default)]
struct EthernetHeader {
#[bit(7)]
preamble: u8,
#[bit(1)]
sd: u8,
#[bit(6)]
dest: u8,
#[bit(6)]
src: u8,
#[bit(2)]
length: u8,
}
// Will expand to something like that:
#[derive(Default)]
struct EthernetHeader {
__base_field_0: u8,
__base_field_1: u8,
__base_field_2: u8,
__base_field_3: u8,
}
impl EthernetHeader {
fn preamble(&self) -> u8 {
self.__base_field_0 & (1 << 7) - 1
}
fn set_preamble(&mut self, val: u8) {
self.__base_field_0 ^= self.__base_field_0 & (1 << 7) - 1;
self.__base_field_0 |= val & (1 << 7) - 1;
}
fn sd(&self) -> u8 { /*impl*/ }
fn set_sd(&mut self, val: u8) { /*impl*/ }
fn dest(&self) -> u8 { /*impl*/ }
fn set_dest(&mut self, val: u8) { /*impl*/ }
fn src(&self) -> u8 { /*impl*/ }
fn set_src(&mut self, val: u8) { /*impl*/ }
fn length(&self) -> u8 { /*impl*/ }
fn set_length(&mut self, val: u8) { /*impl*/ }
}