11use core:: fmt:: { Debug , Display , Formatter , Result } ;
22
3+ use crate :: prelude:: * ;
4+
35/// This wraps an actual backtrace to achieve two things:
46/// - being able to fill this with a stub implementation in `no_std` environments
57/// - being able to use this in conjunction with [`thiserror::Error`]
@@ -9,10 +11,17 @@ impl BT {
911 #[ track_caller]
1012 pub fn capture ( ) -> Self {
1113 // in case of no_std, we can fill with a stub here
12- #[ cfg( target_arch = "wasm32" ) ]
13- return BT ( Box :: new ( std:: backtrace:: Backtrace :: disabled ( ) ) ) ;
14- #[ cfg( not( target_arch = "wasm32" ) ) ]
15- return BT ( Box :: new ( std:: backtrace:: Backtrace :: capture ( ) ) ) ;
14+ #[ cfg( feature = "std" ) ]
15+ {
16+ #[ cfg( target_arch = "wasm32" ) ]
17+ return BT ( Box :: new ( std:: backtrace:: Backtrace :: disabled ( ) ) ) ;
18+ #[ cfg( not( target_arch = "wasm32" ) ) ]
19+ return BT ( Box :: new ( std:: backtrace:: Backtrace :: capture ( ) ) ) ;
20+ }
21+ #[ cfg( not( feature = "std" ) ) ]
22+ {
23+ BT ( Box :: new ( Stub ) )
24+ }
1625 }
1726}
1827
@@ -31,6 +40,21 @@ impl Display for BT {
3140 }
3241}
3342
43+ #[ allow( unused) ]
44+ struct Stub ;
45+
46+ impl Debug for Stub {
47+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result {
48+ write ! ( f, "<disabled>" )
49+ }
50+ }
51+
52+ impl Display for Stub {
53+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result {
54+ write ! ( f, "<disabled>" )
55+ }
56+ }
57+
3458/// This macro implements `From` for a given error type to a given error type where
3559/// the target error has a `backtrace` field.
3660/// This is meant as a replacement for `thiserror`'s `#[from]` attribute, which does not
0 commit comments