You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Synopsis
See
#321 (comment):
> You’re discarding formatting flags provided by the user in format
string, e.g.:
>
> ```rust
> #[derive(derive_more::Display)]
> #[display(fmt = "{:?}", _0)]
> struct Num(usize);
>
> impl std::fmt::Debug for Num {
> fn fmt(&self, fmtr: &mut std::fmt::Formatter) -> std::fmt::Result {
> self.0.fmt(fmtr)
> }
> }
>
> fn main() {
> let num = Num(7);
> println!("{num:03?}"); // prints ‘007’ as expected
> println!("{num:03}"); // prints ‘7’ instead
> }
> ```
## Solution
See
#321 (comment):
> Theoretically, we can support this with the current syntax, because we
can detect so-called _trivial_ cases and transform them into delegation
(we do parse formatting string literal anyway).
>
> ```rust
> #[derive(derive_more::Display)]
> #[display("{_0:?}")] // <--- it's clear to be a trivial delegation
case
> struct Num(usize);
> ```
>
> would expand to
>
> ```rust
> impl std::fmt::Display for Num {
> fn fmt(&self, fmtr: &mut std::fmt::Formatter) -> std::fmt::Result {
> let _0 = &self.0;
> std::fmt::Debug::fmt(_0, fmtr)
> }
> }
> ```
>
> rather than
>
> ```rust
> impl std::fmt::Display for Num {
> fn fmt(&self, fmtr: &mut std::fmt::Formatter) -> std::fmt::Result {
> let _0 = &self.0;
> write!(fmtr, "{_0:?}")
> }
> }
> ```
0 commit comments