Skip to content

Commit 8d0cf06

Browse files
Remove ForEach Trait
Since we disregard any `Pk::Hash` as a valid iterable key, only `Pk` is considered as such. The refactor reflects directly iterating over `Pk` (a valid Miniscript Key).
1 parent 5cb2bcf commit 8d0cf06

File tree

11 files changed

+49
-55
lines changed

11 files changed

+49
-55
lines changed

src/descriptor/bare.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ use crate::policy::{semantic, Liftable};
3030
use crate::prelude::*;
3131
use crate::util::{varint_len, witness_to_scriptsig};
3232
use crate::{
33-
BareCtx, Error, ForEach, ForEachKey, Miniscript, MiniscriptKey, Satisfier, ToPublicKey,
34-
TranslatePk, Translator,
33+
BareCtx, Error, ForEachKey, Miniscript, MiniscriptKey, Satisfier, ToPublicKey, TranslatePk,
34+
Translator,
3535
};
3636

3737
/// Create a Bare Descriptor. That is descriptor that is
@@ -164,7 +164,7 @@ impl_from_str!(
164164
);
165165

166166
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {
167-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: F) -> bool
167+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
168168
where
169169
Pk: 'a,
170170
Pk::Hash: 'a,
@@ -326,12 +326,12 @@ impl_from_str!(
326326
);
327327

328328
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
329-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
329+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
330330
where
331331
Pk: 'a,
332332
Pk::Hash: 'a,
333333
{
334-
pred(ForEach(&self.pk))
334+
pred(&self.pk)
335335
}
336336
}
337337

src/descriptor/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ use self::checksum::verify_checksum;
3737
use crate::miniscript::{Legacy, Miniscript, Segwitv0};
3838
use crate::prelude::*;
3939
use crate::{
40-
expression, miniscript, BareCtx, Error, ForEach, ForEachKey, MiniscriptKey, PkTranslator,
41-
Satisfier, ToPublicKey, TranslatePk, Translator,
40+
expression, miniscript, BareCtx, Error, ForEachKey, MiniscriptKey, PkTranslator, Satisfier,
41+
ToPublicKey, TranslatePk, Translator,
4242
};
4343

4444
mod bare;
@@ -495,7 +495,7 @@ where
495495
}
496496

497497
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk> {
498-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: F) -> bool
498+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
499499
where
500500
Pk: 'a,
501501
Pk::Hash: 'a,
@@ -514,7 +514,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk> {
514514
impl Descriptor<DescriptorPublicKey> {
515515
/// Whether or not the descriptor has any wildcards
516516
pub fn is_deriveable(&self) -> bool {
517-
self.for_any_key(|key| key.as_key().is_deriveable())
517+
self.for_any_key(|key| key.is_deriveable())
518518
}
519519

520520
/// Derives all wildcard keys in the descriptor using the supplied index

src/descriptor/segwitv0.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use crate::policy::{semantic, Liftable};
2828
use crate::prelude::*;
2929
use crate::util::varint_len;
3030
use crate::{
31-
Error, ForEach, ForEachKey, Miniscript, MiniscriptKey, Satisfier, Segwitv0, ToPublicKey,
32-
TranslatePk, Translator,
31+
Error, ForEachKey, Miniscript, MiniscriptKey, Satisfier, Segwitv0, ToPublicKey, TranslatePk,
32+
Translator,
3333
};
3434
/// A Segwitv0 wsh descriptor
3535
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
@@ -246,7 +246,7 @@ impl_from_str!(
246246
);
247247

248248
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wsh<Pk> {
249-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: F) -> bool
249+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
250250
where
251251
Pk: 'a,
252252
Pk::Hash: 'a,
@@ -439,12 +439,12 @@ impl_from_str!(
439439
);
440440

441441
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wpkh<Pk> {
442-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
442+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
443443
where
444444
Pk: 'a,
445445
Pk::Hash: 'a,
446446
{
447-
pred(ForEach(&self.pk))
447+
pred(&self.pk)
448448
}
449449
}
450450

src/descriptor/sh.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use crate::policy::{semantic, Liftable};
3131
use crate::prelude::*;
3232
use crate::util::{varint_len, witness_to_scriptsig};
3333
use crate::{
34-
push_opcode_size, Error, ForEach, ForEachKey, Legacy, Miniscript, MiniscriptKey, Satisfier,
35-
Segwitv0, ToPublicKey, TranslatePk, Translator,
34+
push_opcode_size, Error, ForEachKey, Legacy, Miniscript, MiniscriptKey, Satisfier, Segwitv0,
35+
ToPublicKey, TranslatePk, Translator,
3636
};
3737

3838
/// A Legacy p2sh Descriptor
@@ -377,7 +377,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
377377
}
378378

379379
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Sh<Pk> {
380-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: F) -> bool
380+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
381381
where
382382
Pk: 'a,
383383
Pk::Hash: 'a,

src/descriptor/sortedmulti.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use crate::miniscript::decode::Terminal;
2727
use crate::miniscript::limits::MAX_PUBKEYS_PER_MULTISIG;
2828
use crate::prelude::*;
2929
use crate::{
30-
errstr, expression, miniscript, policy, script_num_size, Error, ForEach, ForEachKey,
31-
Miniscript, MiniscriptKey, Satisfier, ToPublicKey, Translator,
30+
errstr, expression, miniscript, policy, script_num_size, Error, ForEachKey, Miniscript,
31+
MiniscriptKey, Satisfier, ToPublicKey, Translator,
3232
};
3333

3434
/// Contents of a "sortedmulti" descriptor
@@ -112,12 +112,12 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
112112
}
113113

