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 try_into instead of into #47

Open
andreasWallnerIFX opened this issue Jul 24, 2024 · 0 comments
Open

Support for try_into instead of into #47

andreasWallnerIFX opened this issue Jul 24, 2024 · 0 comments

Comments

@andreasWallnerIFX
Copy link

andreasWallnerIFX commented Jul 24, 2024

If I understand the current API correctly we can do conversion of bitfield fields via specifying into T in the field specification. What I'm currently missing is a way to use try_into instead of into.

With my use-case it would look like this (in our normal case I'd use num_enum, removed that for simplicity):

#[repr(u8)]
pub enum UidSize {
    Single = 0,
    Double = 1,
    Triple = 2,
}
impl TryFrom<u8> for UidSize {
    type Error = ();

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(UidSize::Single),
            1 => Ok(UidSize::Double),
            2 => Ok(UidSize::Triple),
            _ => Err(()),
        }
    }
}

bitfield! {
  pub struct Atqa(u16);
  impl Debug;
  // The fields default to u16
  pub bitframe_anticoll, set_bitframe_anticoll: 4, 0;
  pub u8, from try_into UidSize, uid_size, set_uid_size: 7, 6;
}

and the uid_size getter would have to have a Result<UidSize, ()> return value.

Is this currently possible and I overlooked it? Otherwise it would be cool if this could be added...

@andreasWallnerIFX andreasWallnerIFX changed the title Support for t Support for try_into instead of into Jul 24, 2024
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