Skip to content

Commit dc0359e

Browse files
committed
Defer binding_value construction
1 parent 520343e commit dc0359e

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

impl/src/fmt.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,11 @@ impl Display<'_> {
8585
}
8686
_ => continue,
8787
};
88-
let binding_value = match &member {
89-
MemberUnraw::Unnamed(index) => format_ident!("_{}", index),
90-
MemberUnraw::Named(ident) => ident.to_local(),
91-
};
92-
let mut wrapped_binding_value = quote!(::thiserror::__private::Var(#binding_value));
9388
let end_spec = match read.find('}') {
9489
Some(end_spec) => end_spec,
9590
None => return Ok(()),
9691
};
92+
let mut bonus_display = false;
9793
let bound = match read[..end_spec].chars().next_back() {
9894
Some('?') => Trait::Debug,
9995
Some('o') => Trait::Octal,
@@ -105,10 +101,7 @@ impl Display<'_> {
105101
Some('E') => Trait::UpperExp,
106102
Some(_) => Trait::Display,
107103
None => {
108-
has_bonus_display = true;
109-
wrapped_binding_value = quote_spanned! {span=>
110-
#binding_value.as_display()
111-
};
104+
bonus_display = true;
112105
Trait::Display
113106
}
114107
};
@@ -127,11 +120,21 @@ impl Display<'_> {
127120
formatvar = IdentUnraw::new(format_ident!("_{}", formatvar.to_string()));
128121
}
129122
out += &formatvar.to_string();
130-
if macro_named_args.insert(member) {
131-
bindings.push((formatvar.to_local(), wrapped_binding_value));
132-
} else {
123+
if !macro_named_args.insert(member.clone()) {
133124
// Already added to bindings by a previous use.
125+
continue;
134126
}
127+
let binding_value = match &member {
128+
MemberUnraw::Unnamed(index) => format_ident!("_{}", index),
129+
MemberUnraw::Named(ident) => ident.to_local(),
130+
};
131+
let wrapped_binding_value = if bonus_display {
132+
quote_spanned!(span=> #binding_value.as_display())
133+
} else {
134+
quote!(::thiserror::__private::Var(#binding_value))
135+
};
136+
has_bonus_display |= bonus_display;
137+
bindings.push((formatvar.to_local(), wrapped_binding_value));
135138
}
136139

137140
out += read;

0 commit comments

Comments
 (0)