Skip to content

Commit

Permalink
Defer binding_value construction
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 10, 2024
1 parent 520343e commit dc0359e
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions impl/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,11 @@ impl Display<'_> {
}
_ => continue,
};
let binding_value = match &member {
MemberUnraw::Unnamed(index) => format_ident!("_{}", index),
MemberUnraw::Named(ident) => ident.to_local(),
};
let mut wrapped_binding_value = quote!(::thiserror::__private::Var(#binding_value));
let end_spec = match read.find('}') {
Some(end_spec) => end_spec,
None => return Ok(()),
};
let mut bonus_display = false;
let bound = match read[..end_spec].chars().next_back() {
Some('?') => Trait::Debug,
Some('o') => Trait::Octal,
Expand All @@ -105,10 +101,7 @@ impl Display<'_> {
Some('E') => Trait::UpperExp,
Some(_) => Trait::Display,
None => {
has_bonus_display = true;
wrapped_binding_value = quote_spanned! {span=>
#binding_value.as_display()
};
bonus_display = true;
Trait::Display
}
};
Expand All @@ -127,11 +120,21 @@ impl Display<'_> {
formatvar = IdentUnraw::new(format_ident!("_{}", formatvar.to_string()));
}
out += &formatvar.to_string();
if macro_named_args.insert(member) {
bindings.push((formatvar.to_local(), wrapped_binding_value));
} else {
if !macro_named_args.insert(member.clone()) {
// Already added to bindings by a previous use.
continue;
}
let binding_value = match &member {
MemberUnraw::Unnamed(index) => format_ident!("_{}", index),
MemberUnraw::Named(ident) => ident.to_local(),
};
let wrapped_binding_value = if bonus_display {
quote_spanned!(span=> #binding_value.as_display())
} else {
quote!(::thiserror::__private::Var(#binding_value))
};
has_bonus_display |= bonus_display;
bindings.push((formatvar.to_local(), wrapped_binding_value));
}

out += read;
Expand Down

0 comments on commit dc0359e

Please sign in to comment.