Skip to content

Commit

Permalink
Merge pull request #297 from dtolnay/traitident
Browse files Browse the repository at this point in the history
Implement ToTokens without reliance on {:?}
  • Loading branch information
dtolnay authored Apr 20, 2024
2 parents 508ece8 + f3fbd99 commit af477ec
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 af477ec

Please sign in to comment.