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

Change display on enum without _variant to "use as default" behaviour #411

Open
JelteF opened this issue Sep 8, 2024 · 0 comments
Open
Assignees
Milestone

Comments

@JelteF
Copy link
Owner

JelteF commented Sep 8, 2024

This should work:

#[derive(Display)]
#[display("{_0}")]
enum Enum<T> {
    #[display("A")]
    A(i32),
    #[display("B")]
    B(&'static str),
    #[display("C")]
    C(T),
}

fn main() {
    assert_eq!(Enum::<u8>::A(1).to_string(), "A");
    assert_eq!(Enum::<u8>::B("abc").to_string(), "B");
    assert_eq!(Enum::<u8>::C(9).to_string(), "C");
}

As well as:

#[derive(Display)]
#[display("some content: {_0}")]
enum Enum<T> {
    #[display("A")]
    A(i32),
    B(&'static str),
    C(T),
}

fn main() {
    assert_eq!(Enum::<u8>::A(1).to_string(), "A");
    assert_eq!(Enum::<u8>::B("abc").to_string(), "some content: abc");
    assert_eq!(Enum::<u8>::C(9).to_string(), "some content: 9");
}

Related to #395. Note that transparence checks should also work as expected.

@JelteF JelteF added this to the 2.0.0 milestone Sep 8, 2024
@tyranron tyranron self-assigned this Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants