Skip to content

Commit 270e65d

Browse files
committed
Replace macros with blanket impls
1 parent 5ec0f44 commit 270e65d

File tree

10 files changed

+76
-171
lines changed

10 files changed

+76
-171
lines changed

src/descriptor/bare.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -166,24 +166,22 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Bare<Pk> {
166166
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> { self.ms.lift() }
167167
}
168168

169-
impl_from_tree!(
170-
Bare<Pk>,
169+
impl<Pk: crate::FromStrKey> FromTree for Bare<Pk> {
171170
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
172171
let sub = Miniscript::<Pk, BareCtx>::from_tree(top)?;
173172
BareCtx::top_level_checks(&sub)?;
174173
Bare::new(sub)
175174
}
176-
);
175+
}
177176

178-
impl_from_str!(
179-
Bare<Pk>,
180-
type Err = Error;,
177+
impl<Pk: crate::FromStrKey> core::str::FromStr for Bare<Pk> {
178+
type Err = Error;
181179
fn from_str(s: &str) -> Result<Self, Self::Err> {
182180
let desc_str = verify_checksum(s)?;
183181
let top = expression::Tree::from_str(desc_str)?;
184182
Self::from_tree(&top)
185183
}
186-
);
184+
}
187185

188186
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {
189187
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool {
@@ -366,8 +364,7 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Pkh<Pk> {
366364
}
367365
}
368366

369-
impl_from_tree!(
370-
Pkh<Pk>,
367+
impl<Pk: crate::FromStrKey> FromTree for Pkh<Pk> {
371368
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
372369
if top.name == "pkh" && top.args.len() == 1 {
373370
Ok(Pkh::new(expression::terminal(&top.args[0], |pk| Pk::from_str(pk))?)?)
@@ -379,17 +376,16 @@ impl_from_tree!(
379376
)))
380377
}
381378
}
382-
);
379+
}
383380

384-
impl_from_str!(
385-
Pkh<Pk>,
386-
type Err = Error;,
381+
impl<Pk: crate::FromStrKey> core::str::FromStr for Pkh<Pk> {
382+
type Err = Error;
387383
fn from_str(s: &str) -> Result<Self, Self::Err> {
388384
let desc_str = verify_checksum(s)?;
389385
let top = expression::Tree::from_str(desc_str)?;
390386
Self::from_tree(&top)
391387
}
392-
);
388+
}
393389

394390
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
395391
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool { pred(&self.pk) }

src/descriptor/mod.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -918,8 +918,7 @@ impl Descriptor<DefiniteDescriptorKey> {
918918
}
919919
}
920920

921-
impl_from_tree!(
922-
Descriptor<Pk>,
921+
impl<Pk: crate::FromStrKey> crate::expression::FromTree for Descriptor<Pk> {
923922
/// Parse an expression tree into a descriptor.
924923
fn from_tree(top: &expression::Tree) -> Result<Descriptor<Pk>, Error> {
925924
Ok(match (top.name, top.args.len() as u32) {
@@ -931,11 +930,10 @@ impl_from_tree!(
931930
_ => Descriptor::Bare(Bare::from_tree(top)?),
932931
})
933932
}
934-
);
933+
}
935934

936-
impl_from_str!(
937-
Descriptor<Pk>,
938-
type Err = Error;,
935+
impl<Pk: crate::FromStrKey> FromStr for Descriptor<Pk> {
936+
type Err = Error;
939937
fn from_str(s: &str) -> Result<Descriptor<Pk>, Error> {
940938
// tr tree parsing has special code
941939
// Tr::from_str will check the checksum
@@ -950,7 +948,7 @@ impl_from_str!(
950948

951949
Ok(desc)
952950
}
953-
);
951+
}
954952

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

src/descriptor/segwitv0.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Wsh<Pk> {
231231
}
232232
}
233233

234-
impl_from_tree!(
235-
Wsh<Pk>,
234+
impl<Pk: crate::FromStrKey> crate::expression::FromTree for Wsh<Pk> {
236235
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
237236
if top.name == "wsh" && top.args.len() == 1 {
238237
let top = &top.args[0];
@@ -250,7 +249,7 @@ impl_from_tree!(
250249
)))
251250
}
252251
}
253-
);
252+
}
254253

255254
impl<Pk: MiniscriptKey> fmt::Debug for Wsh<Pk> {
256255
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -270,15 +269,14 @@ impl<Pk: MiniscriptKey> fmt::Display for Wsh<Pk> {
270269
}
271270
}
272271

273-
impl_from_str!(
274-
Wsh<Pk>,
275-
type Err = Error;,
272+
impl<Pk: crate::FromStrKey> core::str::FromStr for Wsh<Pk> {
273+
type Err = Error;
276274
fn from_str(s: &str) -> Result<Self, Self::Err> {
277275
let desc_str = verify_checksum(s)?;
278276
let top = expression::Tree::from_str(desc_str)?;
279277
Wsh::<Pk>::from_tree(&top)
280278
}
281-
);
279+
}
282280

