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

Closed
JelteF opened this issue Sep 8, 2024 · 0 comments · Fixed by #395
Closed

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

JelteF opened this issue Sep 8, 2024 · 0 comments · Fixed by #395
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
tyranron added a commit that referenced this issue Jan 3, 2025
…, #411)

- fix top-level `#[display("...")]` attribute on an enum being incorrectly treated as transparent or wrapping
- make top-level `#[display("...")]` attribute on an enum to have defaulting behavior instead of replacing when no wrapping is possible (no `_variant` placeholder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants