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 {
7272 /// [tracking]: https://github.com/rust-lang/rust/issues/53487
7373 #[ cfg( backtrace) ]
7474 pub fn backtrace ( & self ) -> & std:: backtrace:: Backtrace {
75- self . error . downcast_ref :: < E > ( )
75+ self . error . backtrace ( )
7676 }
7777
7878 /// Attempt to downcast the error object to a concrete type.
@@ -106,13 +106,13 @@ impl Error {
106106
107107impl Display for Error {
108108 fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
109- write ! ( formatter , "{}" , self . error)
109+ Display :: fmt ( & self . error , formatter )
110110 }
111111}
112112
113113impl Debug for Error {
114114 fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
115- write ! ( formatter , "{}" , self . error)
115+ Debug :: fmt ( & self . error , formatter )
116116 }
117117}
118118
Original file line number Diff line number Diff line change 9696#![ deny( missing_debug_implementations, nonstandard_style) ]
9797#![ warn( missing_docs, unreachable_pub) ]
9898#![ allow( clippy:: new_without_default) ]
99+ #![ cfg_attr( backtrace, feature( backtrace) ) ]
99100#![ cfg_attr( test, deny( warnings) ) ]
100101#![ cfg_attr( feature = "docs" , feature( doc_cfg) ) ]
101102#![ doc( html_favicon_url = "https://yoshuawuyts.com/assets/http-rs/favicon.ico" ) ]
You can’t perform that action at this time.
0 commit comments