Skip to content

Commit

Permalink
node: some more utility methods on Inner
Browse files Browse the repository at this point in the history
  • Loading branch information
apoelstra committed Jul 13, 2023
1 parent a516651 commit 2adb0a8
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/node/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ impl<C, J, X: Disconnectable<C>, W> Inner<C, J, X, W> {
}
}

impl<C, J, X, W> Inner<Option<C>, J, Option<X>, W> {
impl<C, J, X, W> Inner<Option<C>, J, X, W> {
/// Convert an `Inner<Option<C>, J, W>` to an `Option<Inner<C, J, W>>`.
pub fn transpose(self) -> Option<Inner<C, J, X, W>> {
Some(match self {
Expand All @@ -318,7 +318,31 @@ impl<C, J, X, W> Inner<Option<C>, J, Option<X>, W> {
Inner::AssertL(c, cmr) => Inner::AssertL(c?, cmr),
Inner::AssertR(cmr, c) => Inner::AssertR(cmr, c?),
Inner::Pair(cl, cr) => Inner::Pair(cl?, cr?),
Inner::Disconnect(cl, cr) => Inner::Disconnect(cl?, cr?),
Inner::Disconnect(cl, cr) => Inner::Disconnect(cl?, cr),
Inner::Witness(w) => Inner::Witness(w),
Inner::Fail(entropy) => Inner::Fail(entropy),
Inner::Jet(j) => Inner::Jet(j),
Inner::Word(w) => Inner::Word(w),
})
}
}

impl<C, J, X, W> Inner<C, J, Option<X>, W> {
/// Convert an `Inner<Option<C>, J, W>` to an `Option<Inner<C, J, W>>`.
pub fn transpose_disconnect(self) -> Option<Inner<C, J, X, W>> {
Some(match self {
Inner::Iden => Inner::Iden,
Inner::Unit => Inner::Unit,
Inner::InjL(c) => Inner::InjL(c),
Inner::InjR(c) => Inner::InjR(c),
Inner::Take(c) => Inner::Take(c),
Inner::Drop(c) => Inner::Drop(c),
Inner::Comp(cl, cr) => Inner::Comp(cl, cr),
Inner::Case(cl, cr) => Inner::Case(cl, cr),
Inner::AssertL(c, cmr) => Inner::AssertL(c, cmr),
Inner::AssertR(cmr, c) => Inner::AssertR(cmr, c),
Inner::Pair(cl, cr) => Inner::Pair(cl, cr),
Inner::Disconnect(cl, cr) => Inner::Disconnect(cl, cr?),
Inner::Witness(w) => Inner::Witness(w),
Inner::Fail(entropy) => Inner::Fail(entropy),
Inner::Jet(j) => Inner::Jet(j),
Expand Down Expand Up @@ -351,7 +375,7 @@ impl<C, J, X, W> Inner<C, J, X, Option<W>> {
}
}

impl<C, J: Clone, X, W: Clone> Inner<C, J, X, &W> {
impl<C, J: Clone, X, W: Copy> Inner<C, J, X, &W> {
/// Copies witness data.
///
/// Useful in conjunction with [`Inner::as_ref`] when you don't want to
Expand All @@ -361,6 +385,16 @@ impl<C, J: Clone, X, W: Clone> Inner<C, J, X, &W> {
}
}

impl<C, J: Clone, X: Copy, W> Inner<C, J, &X, W> {
/// Copies disconnect data.
///
/// Useful in conjunction with [`Inner::as_ref`] when you don't want to
/// take a reference to disconnect data.
pub fn copy_disconnect(self) -> Inner<C, J, X, W> {
self.map_disconnect(X::clone)
}
}

impl<C, J: fmt::Display, X, W> fmt::Display for Inner<C, J, X, W> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down

0 comments on commit 2adb0a8

Please sign in to comment.