Skip to content

Commit 851120e

Browse files
committed
Add impl_from_tree!
It is not possible to add a supertrait(and a blanket implemnetation) that captures associated types currently. The where clauses must be carried around everywhere. Use a macro instead
1 parent c43129c commit 851120e

File tree

10 files changed

+227
-283
lines changed

10 files changed

+227
-283
lines changed

src/descriptor/bare.rs

+14-37
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
//!
2020
2121
use core::fmt;
22-
use core::str::FromStr;
2322

2423
use bitcoin::blockdata::script;
2524
use bitcoin::{Address, Network, Script};
@@ -145,35 +144,24 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Bare<Pk> {
145144
}
146145
}
147146

148-
impl<Pk> FromTree for Bare<Pk>
149-
where
150-
Pk: MiniscriptKey + FromStr,
151-
Pk::Hash: FromStr,
152-
<Pk as FromStr>::Err: ToString,
153-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
154-
{
147+
impl_from_tree!(
148+
Bare<Pk>,
155149
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
156150
let sub = Miniscript::<Pk, BareCtx>::from_tree(top)?;
157151
BareCtx::top_level_checks(&sub)?;
158152
Bare::new(sub)
159153
}
160-
}
161-
162-
impl<Pk> FromStr for Bare<Pk>
163-
where
164-
Pk: MiniscriptKey + FromStr,
165-
Pk::Hash: FromStr,
166-
<Pk as FromStr>::Err: ToString,
167-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
168-
{
169-
type Err = Error;
154+
);
170155

156+
impl_from_str!(
157+
Bare<Pk>,
158+
type Err = Error;,
171159
fn from_str(s: &str) -> Result<Self, Self::Err> {
172160
let desc_str = verify_checksum(s)?;
173161
let top = expression::Tree::from_str(desc_str)?;
174162
Self::from_tree(&top)
175163
}
176-
}
164+
);
177165

178166
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {
179167
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: F) -> bool
@@ -313,13 +301,8 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Pkh<Pk> {
313301
}
314302
}
315303

316-
impl<Pk> FromTree for Pkh<Pk>
317-
where
318-
Pk: MiniscriptKey + FromStr,
319-
Pk::Hash: FromStr,
320-
<Pk as FromStr>::Err: ToString,
321-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
322-
{
304+
impl_from_tree!(
305+
Pkh<Pk>,
323306
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
324307
if top.name == "pkh" && top.args.len() == 1 {
325308
Ok(Pkh::new(expression::terminal(&top.args[0], |pk| {
@@ -333,23 +316,17 @@ where
333316
)))
334317
}
335318
}
336-
}
337-
338-
impl<Pk> FromStr for Pkh<Pk>
339-
where
340-
Pk: MiniscriptKey + FromStr,
341-
Pk::Hash: FromStr,
342-
<Pk as FromStr>::Err: ToString,
343-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
344-
{
345-
type Err = Error;
319+
);
346320

321+
impl_from_str!(
322+
Pkh<Pk>,
323+
type Err = Error;,
347324
fn from_str(s: &str) -> Result<Self, Self::Err> {
348325
let desc_str = verify_checksum(s)?;
349326
let top = expression::Tree::from_str(desc_str)?;
350327
Self::from_tree(&top)
351328
}
352-
}
329+
);
353330

354331
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
355332
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool

src/descriptor/mod.rs

+8-19
Original file line numberDiff line numberDiff line change
@@ -651,14 +651,9 @@ impl Descriptor<DescriptorPublicKey> {
651651
}
652652
}
653653

654-
impl<Pk> expression::FromTree for Descriptor<Pk>
655-
where
656-
Pk: MiniscriptKey + str::FromStr,
657-
Pk::Hash: str::FromStr,
658-
<Pk as FromStr>::Err: ToString,
659-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
660-
{
661-
/// Parse an expression tree into a descriptor
654+
impl_from_tree!(
655+
Descriptor<Pk>,
656+
/// Parse an expression tree into a descriptor.
662657
fn from_tree(top: &expression::Tree) -> Result<Descriptor<Pk>, Error> {
663658
Ok(match (top.name, top.args.len() as u32) {
664659
("pkh", 1) => Descriptor::Pkh(Pkh::from_tree(top)?),
@@ -669,17 +664,11 @@ where
669664
_ => Descriptor::Bare(Bare::from_tree(top)?),
670665
})
671666
}
672-
}
673-
674-
impl<Pk> FromStr for Descriptor<Pk>
675-
where
676-
Pk: MiniscriptKey + str::FromStr,
677-
Pk::Hash: str::FromStr,
678-
<Pk as FromStr>::Err: ToString,
679-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
680-
{
681-
type Err = Error;
667+
);
682668

669+
impl_from_str!(
670+
Descriptor<Pk>,
671+
type Err = Error;,
683672
fn from_str(s: &str) -> Result<Descriptor<Pk>, Error> {
684673
// tr tree parsing has special code
685674
// Tr::from_str will check the checksum
@@ -692,7 +681,7 @@ where
692681
expression::FromTree::from_tree(&top)
693682
}
694683
}
695-
}
684+
);
696685