114114
impl<Pk: MiniscriptKey, Ctx: ScriptContext> ForEachKey<Pk> for SortedMultiVec<Pk, Ctx> {
115-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
115+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
116116
where
117117
Pk: 'a,
118118
Pk::Hash: 'a,
119119
{
120-
self.pks.iter().all(|key| pred(ForEach(key)))
120+
self.pks.iter().all(|key| pred(key))
121121
}
122122
}
123123

src/descriptor/tr.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ use crate::policy::Liftable;
1919
use crate::prelude::*;
2020
use crate::util::{varint_len, witness_size};
2121
use crate::{
22-
errstr, Error, ForEach, ForEachKey, MiniscriptKey, Satisfier, Tap, ToPublicKey, TranslatePk,
23-
Translator,
22+
errstr, Error, ForEachKey, MiniscriptKey, Satisfier, Tap, ToPublicKey, TranslatePk, Translator,
2423
};
2524

2625
/// A Taproot Tree representation.
@@ -579,15 +578,15 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Tr<Pk> {
579578
}
580579

581580
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Tr<Pk> {
582-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
581+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
583582
where
584583
Pk: 'a,
585584
Pk::Hash: 'a,
586585
{
587586
let script_keys_res = self
588587
.iter_scripts()
589588
.all(|(_d, ms)| ms.for_each_key(&mut pred));
590-
script_keys_res && pred(ForEach(&self.internal_key))
589+
script_keys_res && pred(&self.internal_key)
591590
}
592591
}
593592

@@ -702,6 +701,6 @@ mod tests {
702701
let desc = desc.replace(&[' ', '\n'][..], "");
703702
let tr = Tr::<String>::from_str(&desc).unwrap();
704703
// Note the last ac12 only has ac and fails the predicate
705-
assert!(!tr.for_each_key(|k| k.0.starts_with("acc")));
704+
assert!(!tr.for_each_key(|k| k.starts_with("acc")));
706705
}
707706
}

src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -459,27 +459,27 @@ where
459459
}
460460

461461
/// Either a key or keyhash, but both contain Pk
462-
pub struct ForEach<'a, Pk: MiniscriptKey>(&'a Pk);
462+
// pub struct ForEach<'a, Pk: MiniscriptKey>(&'a Pk);
463463

464-
impl<'a, Pk: MiniscriptKey<Hash = Pk>> ForEach<'a, Pk> {
465-
/// Convenience method to avoid distinguishing between keys and hashes when these are the same type
466-
pub fn as_key(&self) -> &'a Pk {
467-
self.0
468-
}
469-
}
464+
// impl<'a, Pk: MiniscriptKey<Hash = Pk>> ForEach<'a, Pk> {
465+
// /// Convenience method to avoid distinguishing between keys and hashes when these are the same type
466+
// pub fn as_key(&self) -> &'a Pk {
467+
// self.0
468+
// }
469+
// }
470470

471471
/// Trait describing the ability to iterate over every key
472472
pub trait ForEachKey<Pk: MiniscriptKey> {
473473
/// Run a predicate on every key in the descriptor, returning whether
474474
/// the predicate returned true for every key
475-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: F) -> bool
475+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
476476
where
477477
Pk: 'a,
478478
Pk::Hash: 'a;
479479

480480
/// Run a predicate on every key in the descriptor, returning whether
481481
/// the predicate returned true for any key
482-
fn for_any_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
482+
fn for_any_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
483483
where
484484
Pk: 'a,
485485
Pk::Hash: 'a,

src/miniscript/astelem.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ use crate::miniscript::ScriptContext;
3333
use crate::prelude::*;
3434
use crate::util::MsKeyBuilder;
3535
use crate::{
36-
errstr, expression, script_num_size, Error, ForEach, ForEachKey, Miniscript, MiniscriptKey,
37-
Terminal, ToPublicKey, TranslatePk, Translator,
36+
errstr, expression, script_num_size, Error, ForEachKey, Miniscript, MiniscriptKey, Terminal,
37+
ToPublicKey, TranslatePk, Translator,
3838
};
3939

