rust-lang / rust-lang/rust-analyzer

TryFrom implementation for `repr` tagged enums assist

Open
#22,273 1 comment 2 reactions 1 assignee View on GitHub

@A4-Tacks is already working on this.

Since May 4, 2026.

A-assists C-feature
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

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(()),
        }
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.