Skip to content

A platform agnostic implementation for the modular-bitfield crate

License

Notifications You must be signed in to change notification settings

hacknus/modular-bitfield-to-value

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ToValue Trait for Modular Bitfields for Rust

crates.io Docs Rust

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 and u128 could be implemented...

Fully supported in #![no_std] environments.

Usage

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);
}

About

A platform agnostic implementation for the modular-bitfield crate

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages