Skip to content

Commit 4f52fb7

Browse files
committed
introduce StaticDebugAndDisplay blanket trait, use it for all errors
We will want to start boxing our errors to avoid generic error types (and to reduce the size of error types, by collapsing multiple variants into one). In order to do this, we need a trait that we can box which expresses fmt::Debug and fmt::Display. (Ideally we would also bound on std::error::Error but we can't express this bound without std until Rust 1.81, and we can't have a std-only bound since it would make the std feature non-additive.) This IS A BREAKING CHANGE because it now requires all key parsing errors to have a 'static bound. But the alternatives are pretty bad: * We could continue dropping parsing errors and replacing them with Strings (or not replacing them at all..); or * We could add a <Pk> bound to all of our error types
1 parent b11cdc2 commit 4f52fb7

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/blanket_traits.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ use core::{fmt, hash};
1717

1818
use crate::MiniscriptKey;
1919

20+
/// Auxiliary trait indicating that a type implements both `Debug` and `Display`.
21+
pub trait StaticDebugAndDisplay: fmt::Debug + fmt::Display + 'static {}
22+
23+
impl<T: fmt::Debug + fmt::Display + 'static> StaticDebugAndDisplay for T {}
24+
2025
/// Blanket trait describing a key where all associated types implement `FromStr`,
2126
/// and all `FromStr` errors can be displayed.
2227
pub trait FromStrKey:
@@ -36,7 +41,7 @@ pub trait FromStrKey:
3641
+ fmt::Debug
3742
+ hash::Hash;
3843
/// Dummy type. Do not use.
39-
type _Sha256FromStrErr: fmt::Debug + fmt::Display;
44+
type _Sha256FromStrErr: StaticDebugAndDisplay;
4045
/// Dummy type. Do not use.
4146
type _Hash256: FromStr<Err = Self::_Hash256FromStrErr>
4247
+ Clone
@@ -46,7 +51,7 @@ pub trait FromStrKey:
4651
+ fmt::Debug
4752
+ hash::Hash;
4853
/// Dummy type. Do not use.
49-
type _Hash256FromStrErr: fmt::Debug + fmt::Display;
54+
type _Hash256FromStrErr: StaticDebugAndDisplay;
5055
/// Dummy type. Do not use.
5156
type _Ripemd160: FromStr<Err = Self::_Ripemd160FromStrErr>
5257
+ Clone
@@ -56,7 +61,7 @@ pub trait FromStrKey:
5661
+ fmt::Debug
5762
+ hash::Hash;
5863
/// Dummy type. Do not use.
59-
type _Ripemd160FromStrErr: fmt::Debug + fmt::Display;
64+
type _Ripemd160FromStrErr: StaticDebugAndDisplay;
6065
/// Dummy type. Do not use.
6166
type _Hash160: FromStr<Err = Self::_Hash160FromStrErr>
6267
+ Clone
@@ -66,9 +71,9 @@ pub trait FromStrKey:
6671
+ fmt::Debug
6772
+ hash::Hash;
6873
/// Dummy type. Do not use.
69-
type _Hash160FromStrErr: fmt::Debug + fmt::Display;
74+
type _Hash160FromStrErr: StaticDebugAndDisplay;
7075
/// Dummy type. Do not use.
71-
type _FromStrErr: fmt::Debug + fmt::Display;
76+
type _FromStrErr: StaticDebugAndDisplay;
7277
}
7378

7479
impl<T> FromStrKey for T
@@ -78,11 +83,11 @@ where
7883
Self::Hash256: FromStr,
7984
Self::Ripemd160: FromStr,
8085
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+
<Self as FromStr>::Err: StaticDebugAndDisplay,
87+
<<Self as MiniscriptKey>::Sha256 as FromStr>::Err: StaticDebugAndDisplay,
88+
<Self::Hash256 as FromStr>::Err: StaticDebugAndDisplay,
89+
<Self::Ripemd160 as FromStr>::Err: StaticDebugAndDisplay,
90+
<Self::Hash160 as FromStr>::Err: StaticDebugAndDisplay,
8691
{
8792
type _Sha256 = <T as MiniscriptKey>::Sha256;
8893
type _Sha256FromStrErr = <<T as MiniscriptKey>::Sha256 as FromStr>::Err;

0 commit comments

Comments
 (0)