Skip to content

Commit

Permalink
Replace macros with blanket impls
Browse files Browse the repository at this point in the history
  • Loading branch information
apoelstra committed Mar 4, 2024
1 parent 5ec0f44 commit 270e65d
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 171 deletions.
24 changes: 10 additions & 14 deletions src/descriptor/bare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,24 +166,22 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Bare<Pk> {
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> { self.ms.lift() }
}

impl_from_tree!(
Bare<Pk>,
impl<Pk: crate::FromStrKey> FromTree for Bare<Pk> {
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
let sub = Miniscript::<Pk, BareCtx>::from_tree(top)?;
BareCtx::top_level_checks(&sub)?;
Bare::new(sub)
}
);
}

impl_from_str!(
Bare<Pk>,
type Err = Error;,
impl<Pk: crate::FromStrKey> core::str::FromStr for Bare<Pk> {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let desc_str = verify_checksum(s)?;
let top = expression::Tree::from_str(desc_str)?;
Self::from_tree(&top)
}
);
}

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

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

impl_from_str!(
Pkh<Pk>,
type Err = Error;,
impl<Pk: crate::FromStrKey> core::str::FromStr for Pkh<Pk> {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let desc_str = verify_checksum(s)?;
let top = expression::Tree::from_str(desc_str)?;
Self::from_tree(&top)
}
);
}

impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool { pred(&self.pk) }
Expand Down
12 changes: 5 additions & 7 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,8 +918,7 @@ impl Descriptor<DefiniteDescriptorKey> {
}
}

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

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

Ok(desc)
}
);
}

impl<Pk: MiniscriptKey> fmt::Debug for Descriptor<Pk> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
24 changes: 10 additions & 14 deletions src/descriptor/segwitv0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Wsh<Pk> {
}
}

impl_from_tree!(
Wsh<Pk>,
impl<Pk: crate::FromStrKey> crate::expression::FromTree for Wsh<Pk> {
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
if top.name == "wsh" && top.args.len() == 1 {
let top = &top.args[0];
Expand All @@ -250,7 +249,7 @@ impl_from_tree!(
)))
}
}
);
}

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

impl_from_str!(
Wsh<Pk>,
type Err = Error;,
impl<Pk: crate::FromStrKey> core::str::FromStr for Wsh<Pk> {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let desc_str = verify_checksum(s)?;
let top = expression::Tree::from_str(desc_str)?;
Wsh::<Pk>::from_tree(&top)
}
);
}

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

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

impl_from_str!(
Wpkh<Pk>,
type Err = Error;,
impl<Pk: crate::FromStrKey> core::str::FromStr for Wpkh<Pk> {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let desc_str = verify_checksum(s)?;
let top = expression::Tree::from_str(desc_str)?;
Self::from_tree(&top)
}
);
}

impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wpkh<Pk> {
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool { pred(&self.pk) }
Expand Down
12 changes: 5 additions & 7 deletions src/descriptor/sh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ impl<Pk: MiniscriptKey> fmt::Display for Sh<Pk> {
}
}

impl_from_tree!(
Sh<Pk>,
impl<Pk: crate::FromStrKey> crate::expression::FromTree for Sh<Pk> {
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
if top.name == "sh" && top.args.len() == 1 {
let top = &top.args[0];
Expand All @@ -105,17 +104,16 @@ impl_from_tree!(
)))
}
}
);
}

impl_from_str!(
Sh<Pk>,
type Err = Error;,
impl<Pk: crate::FromStrKey> core::str::FromStr for Sh<Pk> {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let desc_str = verify_checksum(s)?;
let top = expression::Tree::from_str(desc_str)?;
Self::from_tree(&top)
}
);
}

impl<Pk: MiniscriptKey> Sh<Pk> {
/// Get the Inner
Expand Down
17 changes: 7 additions & 10 deletions src/descriptor/tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,7 @@ where
}

#[rustfmt::skip]
impl_block_str!(
Tr<Pk>,
impl<Pk: crate::FromStrKey> Tr<Pk> {
// Helper function to parse taproot script path
fn parse_tr_script_spend(tree: &expression::Tree,) -> Result<TapTree<Pk>, Error> {
match tree {
Expand All @@ -487,10 +486,9 @@ impl_block_str!(
)),
}
}
);
}

impl_from_tree!(
Tr<Pk>,
impl<Pk: crate::FromStrKey> crate::expression::FromTree for Tr<Pk> {
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
if top.name == "tr" {
match top.args.len() {
Expand Down Expand Up @@ -530,17 +528,16 @@ impl_from_tree!(
)))
}
}
);
}

impl_from_str!(
Tr<Pk>,
type Err = Error;,
impl<Pk: crate::FromStrKey> core::str::FromStr for Tr<Pk> {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let desc_str = verify_checksum(s)?;
let top = parse_tr_tree(desc_str)?;
Self::from_tree(&top)
}
);
}

