Skip to content

Commit 1467453

Browse files
committed
compiler: refactor out a call to errstr
The private function `with_huffman_tree` can only fail if it is given an empty input. In both places we call it, we unwrap the result because we know this is impossible. Instead, just change the function to panic internally instead of returning an error we're just going to unwrap. Since the error was constructed with errstr, this gets rid of a call to errstr.
1 parent dd19874 commit 1467453

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

src/policy/concrete.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ use {
1919
core::cmp::Reverse,
2020
};
2121

22-
#[cfg(feature = "compiler")]
23-
use crate::errstr;
2422
use crate::expression::{self, FromTree};
2523
use crate::iter::{Tree, TreeLike};
2624
use crate::miniscript::types::extra_props::TimelockInfo;
@@ -249,7 +247,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
249247
leaf_compilations.push((OrdF64(prob), compilation));
250248
}
251249
if !leaf_compilations.is_empty() {
252-
let tap_tree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
250+
let tap_tree = with_huffman_tree::<Pk>(leaf_compilations);
253251
Some(tap_tree)
254252
} else {
255253
// no policies remaining once the extracted key is skipped
@@ -311,7 +309,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
311309
.collect();
312310

313311
if !leaf_compilations.is_empty() {
314-
let tap_tree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
312+
let tap_tree = with_huffman_tree::<Pk>(leaf_compilations);
315313
Some(tap_tree)
316314
} else {
317315
// no policies remaining once the extracted key is skipped
@@ -957,30 +955,25 @@ impl<Pk: FromStrKey> expression::FromTree for Policy<Pk> {
957955

958956
/// Creates a Huffman Tree from compiled [`Miniscript`] nodes.
959957
#[cfg(feature = "compiler")]
960-
fn with_huffman_tree<Pk: MiniscriptKey>(
961-
ms: Vec<(OrdF64, Miniscript<Pk, Tap>)>,
962-
) -> Result<TapTree<Pk>, Error> {
958+
fn with_huffman_tree<Pk: MiniscriptKey>(ms: Vec<(OrdF64, Miniscript<Pk, Tap>)>) -> TapTree<Pk> {
963959
let mut node_weights = BinaryHeap::<(Reverse<OrdF64>, TapTree<Pk>)>::new();
964960
for (prob, script) in ms {
965961
node_weights.push((Reverse(prob), TapTree::Leaf(Arc::new(script))));
966962
}
967-
if node_weights.is_empty() {
968-
return Err(errstr("Empty Miniscript compilation"));
969-
}
963+
assert_ne!(node_weights.len(), 0, "empty Miniscript compilation");
970964
while node_weights.len() > 1 {
971-
let (p1, s1) = node_weights.pop().expect("len must atleast be two");
972-
let (p2, s2) = node_weights.pop().expect("len must atleast be two");
965+
let (p1, s1) = node_weights.pop().expect("len must at least be two");
966+
let (p2, s2) = node_weights.pop().expect("len must at least be two");
973967

974968
let p = (p1.0).0 + (p2.0).0;
975969
node_weights.push((Reverse(OrdF64(p)), TapTree::combine(s1, s2)));
976970
}
977971

978972
debug_assert!(node_weights.len() == 1);
979-
let node = node_weights
973+
node_weights
980974
.pop()
981975
.expect("huffman tree algorithm is broken")
982-
.1;
983-
Ok(node)
976+
.1
984977
}
985978

986979
/// Enumerates a [`Policy::Thresh(k, ..n..)`] into `n` different thresh's.

0 commit comments

Comments
 (0)