2
2
3
3
use core:: fmt:: { self , Debug , Display } ;
4
4
5
- #[ cfg( feature = "std " ) ]
6
- use std :: boxed:: Box ;
5
+ #[ cfg( feature = "alloc " ) ]
6
+ use alloc :: boxed:: Box ;
7
7
8
8
/// Result type.
9
9
///
@@ -16,17 +16,16 @@ pub type Result<T> = core::result::Result<T, Error>;
16
16
/// could potentially be used recover signing private keys or forge signatures
17
17
/// (e.g. [BB'06]).
18
18
///
19
- /// When the `std` feature is enabled, it impls [`std::error::Error`] and
20
- /// supports an optional [`std::error::Error::source`], which can be used by
21
- /// things like remote signers (e.g. HSM, KMS) to report I/O or auth errors.
19
+ /// When the `alloc` feature is enabled, it supports an optional [`core::error::Error::source`],
20
+ /// which can be used by things like remote signers (e.g. HSM, KMS) to report I/O or auth errors.
22
21
///
23
22
/// [BB'06]: https://en.wikipedia.org/wiki/Daniel_Bleichenbacher
24
23
#[ derive( Default ) ]
25
24
#[ non_exhaustive]
26
25
pub struct Error {
27
26
/// Source of the error (if applicable).
28
- #[ cfg( feature = "std " ) ]
29
- source : Option < Box < dyn std :: error:: Error + Send + Sync + ' static > > ,
27
+ #[ cfg( feature = "alloc " ) ]
28
+ source : Option < Box < dyn core :: error:: Error + Send + Sync + ' static > > ,
30
29
}
31
30
32
31
impl Error {
@@ -41,9 +40,9 @@ impl Error {
41
40
/// errors e.g. signature parsing or verification errors. The intended use
42
41
/// cases are for propagating errors related to external signers, e.g.
43
42
/// communication/authentication errors with HSMs, KMS, etc.
44
- #[ cfg( feature = "std " ) ]
43
+ #[ cfg( feature = "alloc " ) ]
45
44
pub fn from_source (
46
- source : impl Into < Box < dyn std :: error:: Error + Send + Sync + ' static > > ,
45
+ source : impl Into < Box < dyn core :: error:: Error + Send + Sync + ' static > > ,
47
46
) -> Self {
48
47
Self {
49
48
source : Some ( source. into ( ) ) ,
@@ -52,12 +51,12 @@ impl Error {
52
51
}
53
52
54
53
impl Debug for Error {
55
- #[ cfg( not( feature = "std " ) ) ]
54
+ #[ cfg( not( feature = "alloc " ) ) ]
56
55
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
57
56
f. write_str ( "signature::Error {}" )
58
57
}
59
58
60
- #[ cfg( feature = "std " ) ]
59
+ #[ cfg( feature = "alloc " ) ]
61
60
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
62
61
f. write_str ( "signature::Error { source: " ) ?;
63
62
@@ -77,31 +76,37 @@ impl Display for Error {
77
76
}
78
77
}
79
78
80
- #[ cfg( feature = "std " ) ]
81
- impl From < Box < dyn std :: error:: Error + Send + Sync + ' static > > for Error {
82
- fn from ( source : Box < dyn std :: error:: Error + Send + Sync + ' static > ) -> Error {
79
+ #[ cfg( feature = "alloc " ) ]
80
+ impl From < Box < dyn core :: error:: Error + Send + Sync + ' static > > for Error {
81
+ fn from ( source : Box < dyn core :: error:: Error + Send + Sync + ' static > ) -> Error {
83
82
Self :: from_source ( source)
84
83
}
85
84
}
86
85
87
86
#[ cfg( feature = "rand_core" ) ]
88
87
impl From < rand_core:: Error > for Error {
89
- #[ cfg( not( feature = "std " ) ) ]
88
+ #[ cfg( not( feature = "alloc " ) ) ]
90
89
fn from ( _source : rand_core:: Error ) -> Error {
91
90
Error :: new ( )
92
91
}
93
92
94
- #[ cfg( feature = "std " ) ]
93
+ #[ cfg( feature = "alloc " ) ]
95
94
fn from ( source : rand_core:: Error ) -> Error {
96
95
Error :: from_source ( source)
97
96
}
98
97
}
99
98
100
- #[ cfg( feature = "std" ) ]
101
- impl std:: error:: Error for Error {
102
- fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
103
- self . source
104
- . as_ref ( )
105
- . map ( |source| source. as_ref ( ) as & ( dyn std:: error:: Error + ' static ) )
99
+ impl core:: error:: Error for Error {
100
+ fn source ( & self ) -> Option < & ( dyn core:: error:: Error + ' static ) > {
101
+ #[ cfg( not( feature = "alloc" ) ) ]
102
+ {
103
+ None
104
+ }
105
+ #[ cfg( feature = "alloc" ) ]
106
+ {
107
+ self . source
108
+ . as_ref ( )
109
+ . map ( |source| source. as_ref ( ) as & ( dyn core:: error:: Error + ' static ) )
110
+ }
106
111
}
107
112
}
0 commit comments