Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for to_u32() conversion to u32 and similar #102

Open
hacknus opened this issue Sep 26, 2023 · 0 comments
Open

Support for to_u32() conversion to u32 and similar #102

hacknus opened this issue Sep 26, 2023 · 0 comments

Comments

@hacknus
Copy link

hacknus commented Sep 26, 2023

Hello
First of all; thank you for this very useful library!
I created the additional trait modular-bitfield-to-value ToValue which can be implemented for all structs annotated with #[bitfield].
It allows to then convert the struct to a u32 which can be useful for debugging.

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

We could keep this separate as is, then I'd push it to crates.io, but if you want, we could also implement it in the modular-bitfield crate (maybe through a feature)?

Let me know what you think!

EDIT:
If this would be implemented in modular-bitfield it would not need to be a trait, we could merely copy the functions over...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant