File tree Expand file tree Collapse file tree 3 files changed +64
-3
lines changed Expand file tree Collapse file tree 3 files changed +64
-3
lines changed Original file line number Diff line number Diff line change
1
+ use std:: env;
2
+ use std:: fs;
3
+ use std:: path:: Path ;
4
+ use std:: process:: { Command , ExitStatus , Stdio } ;
5
+
6
+ // This code exercises the surface area that we expect of the std Backtrace
7
+ // type. If the current toolchain is able to compile it, we go ahead and use
8
+ // backtrace in anyhow.
9
+ const PROBE : & str = r#"
10
+ #![feature(backtrace)]
11
+ #![allow(dead_code)]
12
+
13
+ use std::backtrace::{Backtrace, BacktraceStatus};
14
+ use std::error::Error;
15
+ use std::fmt::{self, Display};
16
+
17
+ #[derive(Debug)]
18
+ struct E;
19
+
20
+ impl Display for E {
21
+ fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
22
+ unimplemented!()
23
+ }
24
+ }
25
+
26
+ impl Error for E {
27
+ fn backtrace(&self) -> Option<&Backtrace> {
28
+ let backtrace = Backtrace::capture();
29
+ match backtrace.status() {
30
+ BacktraceStatus::Captured | BacktraceStatus::Disabled | _ => {}
31
+ }
32
+ unimplemented!()
33
+ }
34
+ }
35
+ "# ;
36
+
37
+ fn main ( ) {
38
+ match compile_probe ( ) {
39
+ Some ( status) if status. success ( ) => println ! ( "cargo:rustc-cfg=backtrace" ) ,
40
+ _ => { }
41
+ }
42
+ }
43
+
44
+ fn compile_probe ( ) -> Option < ExitStatus > {
45
+ let rustc = env:: var_os ( "RUSTC" ) ?;
46
+ let out_dir = env:: var_os ( "OUT_DIR" ) ?;
47
+ let probefile = Path :: new ( & out_dir) . join ( "probe.rs" ) ;
48
+ fs:: write ( & probefile, PROBE ) . ok ( ) ?;
49
+ Command :: new ( rustc)
50
+ . stderr ( Stdio :: null ( ) )
51
+ . arg ( "--edition=2018" )
52
+ . arg ( "--crate-name=http_types_build" )
53
+ . arg ( "--crate-type=lib" )
54
+ . arg ( "--emit=metadata" )
55
+ . arg ( "--out-dir" )
56
+ . arg ( out_dir)
57
+ . arg ( probefile)
58
+ . status ( )
59
+ . ok ( )
60
+ }
Original file line number Diff line number Diff line change @@ -72,7 +72,7 @@ impl Error {
72
72
/// [tracking]: https://github.com/rust-lang/rust/issues/53487
73
73
#[ cfg( backtrace) ]
74
74
pub fn backtrace ( & self ) -> & std:: backtrace:: Backtrace {
75
- self . error . downcast_ref :: < E > ( )
75
+ self . error . backtrace ( )
76
76
}
77
77
78
78
/// Attempt to downcast the error object to a concrete type.
@@ -106,13 +106,13 @@ impl Error {
106
106
107
107
impl Display for Error {
108
108
fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
109
- write ! ( formatter , "{}" , self . error)
109
+ Display :: fmt ( & self . error , formatter )
110
110
}
111
111
}
112
112
113
113
impl Debug for Error {
114
114
fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
115
- write ! ( formatter , "{}" , self . error)
115
+ Debug :: fmt ( & self . error , formatter )
116
116
}
117
117
}
118
118
Original file line number Diff line number Diff line change 96
96
#![ deny( missing_debug_implementations, nonstandard_style) ]
97
97
#![ warn( missing_docs, unreachable_pub) ]
98
98
#![ allow( clippy:: new_without_default) ]
99
+ #![ cfg_attr( backtrace, feature( backtrace) ) ]
99
100
#![ cfg_attr( test, deny( warnings) ) ]
100
101
#![ cfg_attr( feature = "docs" , feature( doc_cfg) ) ]
101
102
#![ doc( html_favicon_url = "https://yoshuawuyts.com/assets/http-rs/favicon.ico" ) ]
You can’t perform that action at this time.
0 commit comments