Skip to content

TryFrom implementation for repr tagged enums assist #22273

@TheNeikos

Description

@TheNeikos

If you have an enum like:

#[repr(u8)]
enum Data {
  Ok = 0x00,
  Error = 0x01,
}

I think it would be handy to have it generate a TryFrom implementation. The error type could be set to (), it's easy to replace if need be.

Something like:

impl TryFrom<u8> for Data {
    type Error = ();
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0x00 => Ok(Self::Data),
            0x01 => Ok(Self::Error),
            _ => return Err(()),
        }
    }
}

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions