Skip to content

Commit f6c8798

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 f564916 commit f6c8798

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

src/miniscript/astelem.rs

+37-32
Original file line numberDiff line numberDiff line change
@@ -182,45 +182,15 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> fmt::Debug for Terminal<Pk, Ctx> {
182182
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
183183
f.write_str("[")?;
184184
if let Ok(type_map) = types::Type::type_check(self) {
185-
f.write_str(match type_map.corr.base {
186-
types::Base::B => "B",
187-
types::Base::K => "K",
188-
types::Base::V => "V",
189-
types::Base::W => "W",
190-
})?;
191-
fmt::Write::write_char(f, '/')?;
192-
f.write_str(match type_map.corr.input {
193-
types::Input::Zero => "z",
194-
types::Input::One => "o",
195-
types::Input::OneNonZero => "on",
196-
types::Input::Any => "",
197-
types::Input::AnyNonZero => "n",
198-
})?;
199-
if type_map.corr.dissatisfiable {
200-
fmt::Write::write_char(f, 'd')?;
201-
}
202-
if type_map.corr.unit {
203-
fmt::Write::write_char(f, 'u')?;
204-
}
205-
f.write_str(match type_map.mall.dissat {
206-
types::Dissat::None => "f",
207-
types::Dissat::Unique => "e",
208-
types::Dissat::Unknown => "",
209-
})?;
210-
if type_map.mall.safe {
211-
fmt::Write::write_char(f, 's')?;
212-
}
213-
if type_map.mall.non_malleable {
214-
fmt::Write::write_char(f, 'm')?;
215-
}
185+
fmt_type_map(f, type_map)?;
216186
} else {
217187
f.write_str("TYPECHECK FAILED")?;
218188
}
219189
f.write_str("]")?;
220190
if let Some((ch, sub)) = self.wrap_char() {
221191
fmt::Write::write_char(f, ch)?;
222192
if sub.node.wrap_char().is_none() {
223-
fmt::Write::write_char(f, ':')?;
193+
f.write_str(":")?;
224194
}
225195
write!(f, "{:?}", sub)
226196
} else {
@@ -229,6 +199,41 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> fmt::Debug for Terminal<Pk, Ctx> {
229199
}
230200
}
231201

202+
fn fmt_type_map(f: &mut fmt::Formatter<'_>, type_map: types::Type) -> fmt::Result {
203+
f.write_str(match type_map.corr.base {
204+
types::Base::B => "B",
205+
types::Base::K => "K",
206+
types::Base::V => "V",
207+
types::Base::W => "W",
208+
})?;
209+
f.write_str("/")?;
210+
f.write_str(match type_map.corr.input {
211+
types::Input::Zero => "z",
212+
types::Input::One => "o",
213+
types::Input::OneNonZero => "on",
214+
types::Input::Any => "",
215+
types::Input::AnyNonZero => "n",
216+
})?;
217+
if type_map.corr.dissatisfiable {
218+
f.write_str("d")?;
219+
}
220+
if type_map.corr.unit {
221+
f.write_str("u")?;
222+
}
223+
f.write_str(match type_map.mall.dissat {
224+
types::Dissat::None => "f",
225+
types::Dissat::Unique => "e",
226+
types::Dissat::Unknown => "",
227+
})?;
228+
if type_map.mall.safe {
229+
f.write_str("s")?;
230+
}
231+
if type_map.mall.non_malleable {
232+
f.write_str("m")?;
233+
}
234+
Ok(())
235+
}
236+
232237
impl<Pk: MiniscriptKey, Ctx: ScriptContext> fmt::Display for Terminal<Pk, Ctx> {
233238
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.conditional_fmt(f, false) }
234239
}

0 commit comments

Comments
 (0)