Skip to main content

miniscript/
blanket_traits.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! Blanket Traits
4//!
5//! Because of this library's heavy use of generics, we often require complicated
6//! trait bounds (especially when it comes to [`FromStr`] and its
7//! associated error types). These blanket traits act as aliases, allowing easier
8//! descriptions of them.
9//!
10//! While these traits are not sealed, they have blanket-impls which prevent you
11//! from directly implementing them on your own types. The traits will be
12//! automatically implemented if you satisfy all the bounds.
13//!
14
15use core::str::FromStr;
16use core::{fmt, hash};
17
18use crate::MiniscriptKey;
19
20/// Auxiliary trait indicating that a type implements both `Debug`, `Display`, `Send` and `Sync`.
21// NOTE: `Send` / `Sync` is required to maintain compatibility with downstream error handling libraries.
22pub trait StaticDebugAndDisplay: fmt::Debug + fmt::Display + Send + Sync + 'static {}
23
24impl<T: fmt::Debug + fmt::Display + Send + Sync + 'static> StaticDebugAndDisplay for T {}
25
26/// Blanket trait describing a key where all associated types implement `FromStr`,
27/// and all `FromStr` errors can be displayed.
28pub trait FromStrKey:
29    MiniscriptKey<
30        Sha256 = Self::_Sha256,
31        Hash256 = Self::_Hash256,
32        Ripemd160 = Self::_Ripemd160,
33        Hash160 = Self::_Hash160,
34    > + FromStr<Err = Self::_FromStrErr>
35{
36    /// Dummy type. Do not use.
37    type _Sha256: FromStr<Err = Self::_Sha256FromStrErr>
38        + Clone
39        + Eq
40        + Ord
41        + fmt::Display
42        + fmt::Debug
43        + hash::Hash;
44    /// Dummy type. Do not use.
45    type _Sha256FromStrErr: StaticDebugAndDisplay;
46    /// Dummy type. Do not use.
47    type _Hash256: FromStr<Err = Self::_Hash256FromStrErr>
48        + Clone
49        + Eq
50        + Ord
51        + fmt::Display
52        + fmt::Debug
53        + hash::Hash;
54    /// Dummy type. Do not use.
55    type _Hash256FromStrErr: StaticDebugAndDisplay;
56    /// Dummy type. Do not use.
57    type _Ripemd160: FromStr<Err = Self::_Ripemd160FromStrErr>
58        + Clone
59        + Eq
60        + Ord
61        + fmt::Display
62        + fmt::Debug
63        + hash::Hash;
64    /// Dummy type. Do not use.
65    type _Ripemd160FromStrErr: StaticDebugAndDisplay;
66    /// Dummy type. Do not use.
67    type _Hash160: FromStr<Err = Self::_Hash160FromStrErr>
68        + Clone
69        + Eq
70        + Ord
71        + fmt::Display
72        + fmt::Debug
73        + hash::Hash;
74    /// Dummy type. Do not use.
75    type _Hash160FromStrErr: StaticDebugAndDisplay;
76    /// Dummy type. Do not use.
77    type _FromStrErr: StaticDebugAndDisplay;
78}
79
80impl<T> FromStrKey for T
81where
82    Self: MiniscriptKey + FromStr,
83    <Self as MiniscriptKey>::Sha256: FromStr,
84    Self::Hash256: FromStr,
85    Self::Ripemd160: FromStr,
86    Self::Hash160: FromStr,
87    <Self as FromStr>::Err: StaticDebugAndDisplay,
88    <<Self as MiniscriptKey>::Sha256 as FromStr>::Err: StaticDebugAndDisplay,
89    <Self::Hash256 as FromStr>::Err: StaticDebugAndDisplay,
90    <Self::Ripemd160 as FromStr>::Err: StaticDebugAndDisplay,
91    <Self::Hash160 as FromStr>::Err: StaticDebugAndDisplay,
92{
93    type _Sha256 = <T as MiniscriptKey>::Sha256;
94    type _Sha256FromStrErr = <<T as MiniscriptKey>::Sha256 as FromStr>::Err;
95    type _Hash256 = <T as MiniscriptKey>::Hash256;
96    type _Hash256FromStrErr = <<T as MiniscriptKey>::Hash256 as FromStr>::Err;
97    type _Ripemd160 = <T as MiniscriptKey>::Ripemd160;
98    type _Ripemd160FromStrErr = <<T as MiniscriptKey>::Ripemd160 as FromStr>::Err;
99    type _Hash160 = <T as MiniscriptKey>::Hash160;
100    type _Hash160FromStrErr = <<T as MiniscriptKey>::Hash160 as FromStr>::Err;
101    type _FromStrErr = <T as FromStr>::Err;
102}