Skip to content

Commit 752acb3

Browse files
committed
apply immediate dispatch in generic fn
by refactoring out the function when the type is known, we avoid the duplication in the generic function. In the process write_char is replaced with write_str when the string is static because by looking at the impl it saves a utf8 str conversion from the char.
1 parent 426c34d commit 752acb3

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/miniscript/astelem.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,15 @@ fn conditional_fmt<D: fmt::Debug + fmt::Display>(
180180

181181
impl<Pk: MiniscriptKey, Ctx: ScriptContext> fmt::Debug for Terminal<Pk, Ctx> {
182182
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
183-
f.write_str("[")?;
184-
if let Ok(type_map) = types::Type::type_check(self) {
183+
184+
fn fmt_type_map(f: &mut fmt::Formatter<'_>, type_map: types::Type) -> fmt::Result {
185185
f.write_str(match type_map.corr.base {
186186
types::Base::B => "B",
187187
types::Base::K => "K",
188188
types::Base::V => "V",
189189
types::Base::W => "W",
190190
})?;
191-
fmt::Write::write_char(f, '/')?;
191+
f.write_str("/")?;
192192
f.write_str(match type_map.corr.input {
193193
types::Input::Zero => "z",
194194
types::Input::One => "o",
@@ -197,30 +197,36 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> fmt::Debug for Terminal<Pk, Ctx> {
197197
types::Input::AnyNonZero => "n",
198198
})?;
199199
if type_map.corr.dissatisfiable {
200-
fmt::Write::write_char(f, 'd')?;
200+
f.write_str("d")?;
201201
}
202202
if type_map.corr.unit {
203-
fmt::Write::write_char(f, 'u')?;
203+
f.write_str("u")?;
204204
}
205205
f.write_str(match type_map.mall.dissat {
206206
types::Dissat::None => "f",
207207
types::Dissat::Unique => "e",
208208
types::Dissat::Unknown => "",
209209
})?;
210210
if type_map.mall.safe {
211-
fmt::Write::write_char(f, 's')?;
211+
f.write_str("s")?;
212212
}
213213
if type_map.mall.non_malleable {
214-
fmt::Write::write_char(f, 'm')?;
214+
f.write_str("m")?;
215215
}
216+
Ok(())
217+
}
218+
219+
f.write_str("[")?;
220+
if let Ok(type_map) = types::Type::type_check(self) {
221+
fmt_type_map(f, type_map)?;
216222
} else {
217223
f.write_str("TYPECHECK FAILED")?;
218224
}
219225
f.write_str("]")?;
220226
if let Some((ch, sub)) = self.wrap_char() {
221227
fmt::Write::write_char(f, ch)?;
222228
if sub.node.wrap_char().is_none() {
223-
fmt::Write::write_char(f, ':')?;
229+
f.write_str(":")?;
224230
}
225231
write!(f, "{:?}", sub)
226232
} else {

0 commit comments

Comments
 (0)