Skip to content

Commit d2e30d5

Browse files
committed
tr: introduce TapTree:leaf constructor
This constructor will be part of the new API introduced in the following "flatten TapTree" commit. To reduce the diff of that commit, introduce it now.
1 parent c362d90 commit d2e30d5

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

src/descriptor/tr/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ impl<Pk: FromStrKey> crate::expression::FromTree for Tr<Pk> {
456456
return Err(Error::NonTopLevel(format!("{:?}", script)));
457457
};
458458

459-
tree_stack.push(node.parent().unwrap(), TapTree::Leaf(Arc::new(script)))?;
459+
tree_stack.push(node.parent().unwrap(), TapTree::leaf(script))?;
460460
tap_tree_iter.skip_descendants();
461461
}
462462
}

src/descriptor/tr/taptree.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ pub enum TapTree<Pk: MiniscriptKey> {
4646
}
4747

4848
impl<Pk: MiniscriptKey> TapTree<Pk> {
49+
/// Creates a `TapTree` leaf from a Miniscript.
50+
pub fn leaf<A: Into<Arc<Miniscript<Pk, Tap>>>>(ms: A) -> Self { TapTree::Leaf(ms.into()) }
51+
4952
/// Creates a `TapTree` by combining `left` and `right` tree nodes.
5053
pub fn combine(left: TapTree<Pk>, right: TapTree<Pk>) -> Result<Self, TapTreeDepthError> {
5154
let height = 1 + cmp::max(left.height(), right.height());

src/policy/concrete.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
589589
.collect()
590590
}
591591

592-
/// Gets the number of [TapLeaf](`TapTree::Leaf`)s considering exhaustive root-level [`Policy::Or`]
592+
/// Gets the number of [TapLeaf](`TapTree::leaf`)s considering exhaustive root-level [`Policy::Or`]
593593
/// and [`Policy::Thresh`] disjunctions for the `TapTree`.
594594
#[cfg(feature = "compiler")]
595595
fn num_tap_leaves(&self) -> usize { self.tapleaf_probability_iter().count() }
@@ -957,7 +957,7 @@ impl<Pk: FromStrKey> expression::FromTree for Policy<Pk> {
957957
fn with_huffman_tree<Pk: MiniscriptKey>(ms: Vec<(OrdF64, Miniscript<Pk, Tap>)>) -> TapTree<Pk> {
958958
let mut node_weights = BinaryHeap::<(Reverse<OrdF64>, TapTree<Pk>)>::new();
959959
for (prob, script) in ms {
960-
node_weights.push((Reverse(prob), TapTree::Leaf(Arc::new(script))));
960+
node_weights.push((Reverse(prob), TapTree::leaf(script)));
961961
}
962962
assert_ne!(node_weights.len(), 0, "empty Miniscript compilation");
963963
while node_weights.len() > 1 {

src/policy/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ mod tests {
375375
let descriptor = policy.compile_tr(Some(unspendable_key.clone())).unwrap();
376376

377377
let ms_compilation: Miniscript<String, Tap> = ms_str!("multi_a(2,A,B,C,D)");
378-
let tree: TapTree<String> = TapTree::Leaf(Arc::new(ms_compilation));
378+
let tree: TapTree<String> = TapTree::leaf(ms_compilation);
379379
let expected_descriptor =
380380
Descriptor::new_tr(unspendable_key.clone(), Some(tree)).unwrap();
381381
assert_eq!(descriptor, expected_descriptor);
@@ -391,8 +391,8 @@ mod tests {
391391
let right_ms_compilation: Arc<Miniscript<String, Tap>> =
392392
Arc::new(ms_str!("and_v(v:pk(A),pk(B))"));
393393

394-
let left = TapTree::Leaf(left_ms_compilation);
395-
let right = TapTree::Leaf(right_ms_compilation);
394+
let left = TapTree::leaf(left_ms_compilation);
395+
let right = TapTree::leaf(right_ms_compilation);
396396
let tree = TapTree::combine(left, right).unwrap();
397397

398398
let expected_descriptor =
@@ -454,7 +454,7 @@ mod tests {
454454
.into_iter()
455455
.map(|x| {
456456
let leaf_policy: Concrete<String> = policy_str!("{}", x);
457-
TapTree::Leaf(Arc::from(leaf_policy.compile::<Tap>().unwrap()))
457+
TapTree::leaf(leaf_policy.compile::<Tap>().unwrap())
458458
})
459459
.collect::<Vec<_>>();
460460

0 commit comments

Comments
 (0)