Skip to content

Commit

Permalink
Make the formatter argument name unlikely to clash with user names
Browse files Browse the repository at this point in the history
Fixes #202
  • Loading branch information
shepmaster committed Jan 7, 2020
1 parent 4a24a17 commit 0d141c7
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions snafu-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,16 @@ fn private_visibility() -> UserInput {
Box::new(quote! {})
}

struct StaticIdent(&'static str);

impl quote::ToTokens for StaticIdent {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
proc_macro2::Ident::new(self.0, proc_macro2::Span::call_site()).to_tokens(tokens)
}
}

const FORMATTER_ARG: StaticIdent = StaticIdent("__snafu_display_formatter");

impl From<SnafuInfo> for proc_macro::TokenStream {
fn from(other: SnafuInfo) -> proc_macro::TokenStream {
match other {
Expand Down Expand Up @@ -1536,7 +1546,7 @@ impl<'a> DisplayImpl<'a> {

quote! {
#enum_name::#variant_name { #field_names } => {
write!(f, #format)
write!(#FORMATTER_ARG, #format)
}
}
})
Expand All @@ -1558,7 +1568,7 @@ impl<'a> quote::ToTokens for DisplayImpl<'a> {
where
#(#where_clauses),*
{
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
fn fmt(&self, #FORMATTER_ARG: &mut core::fmt::Formatter) -> core::fmt::Result {
#[allow(unused_variables)]
match *self {
#(#variants_to_display)*
Expand Down

0 comments on commit 0d141c7

Please sign in to comment.