Skip to content

Commit

Permalink
Fix some macro bugs for 1.41.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sanket1729 committed Jun 6, 2022
1 parent 197365c commit 397b38e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ impl Descriptor<DescriptorPublicKey> {

impl_from_tree!(
Descriptor<Pk>,
/// Parse an expression tree into a descriptor
/// 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) {
("pkh", 1) => Descriptor::Pkh(Pkh::from_tree(top)?),
Expand Down
3 changes: 2 additions & 1 deletion src/descriptor/tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,11 @@ where
}
}

#[rustfmt::skip]
impl_block_str!(
Tr<Pk>,
// Helper function to parse taproot script path
fn parse_tr_script_spend(tree: &expression::Tree) -> Result<TapTree<Pk>, Error> {
fn parse_tr_script_spend(tree: &expression::Tree,) -> Result<TapTree<Pk>, Error> {
match tree {
expression::Tree { name, args } if !name.is_empty() && args.is_empty() => {
let script = Miniscript::<Pk, Tap>::from_str(name)?;
Expand Down
37 changes: 31 additions & 6 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ macro_rules! policy_str {
/// 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, $fn: item) => {
($(;$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: MiniscriptKey + core::str::FromStr,
Expand All @@ -30,15 +34,24 @@ macro_rules! impl_from_tree {
<<Pk as MiniscriptKey>::Sha256Hash as core::str::FromStr>::Err: $crate::prelude::ToString,
$($gen : $gen_con,)*
{
$fn

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

/// Macro for implementing FromTree 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 $(, $it: item)*) => {
($(;$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: MiniscriptKey + core::str::FromStr,
Expand All @@ -49,15 +62,24 @@ macro_rules! impl_from_str {
<<Pk as MiniscriptKey>::Sha256Hash as core::str::FromStr>::Err: $crate::prelude::ToString,
$($gen : $gen_con,)*
{
$($it)*
type Err = $err_ty;

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

/// Macro for implementing FromTree trait. This avoids copying all the Pk::Associated type bounds
/// throughout the codebase.
macro_rules! impl_block_str {
($(;$gen:ident; $gen_con:ident, )* $name: ty $(, $it: item)*) => {
($(;$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: MiniscriptKey + core::str::FromStr,
Expand All @@ -68,7 +90,10 @@ macro_rules! impl_block_str {
<<Pk as MiniscriptKey>::Sha256Hash as core::str::FromStr>::Err: $crate::prelude::ToString,
$($gen : $gen_con,)*
{
$($it)*
$(#[$meta])*
$v fn $fn($($arg: $type,)* ) -> $ret {
$body
}
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/miniscript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ impl_block_str!(
/// Some of the analysis guarantees of miniscript are lost when dealing with
/// insane scripts. In general, in a multi-party setting users should only
/// accept sane scripts.
pub fn from_str_insane(s: &str) -> Result<Miniscript<Pk, Ctx>, Error>
pub fn from_str_insane(s: &str,) -> Result<Miniscript<Pk, Ctx>, Error>
{
// This checks for invalid ASCII chars
let top = expression::Tree::from_str(s)?;
Expand Down
8 changes: 4 additions & 4 deletions src/policy/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,14 +726,14 @@ impl_from_str!(

serde_string_impl_pk!(Policy, "a miniscript concrete policy");

#[rustfmt::skip]
impl_block_str!(
Policy<Pk>,
/// Helper function for `from_tree` to parse subexpressions with
/// names of the form x@y
fn from_tree_prob(
top: &expression::Tree,
allow_prob: bool,
) -> Result<(usize, Policy<Pk>), Error> {
fn from_tree_prob(top: &expression::Tree, allow_prob: bool,)
-> Result<(usize, Policy<Pk>), Error>
{
let frag_prob;
let frag_name;
let mut name_split = top.name.split('@');
Expand Down

0 comments on commit 397b38e

Please sign in to comment.