283281
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wsh<Pk> {
284282
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool {
@@ -475,8 +473,7 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Wpkh<Pk> {
475473
}
476474
}
477475

478-
impl_from_tree!(
479-
Wpkh<Pk>,
476+
impl<Pk: crate::FromStrKey> crate::expression::FromTree for Wpkh<Pk> {
480477
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
481478
if top.name == "wpkh" && top.args.len() == 1 {
482479
Ok(Wpkh::new(expression::terminal(&top.args[0], |pk| Pk::from_str(pk))?)?)
@@ -488,17 +485,16 @@ impl_from_tree!(
488485
)))
489486
}
490487
}
491-
);
488+
}
492489

493-
impl_from_str!(
494-
Wpkh<Pk>,
495-
type Err = Error;,
490+
impl<Pk: crate::FromStrKey> core::str::FromStr for Wpkh<Pk> {
491+
type Err = Error;
496492
fn from_str(s: &str) -> Result<Self, Self::Err> {
497493
let desc_str = verify_checksum(s)?;
498494
let top = expression::Tree::from_str(desc_str)?;
499495
Self::from_tree(&top)
500496
}
501-
);
497+
}
502498

503499
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wpkh<Pk> {
504500
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool { pred(&self.pk) }

src/descriptor/sh.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ impl<Pk: MiniscriptKey> fmt::Display for Sh<Pk> {
8181
}
8282
}
8383

84-
impl_from_tree!(
85-
Sh<Pk>,
84+
impl<Pk: crate::FromStrKey> crate::expression::FromTree for Sh<Pk> {
8685
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
8786
if top.name == "sh" && top.args.len() == 1 {
8887
let top = &top.args[0];
@@ -105,17 +104,16 @@ impl_from_tree!(
105104
)))
106105
}
107106
}
108-
);
107+
}
109108

110-
impl_from_str!(
111-
Sh<Pk>,
112-
type Err = Error;,
109+
impl<Pk: crate::FromStrKey> core::str::FromStr for Sh<Pk> {
110+
type Err = Error;
113111
fn from_str(s: &str) -> Result<Self, Self::Err> {
114112
let desc_str = verify_checksum(s)?;
115113
let top = expression::Tree::from_str(desc_str)?;
116114
Self::from_tree(&top)
117115
}
118-
);
116+
}
119117

120118
impl<Pk: MiniscriptKey> Sh<Pk> {
121119
/// Get the Inner

src/descriptor/tr.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,7 @@ where
467467
}
468468

469469
#[rustfmt::skip]
470-
impl_block_str!(
471-
Tr<Pk>,
470+
impl<Pk: crate::FromStrKey> Tr<Pk> {
472471
// Helper function to parse taproot script path
473472
fn parse_tr_script_spend(tree: &expression::Tree,) -> Result<TapTree<Pk>, Error> {
474473
match tree {
@@ -487,10 +486,9 @@ impl_block_str!(
487486
)),
488487
}
489488
}
490-
);
489+
}
491490

492-
impl_from_tree!(
493-
Tr<Pk>,
491+
impl<Pk: crate::FromStrKey> crate::expression::FromTree for Tr<Pk> {
494492
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
495493
if top.name == "tr" {
496494
match top.args.len() {
@@ -530,17 +528,16 @@ impl_from_tree!(
530528
)))
531529
}
532530
}
533-
);
531+
}
534532

535-
impl_from_str!(
536-
Tr<Pk>,
537-
type Err = Error;,
533+
impl<Pk: crate::FromStrKey> core::str::FromStr for Tr<Pk> {
534+
type Err = Error;
538535
fn from_str(s: &str) -> Result<Self, Self::Err> {
539536
let desc_str = verify_checksum(s)?;
540537
let top = parse_tr_tree(desc_str)?;
541538
Self::from_tree(&top)
542539
}
543-
);
540+
}
544541

