-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
## 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:?}") > } > } > ```
- Loading branch information
Showing
11 changed files
with
2,374 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.