Skip to content

Commit

Permalink
Implement ToTokens without reliance on {:?}
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Apr 20, 2024
1 parent 508ece8 commit f3fbd99
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions impl/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,18 @@ impl ToTokens for Display<'_> {

impl ToTokens for Trait {
fn to_tokens(&self, tokens: &mut TokenStream) {
let trait_name = format_ident!("{}", format!("{:?}", self));
tokens.extend(quote!(::core::fmt::#trait_name));
let trait_name = match self {
Trait::Debug => "Debug",
Trait::Display => "Display",
Trait::Octal => "Octal",
Trait::LowerHex => "LowerHex",
Trait::UpperHex => "UpperHex",
Trait::Pointer => "Pointer",
Trait::Binary => "Binary",
Trait::LowerExp => "LowerExp",
Trait::UpperExp => "UpperExp",
};
let ident = Ident::new(trait_name, Span::call_site());
tokens.extend(quote!(::core::fmt::#ident));
}
}

0 comments on commit f3fbd99

Please sign in to comment.