Skip to content

Commit e1e95d3

Browse files
committed
Compress patter match in script_size
Compress match arms using `|`, this makes the code more terse and easier to quickly see if terminal variants return the expected length (for the trivial ones at least).
1 parent 23897d1 commit e1e95d3

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

src/miniscript/mod.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -122,33 +122,24 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
122122
/// to instead call the corresponding function on a `Descriptor`, which
123123
/// will handle the segwit/non-segwit technicalities for you.
124124
pub fn script_size(&self) -> usize {
125+
use Terminal::*;
126+
125127
let mut len = 0;
126128
for ms in self.pre_order_iter() {
127129
len += match ms.node {
130+
AndV(..) => 0,
131+
True | False | Swap(..) | Check(..) | ZeroNotEqual(..) | AndB(..) | OrB(..) => 1,
132+
Alt(..) | OrC(..) => 2,
133+
DupIf(..) | AndOr(..) | OrD(..) | OrI(..) => 3,
134+
NonZero(..) => 4,
135+
PkH(..) | RawPkH(..) => 24,
136+
Ripemd160(..) | Hash160(..) => 21 + 6,
137+
Sha256(..) | Hash256(..) => 33 + 6,
138+
128139
Terminal::PkK(ref pk) => Ctx::pk_len(pk),
129-
Terminal::PkH(..) | Terminal::RawPkH(..) => 24,
130140
Terminal::After(n) => script_num_size(n.to_consensus_u32() as usize) + 1,
131141
Terminal::Older(n) => script_num_size(n.to_consensus_u32() as usize) + 1,
132-
Terminal::Sha256(..) => 33 + 6,
133-
Terminal::Hash256(..) => 33 + 6,
134-
Terminal::Ripemd160(..) => 21 + 6,
135-
Terminal::Hash160(..) => 21 + 6,
136-
Terminal::True => 1,
137-
Terminal::False => 1,
138-
Terminal::Alt(..) => 2,
139-
Terminal::Swap(..) => 1,
140-
Terminal::Check(..) => 1,
141-
Terminal::DupIf(..) => 3,
142142
Terminal::Verify(ref sub) => usize::from(!sub.ext.has_free_verify),
143-
Terminal::NonZero(..) => 4,
144-
Terminal::ZeroNotEqual(..) => 1,
145-
Terminal::AndV(..) => 0,
146-
Terminal::AndB(..) => 1,
147-
Terminal::AndOr(..) => 3,
148-
Terminal::OrB(..) => 1,
149-
Terminal::OrD(..) => 3,
150-
Terminal::OrC(..) => 2,
151-
Terminal::OrI(..) => 3,
152143
Terminal::Thresh(k, ref subs) => {
153144
assert!(!subs.is_empty(), "threshold must be nonempty");
154145
script_num_size(k) // k

0 commit comments

Comments
 (0)