frozenlib / parse-display

Procedural macro to implement Display and FromStr using common settings.
Apache License 2.0
182 stars 14 forks source link

#[display("{:?}")] could be better #35

Closed KalitaAlexey closed 9 months ago

KalitaAlexey commented 9 months ago
#[derive(Display)]
#[display("{:?}")]
enum E {
    A,
    B,
}

It generates the following Display::fmt:

match self {
    &Self::A=> f.write_fmt(format_args!("{0:?}", "A")),
    &Self::B=> f.write_fmt(format_args!("{0:?}", "B")),
}

I think "{:?}" should generate the following:

<Self as Debug>::fmt(self, f)
frozenlib commented 9 months ago

That's a good idea.

Indeed, there's no need to make traits other than Display available for variant names, and it would be convenient if formatting traits for Self could be made available.

frozenlib commented 9 months ago

I implemented this specification with de8a9431787053078ed706fc6ff53126c35a4acf.

There are some inefficiencies in the generated code, which I will eventually fix.

KalitaAlexey commented 9 months ago

Thank you.