4040
impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
@@ -76,17 +76,14 @@ where
7676
}
7777

7878
impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
79-
pub(super) fn real_for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(
80-
&'a self,
81-
pred: &mut F,
82-
) -> bool
79+
pub(super) fn real_for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: &mut F) -> bool
8380
where
8481
Pk: 'a,
8582
Pk::Hash: 'a,
8683
{
8784
match *self {
88-
Terminal::PkK(ref p) => pred(ForEach(p)),
89-
Terminal::PkH(ref p) => pred(ForEach(p)),
85+
Terminal::PkK(ref p) => pred(p),
86+
Terminal::PkH(ref p) => pred(p),
9087
Terminal::RawPkH(..)
9188
| Terminal::After(..)
9289
| Terminal::Older(..)
@@ -118,7 +115,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
118115
}
119116
Terminal::Thresh(_, ref subs) => subs.iter().all(|sub| sub.real_for_each_key(pred)),
120117
Terminal::Multi(_, ref keys) | Terminal::MultiA(_, ref keys) => {
121-
keys.iter().all(|key| pred(ForEach(key)))
118+
keys.iter().all(|key| pred(key))
122119
}
123120
}
124121
}
@@ -200,7 +197,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
200197
}
201198

202199
impl<Pk: MiniscriptKey, Ctx: ScriptContext> ForEachKey<Pk> for Terminal<Pk, Ctx> {
203-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
200+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
204201
where
205202
Pk: 'a,
206203
Pk::Hash: 'a,

src/miniscript/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ pub use crate::miniscript::context::ScriptContext;
5353
use crate::miniscript::decode::Terminal;
5454
use crate::miniscript::types::extra_props::ExtData;
5555
use crate::miniscript::types::Type;
56-
use crate::{
57-
expression, Error, ForEach, ForEachKey, MiniscriptKey, ToPublicKey, TranslatePk, Translator,
58-
};
56+
use crate::{expression, Error, ForEachKey, MiniscriptKey, ToPublicKey, TranslatePk, Translator};
5957
#[cfg(test)]
6058
mod ms_tests;
6159

@@ -271,7 +269,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
271269
}
272270

273271
impl<Pk: MiniscriptKey, Ctx: ScriptContext> ForEachKey<Pk> for Miniscript<Pk, Ctx> {
274-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
272+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
275273
where
276274
Pk: 'a,
277275
Pk::Hash: 'a,
@@ -299,7 +297,7 @@ where
299297
}
300298

301299
impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
302-
fn real_for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: &mut F) -> bool
300+
fn real_for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: &mut F) -> bool
303301
where
304302
Pk: 'a,
305303
Pk::Hash: 'a,

src/policy/concrete.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use crate::expression::{self, FromTree};
4040
use crate::miniscript::limits::{LOCKTIME_THRESHOLD, SEQUENCE_LOCKTIME_TYPE_FLAG};
4141
use crate::miniscript::types::extra_props::TimelockInfo;
4242
use crate::prelude::*;
43-
use crate::{errstr, Error, ForEach, ForEachKey, MiniscriptKey, Translator};
43+
use crate::{errstr, Error, ForEachKey, MiniscriptKey, Translator};
4444

4545
/// Concrete policy which corresponds directly to a Miniscript structure,
4646
/// and whose disjunctions are annotated with satisfaction probabilities
@@ -297,14 +297,14 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
297297
}
298298

299299
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
300-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
300+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
301301
where
302302
Pk: 'a,
303303
Pk::Hash: 'a,
304304
{
305305
match *self {
306306
Policy::Unsatisfiable | Policy::Trivial => true,
307-
Policy::Key(ref pk) => pred(ForEach(pk)),
307+
Policy::Key(ref pk) => pred(pk),
308308
Policy::Sha256(..)
309309
| Policy::Hash256(..)
310310
| Policy::Ripemd160(..)

src/policy/semantic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use bitcoin::hashes::{hash160, ripemd160, sha256d};
2323
use super::concrete::PolicyError;
2424
use super::ENTAILMENT_MAX_TERMINALS;
2525
use crate::prelude::*;
26-
use crate::{errstr, expression, timelock, Error, ForEach, ForEachKey, MiniscriptKey, Translator};
26+
use crate::{errstr, expression, timelock, Error, ForEachKey, MiniscriptKey, Translator};
2727

2828
/// Abstract policy which corresponds to the semantics of a Miniscript
2929
/// and which allows complex forms of analysis, e.g. filtering and
@@ -56,7 +56,7 @@ pub enum Policy<Pk: MiniscriptKey> {
5656
}
5757

5858
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
59-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
59+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
6060
where
6161
Pk: 'a,
6262
Pk::Hash: 'a,

0 commit comments

Comments
 (0)