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

Failable conversion? #54

Open
ianthetechie opened this issue Oct 19, 2024 · 1 comment
Open

Failable conversion? #54

ianthetechie opened this issue Oct 19, 2024 · 1 comment

Comments

@ianthetechie
Copy link

Hey, thanks for the great crate!

It would be neat if there was a failable version, for cases when an enumeration with an integral repr doesn't use the full range (some bit patterns are invalid). I'm not sure if this fits within the goals of the crate, but thought I'd open the feature request anyways.

@wrenger
Copy link
Owner

wrenger commented Oct 29, 2024

Apologies for the delayed response. So you are looking for getters that return a result?

enum MyEnum {
    A = 1,
    B = 2,
    // others are not valid
}

#[bitfield(u8)]
struct Bitfield {
    #[bits(4)]
    my_data: MyEnum,
    #[bits(4)]
    other: u8,
}

assert_eq!(Ok(MyEnum::B), Bitfield(0xffff_0002).my_data_checked());
assert!(Bitfield(0xffff_ffff).my_data_checked().is_err());

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

2 participants