545542
impl<Pk: MiniscriptKey> fmt::Debug for Tr<Pk> {
546543
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

src/macros.rs

-67
Original file line numberDiff line numberDiff line change
@@ -18,73 +18,6 @@ macro_rules! policy_str {
1818
($($arg:tt)*) => ($crate::policy::Concrete::from_str(&format!($($arg)*)).unwrap())
1919
}
2020

21-
/// Macro for implementing FromTree trait. This avoids copying all the Pk::Associated type bounds
22-
/// throughout the codebase.
23-
macro_rules! impl_from_tree {
24-
($(;$gen:ident; $gen_con:ident, )* $name: ty,
25-
$(#[$meta:meta])*
26-
fn $fn:ident ( $($arg:ident : $type:ty),* ) -> $ret:ty
27-
$body:block
28-
) => {
29-
impl<Pk $(, $gen)*> $crate::expression::FromTree for $name
30-
where
31-
Pk: $crate::FromStrKey,
32-
$($gen : $gen_con,)*
33-
{
34-
35-
$(#[$meta])*
36-
fn $fn($($arg: $type)* ) -> $ret {
37-
$body
38-
}
39-
}
40-
};
41-
}
42-
43-
/// Macro for implementing FromStr trait. This avoids copying all the Pk::Associated type bounds
44-
/// throughout the codebase.
45-
macro_rules! impl_from_str {
46-
($(;$gen:ident; $gen_con:ident, )* $name: ty,
47-
type Err = $err_ty:ty;,
48-
$(#[$meta:meta])*
49-
fn $fn:ident ( $($arg:ident : $type:ty),* ) -> $ret:ty
50-
$body:block
51-
) => {
52-
impl<Pk $(, $gen)*> core::str::FromStr for $name
53-
where
54-
Pk: $crate::FromStrKey,
55-
$($gen : $gen_con,)*
56-
{
57-
type Err = $err_ty;
58-
59-
$(#[$meta])*
60-
fn $fn($($arg: $type)* ) -> $ret {
61-
$body
62-
}
63-
}
64-
};
65-
}
66-
67-
/// Macro for impl Struct with associated bounds. This avoids copying all the Pk::Associated type bounds
68-
/// throughout the codebase.
69-
macro_rules! impl_block_str {
70-
($(;$gen:ident; $gen_con:ident, )* $name: ty,
71-
$(#[$meta:meta])*
72-
$v:vis fn $fn:ident ( $($arg:ident : $type:ty, )* ) -> $ret:ty
73-
$body:block
74-
) => {
75-
impl<Pk $(, $gen)*> $name
76-
where
77-
Pk: $crate::FromStrKey,
78-
$($gen : $gen_con,)*
79-
{
80-
$(#[$meta])*
81-
$v fn $fn($($arg: $type,)* ) -> $ret {
82-
$body
83-
}
84-
}
85-
};
86-
}
87-
8821
/// A macro that implements serde serialization and deserialization using the
8922
/// `fmt::Display` and `str::FromStr` traits.
9023
macro_rules! serde_string_impl_pk {

src/miniscript/astelem.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,15 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> fmt::Display for Terminal<Pk, Ctx> {
240240
}
241241
}
242242

243-
impl_from_tree!(
244-
;Ctx; ScriptContext,
245-
Arc<Terminal<Pk, Ctx>>,
243+
impl<Pk: crate::FromStrKey, Ctx: ScriptContext> crate::expression::FromTree
244+
for Arc<Terminal<Pk, Ctx>>
245+
{
246246
fn from_tree(top: &expression::Tree) -> Result<Arc<Terminal<Pk, Ctx>>, Error> {
247247
Ok(Arc::new(expression::FromTree::from_tree(top)?))
248248
}
249-
);
249+
}
250250

251-
impl_from_tree!(
252-
;Ctx; ScriptContext,
253-
Terminal<Pk, Ctx>,
251+
impl<Pk: crate::FromStrKey, Ctx: ScriptContext> crate::expression::FromTree for Terminal<Pk, Ctx> {
254252
fn from_tree(top: &expression::Tree) -> Result<Terminal<Pk, Ctx>, Error> {
255253
let mut aliased_wrap;
256254
let frag_name;
@@ -303,9 +301,13 @@ impl_from_tree!(
303301
("pk_k", 1) => {
304302
expression::terminal(&top.args[0], |x| Pk::from_str(x).map(Terminal::PkK))
305303
}
306-
("pk_h", 1) => expression::terminal(&top.args[0], |x| Pk::from_str(x).map(Terminal::PkH)),
304+
("pk_h", 1) => {
305+
expression::terminal(&top.args[0], |x| Pk::from_str(x).map(Terminal::PkH))
306+
}
307307
("after", 1) => expression::terminal(&top.args[0], |x| {
308-
expression::parse_num(x).map(|x| Terminal::After(AbsLockTime::from(absolute::LockTime::from_consensus(x))))
308+
expression::parse_num(x).map(|x| {
309+
Terminal::After(AbsLockTime::from(absolute::LockTime::from_consensus(x)))
310+
})
309311
}),
310312
("older", 1) => expression::terminal(&top.args[0], |x| {
311313
expression::parse_num(x).map(|x| Terminal::Older(Sequence::from_consensus(x)))
@@ -424,7 +426,7 @@ impl_from_tree!(
424426
Ctx::check_global_validity(&ms)?;
425427
Ok(ms.node)
426428
}
427-
);
429+
}
428430

429431
/// Helper trait to add a `push_astelem` method to `script::Builder`
430432
trait PushAstElem<Pk: MiniscriptKey, Ctx: ScriptContext> {

0 commit comments

Comments
 (0)