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/// Blanket trait describing a key where all associated types implement `FromStr`,
21/// and all `FromStr` errors can be displayed.
22pub trait FromStrKey:
23    MiniscriptKey<
24        Sha256 = Self::_Sha256,
25        Hash256 = Self::_Hash256,
26        Ripemd160 = Self::_Ripemd160,
27        Hash160 = Self::_Hash160,
28    > + FromStr<Err = Self::_FromStrErr>
29{
30    /// Dummy type. Do not use.
31    type _Sha256: FromStr<Err = Self::_Sha256FromStrErr>
32        + Clone
33        + Eq
34        + Ord
35        + fmt::Display
36        + fmt::Debug
37        + hash::Hash;
38    /// Dummy type. Do not use.
39    type _Sha256FromStrErr: fmt::Debug + fmt::Display;
40    /// Dummy type. Do not use.
41    type _Hash256: FromStr<Err = Self::_Hash256FromStrErr>
42        + Clone
43        + Eq
44        + Ord
45        + fmt::Display
46        + fmt::Debug
47        + hash::Hash;
48    /// Dummy type. Do not use.
49    type _Hash256FromStrErr: fmt::Debug + fmt::Display;
50    /// Dummy type. Do not use.
51    type _Ripemd160: FromStr<Err = Self::_Ripemd160FromStrErr>
52        + Clone
53        + Eq
54        + Ord
55        + fmt::Display
56        + fmt::Debug
57        + hash::Hash;
58    /// Dummy type. Do not use.
59    type _Ripemd160FromStrErr: fmt::Debug + fmt::Display;
60    /// Dummy type. Do not use.
61    type _Hash160: FromStr<Err = Self::_Hash160FromStrErr>
62        + Clone
63        + Eq
64        + Ord
65        + fmt::Display
66        + fmt::Debug
67        + hash::Hash;
68    /// Dummy type. Do not use.
69    type _Hash160FromStrErr: fmt::Debug + fmt::Display;
70    /// Dummy type. Do not use.
71    type _FromStrErr: fmt::Debug + fmt::Display;
72}
73
74impl<T> FromStrKey for T
75where
76    Self: MiniscriptKey + FromStr,
77    <Self as MiniscriptKey>::Sha256: FromStr,
78    Self::Hash256: FromStr,
79    Self::Ripemd160: FromStr,
80    Self::Hash160: FromStr,
81    <Self as FromStr>::Err: fmt::Debug + fmt::Display,
82    <<Self as MiniscriptKey>::Sha256 as FromStr>::Err: fmt::Debug + fmt::Display,
83    <Self::Hash256 as FromStr>::Err: fmt::Debug + fmt::Display,
84    <Self::Ripemd160 as FromStr>::Err: fmt::Debug + fmt::Display,
85    <Self::Hash160 as FromStr>::Err: fmt::Debug + fmt::Display,
86{
87    type _Sha256 = <T as MiniscriptKey>::Sha256;
88    type _Sha256FromStrErr = <<T as MiniscriptKey>::Sha256 as FromStr>::Err;
89    type _Hash256 = <T as MiniscriptKey>::Hash256;
90    type _Hash256FromStrErr = <<T as MiniscriptKey>::Hash256 as FromStr>::Err;
91    type _Ripemd160 = <T as MiniscriptKey>::Ripemd160;
92    type _Ripemd160FromStrErr = <<T as MiniscriptKey>::Ripemd160 as FromStr>::Err;
93    type _Hash160 = <T as MiniscriptKey>::Hash160;
94    type _Hash160FromStrErr = <<T as MiniscriptKey>::Hash160 as FromStr>::Err;
95    type _FromStrErr = <T as FromStr>::Err;
96}