Skip to content

Commit 2f66778

Browse files
committed
types: replace closure with iterator
Simplifies the API and reduces copying.
1 parent 22dad5e commit 2f66778

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

src/miniscript/types/correctness.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,12 @@ impl Correctness {
467467

468468
/// Constructor for the correctness properties of the `thresh` fragment
469469
// Cannot be constfn because it takes a closure.
470-
pub fn threshold<S>(_k: usize, n: usize, mut sub_ck: S) -> Result<Self, ErrorKind>
470+
pub fn threshold<'a, I>(_k: usize, subs: I) -> Result<Self, ErrorKind>
471471
where
472-
S: FnMut(usize) -> Self,
472+
I: Iterator<Item = &'a Self>,
473473
{
474474
let mut num_args = 0;
475-
for i in 0..n {
476-
let subtype = sub_ck(i);
475+
for (i, subtype) in subs.enumerate() {
477476
num_args += match subtype.input {
478477
Input::Zero => 0,
479478
Input::One | Input::OneNonZero => 1,

src/miniscript/types/malleability.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,19 +278,20 @@ impl Malleability {
278278

279279
/// Constructor for the malleabilitiy properties of the `thresh` fragment.
280280
// Cannot be constfn because it takes a closure.
281-
pub fn threshold<S>(k: usize, n: usize, mut sub_ck: S) -> Self
281+
pub fn threshold<'a, I>(k: usize, subs: I) -> Self
282282
where
283-
S: FnMut(usize) -> Self,
283+
I: ExactSizeIterator<Item = &'a Self>,
284284
{
285+
let n = subs.len();
285286
let mut safe_count = 0;
286287
let mut all_are_dissat_unique = true;
287288
let mut all_are_non_malleable = true;
288-
for i in 0..n {
289-
let subtype = sub_ck(i);
289+
for subtype in subs {
290290
safe_count += usize::from(subtype.safe);
291291
all_are_dissat_unique &= subtype.dissat == Dissat::Unique;
292292
all_are_non_malleable &= subtype.non_malleable;
293293
}
294+
294295
Malleability {
295296
dissat: if all_are_dissat_unique && safe_count == n {
296297
Dissat::Unique

src/miniscript/types/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,13 +464,13 @@ impl Type {
464464

465465
/// Constructor for the type of the `thresh` fragment.
466466
// Cannot be a constfn because it takes a closure.
467-
pub fn threshold<S>(k: usize, n: usize, mut sub_ck: S) -> Result<Self, ErrorKind>
467+
pub fn threshold<'a, I>(k: usize, subs: I) -> Result<Self, ErrorKind>
468468
where
469-
S: FnMut(usize) -> Self,
469+
I: Clone + ExactSizeIterator<Item = &'a Self>,
470470
{
471471
Ok(Type {
472-
corr: Correctness::threshold(k, n, |n| sub_ck(n).corr)?,
473-
mall: Malleability::threshold(k, n, |n| sub_ck(n).mall),
472+
corr: Correctness::threshold(k, subs.clone().map(|s| &s.corr))?,
473+
mall: Malleability::threshold(k, subs.map(|s| &s.mall)),
474474
})
475475
}
476476
}
@@ -593,7 +593,7 @@ impl Type {
593593
});
594594
}
595595

596-
let res = Self::threshold(k, subs.len(), |n| subs[n].ty);
596+
let res = Self::threshold(k, subs.iter().map(|ms| &ms.ty));
597597

598598
res.map_err(|kind| Error { fragment_string: fragment.to_string(), error: kind })
599599
}

0 commit comments

Comments
 (0)