Skip to content

Commit

Permalink
Generate more concise code for pass-through format
Browse files Browse the repository at this point in the history
Fixes #13.
  • Loading branch information
dtolnay committed Oct 11, 2019
1 parent a6b1d28 commit 5994705
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions impl/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,21 @@ impl ToTokens for Display {
fn to_tokens(&self, tokens: &mut TokenStream) {
let fmt = &self.fmt;
let args = &self.args;
tokens.extend(quote! {
write!(formatter, #fmt #args)
});
if self.was_shorthand && fmt.value() == "{}" {
let arg = args.clone().into_iter().skip(1).next().unwrap();
tokens.extend(quote! {
std::fmt::Display::fmt(#arg, formatter)
});
} else if self.was_shorthand && fmt.value() == "{:?}" {
let arg = args.clone().into_iter().skip(1).next().unwrap();
tokens.extend(quote! {
std::fmt::Debug::fmt(#arg, formatter)
});
} else {
tokens.extend(quote! {
write!(formatter, #fmt #args)
});
}
}
}

Expand Down

0 comments on commit 5994705

Please sign in to comment.