697686
impl<Pk: MiniscriptKey> fmt::Debug for Descriptor<Pk> {
698687
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

src/descriptor/segwitv0.rs

+15-37
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
//! of wsh, wpkh and sortedmulti inside wsh.
1818
1919
use core::fmt;
20-
use core::str::FromStr;
2120

2221
use bitcoin::{self, Address, Network, Script};
2322

@@ -194,13 +193,8 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Wsh<Pk> {
194193
}
195194
}
196195

197-
impl<Pk> FromTree for Wsh<Pk>
198-
where
199-
Pk: MiniscriptKey + FromStr,
200-
Pk::Hash: FromStr,
201-
<Pk as FromStr>::Err: ToString,
202-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
203-
{
196+
impl_from_tree!(
197+
Wsh<Pk>,
204198
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
205199
if top.name == "wsh" && top.args.len() == 1 {
206200
let top = &top.args[0];
@@ -222,7 +216,8 @@ where
222216
)))
223217
}
224218
}
225-
}
219+
);
220+
226221
impl<Pk: MiniscriptKey> fmt::Debug for Wsh<Pk> {
227222
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
228223
match self.inner {
@@ -240,21 +235,15 @@ impl<Pk: MiniscriptKey> fmt::Display for Wsh<Pk> {
240235
}
241236
}
242237

243-
impl<Pk> FromStr for Wsh<Pk>
244-
where
245-
Pk: MiniscriptKey + FromStr,
246-
Pk::Hash: FromStr,
247-
<Pk as FromStr>::Err: ToString,
248-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
249-
{
250-
type Err = Error;
251-
238+
impl_from_str!(
239+
Wsh<Pk>,
240+
type Err = Error;,
252241
fn from_str(s: &str) -> Result<Self, Self::Err> {
253242
let desc_str = verify_checksum(s)?;
254243
let top = expression::Tree::from_str(desc_str)?;
255244
Wsh::<Pk>::from_tree(&top)
256245
}
257-
}
246+
);
258247

259248
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wsh<Pk> {
260249
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: F) -> bool
@@ -423,13 +412,8 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Wpkh<Pk> {
423412
}
424413
}
425414

426-
impl<Pk> FromTree for Wpkh<Pk>
427-
where
428-
Pk: MiniscriptKey + FromStr,
429-
Pk::Hash: FromStr,
430-
<Pk as FromStr>::Err: ToString,
431-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
432-
{
415+
impl_from_tree!(
416+
Wpkh<Pk>,
433417
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
434418
if top.name == "wpkh" && top.args.len() == 1 {
435419
Ok(Wpkh::new(expression::terminal(&top.args[0], |pk| {
@@ -443,23 +427,17 @@ where
443427
)))
444428
}
445429
}
446-
}
447-
448-
impl<Pk> FromStr for Wpkh<Pk>
449-
where
450-
Pk: MiniscriptKey + FromStr,
451-
Pk::Hash: FromStr,
452-
<Pk as FromStr>::Err: ToString,
453-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
454-
{
455-
type Err = Error;
430+
);
456431

432+
impl_from_str!(
433+
Wpkh<Pk>,
434+
type Err = Error;,
457435
fn from_str(s: &str) -> Result<Self, Self::Err> {
458436
let desc_str = verify_checksum(s)?;
459437
let top = expression::Tree::from_str(desc_str)?;
460438
Self::from_tree(&top)
461439
}
462-
}
440+
);
463441

464442
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wpkh<Pk> {
465443
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool

src/descriptor/sh.rs

+7-18
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
//!
2020
2121
use core::fmt;
22-
use core::str::FromStr;
2322

2423
use bitcoin::blockdata::script;
2524
use bitcoin::{Address, Network, Script};
@@ -91,13 +90,8 @@ impl<Pk: MiniscriptKey> fmt::Display for Sh<Pk> {
9190
}
9291
}
9392

94-
impl<Pk> FromTree for Sh<Pk>
95-
where
96-
Pk: MiniscriptKey + FromStr,
97-
Pk::Hash: FromStr,
98-
<Pk as FromStr>::Err: ToString,
99-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
100-
{
93+
impl_from_tree!(
94+
Sh<Pk>,
10195
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
10296
if top.name == "sh" && top.args.len() == 1 {
10397
let top = &top.args[0];
@@ -120,22 +114,17 @@ where
120114
)))
121115
}
122116
}
123-
}
117+
);
124118

125-
impl<Pk> FromStr for Sh<Pk>
126-
where
127-
Pk: MiniscriptKey + FromStr,
128-
Pk::Hash: FromStr,
129-
<Pk as FromStr>::Err: ToString,
130-
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
131-
{
132-
type Err = Error;
119+
impl_from_str!(
120+
Sh<Pk>,
121+
type Err = Error;,
133122
fn from_str(s: &str) -> Result<Self, Self::Err> {
134123
let desc_str = verify_checksum(s)?;
135124
let top = expression::Tree::from_str(desc_str)?;
136125
Self::from_tree(&top)
137126
}
138-
}
127+
);
139128

140129
impl<Pk: MiniscriptKey> Sh<Pk> {
141130
/// Get the Inner

0 commit comments

Comments
 (0)