impl<Pk: MiniscriptKey> fmt::Debug for Tr<Pk> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
67 changes: 0 additions & 67 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,73 +18,6 @@ macro_rules! policy_str {
($($arg:tt)*) => ($crate::policy::Concrete::from_str(&format!($($arg)*)).unwrap())
}

/// Macro for implementing FromTree trait. This avoids copying all the Pk::Associated type bounds
/// throughout the codebase.
macro_rules! impl_from_tree {
($(;$gen:ident; $gen_con:ident, )* $name: ty,
$(#[$meta:meta])*
fn $fn:ident ( $($arg:ident : $type:ty),* ) -> $ret:ty
$body:block
) => {
impl<Pk $(, $gen)*> $crate::expression::FromTree for $name
where
Pk: $crate::FromStrKey,
$($gen : $gen_con,)*
{

$(#[$meta])*
fn $fn($($arg: $type)* ) -> $ret {
$body
}
}
};
}

/// Macro for implementing FromStr trait. This avoids copying all the Pk::Associated type bounds
/// throughout the codebase.
macro_rules! impl_from_str {
($(;$gen:ident; $gen_con:ident, )* $name: ty,
type Err = $err_ty:ty;,
$(#[$meta:meta])*
fn $fn:ident ( $($arg:ident : $type:ty),* ) -> $ret:ty
$body:block
) => {
impl<Pk $(, $gen)*> core::str::FromStr for $name
where
Pk: $crate::FromStrKey,
$($gen : $gen_con,)*
{
type Err = $err_ty;

$(#[$meta])*
fn $fn($($arg: $type)* ) -> $ret {
$body
}
}
};
}

/// Macro for impl Struct with associated bounds. This avoids copying all the Pk::Associated type bounds
/// throughout the codebase.
macro_rules! impl_block_str {
($(;$gen:ident; $gen_con:ident, )* $name: ty,
$(#[$meta:meta])*
$v:vis fn $fn:ident ( $($arg:ident : $type:ty, )* ) -> $ret:ty
$body:block
) => {
impl<Pk $(, $gen)*> $name
where
Pk: $crate::FromStrKey,
$($gen : $gen_con,)*
{
$(#[$meta])*
$v fn $fn($($arg: $type,)* ) -> $ret {
$body
}
}
};
}

/// A macro that implements serde serialization and deserialization using the
/// `fmt::Display` and `str::FromStr` traits.
macro_rules! serde_string_impl_pk {
Expand Down
22 changes: 12 additions & 10 deletions src/miniscript/astelem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,15 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> fmt::Display for Terminal<Pk, Ctx> {
}
}

impl_from_tree!(
;Ctx; ScriptContext,
Arc<Terminal<Pk, Ctx>>,
impl<Pk: crate::FromStrKey, Ctx: ScriptContext> crate::expression::FromTree
for Arc<Terminal<Pk, Ctx>>
{
fn from_tree(top: &expression::Tree) -> Result<Arc<Terminal<Pk, Ctx>>, Error> {
Ok(Arc::new(expression::FromTree::from_tree(top)?))
}
);
}

impl_from_tree!(
;Ctx; ScriptContext,
Terminal<Pk, Ctx>,
impl<Pk: crate::FromStrKey, Ctx: ScriptContext> crate::expression::FromTree for Terminal<Pk, Ctx> {
fn from_tree(top: &expression::Tree) -> Result<Terminal<Pk, Ctx>, Error> {
let mut aliased_wrap;
let frag_name;
Expand Down Expand Up @@ -303,9 +301,13 @@ impl_from_tree!(
("pk_k", 1) => {
expression::terminal(&top.args[0], |x| Pk::from_str(x).map(Terminal::PkK))
}
("pk_h", 1) => expression::terminal(&top.args[0], |x| Pk::from_str(x).map(Terminal::PkH)),
("pk_h", 1) => {
expression::terminal(&top.args[0], |x| Pk::from_str(x).map(Terminal::PkH))
}
("after", 1) => expression::terminal(&top.args[0], |x| {
expression::parse_num(x).map(|x| Terminal::After(AbsLockTime::from(absolute::LockTime::from_consensus(x))))
expression::parse_num(x).map(|x| {
Terminal::After(AbsLockTime::from(absolute::LockTime::from_consensus(x)))
})
}),
("older", 1) => expression::terminal(&top.args[0], |x| {
expression::parse_num(x).map(|x| Terminal::Older(Sequence::from_consensus(x)))
Expand Down Expand Up @@ -424,7 +426,7 @@ impl_from_tree!(
Ctx::check_global_validity(&ms)?;
Ok(ms.node)
}
);
}

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

0 comments on commit 270e65d

Please sign in to comment.