Skip to content

feat: Add ide-assist: generate_try_from_repr_for_enum#22279

Open
A4-Tacks wants to merge 2 commits intorust-lang:masterfrom
A4-Tacks:enum-tryfrom-repr
Open

feat: Add ide-assist: generate_try_from_repr_for_enum#22279
A4-Tacks wants to merge 2 commits intorust-lang:masterfrom
A4-Tacks:enum-tryfrom-repr

Conversation

@A4-Tacks
Copy link
Copy Markdown
Member

@A4-Tacks A4-Tacks commented May 4, 2026

Close #22273

Add a TryFrom<{repr}> impl for this enum.

Example

#[repr(u32)]
enum Foo {
    A = 1$0,
    B = 1 << 1,
}

->

#[repr(u32)]
enum Foo {
    A = 1,
    B = 1 << 1,
}

impl TryFrom<u32> for Foo {
    type Error = ();

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        match value {
            1 => Ok(Self::A),
            2 => Ok(Self::B),
            _ => Err(()),
        }
    }
}

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 4, 2026
Add a TryFrom<{repr}> impl for this enum.

Example
---
```rust
#[repr(u32)]
enum Foo {
    A = 1$0,
    B = 1 << 1,
}
```
->
```rust
#[repr(u32)]
enum Foo {
    A = 1,
    B = 1 << 1,
}

impl TryFrom<u32> for Foo {
    type Error = ();

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        match value {
            1 => Ok(Self::A),
            2 => Ok(Self::B),
            _ => Err(()),
        }
    }
}
```
@A4-Tacks A4-Tacks force-pushed the enum-tryfrom-repr branch from 5ef4648 to 2656666 Compare May 4, 2026 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TryFrom implementation for repr tagged enums assist

2 participants