anyhow/
nightly.rs

1// This code exercises the surface area that we expect of the Error generic
2// member access API. If the current toolchain is able to compile it, then
3// anyhow is able to provide backtrace support.
4
5#![cfg_attr(anyhow_build_probe, feature(error_generic_member_access))]
6
7use core::error::{self, Error};
8use std::backtrace::Backtrace;
9
10pub use core::error::Request;
11
12#[cfg(anyhow_build_probe)]
13const _: () = {
14    use core::fmt::{self, Debug, Display};
15
16    struct MyError(Backtrace);
17
18    impl Debug for MyError {
19        fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
20            unimplemented!()
21        }
22    }
23
24    impl Display for MyError {
25        fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
26            unimplemented!()
27        }
28    }
29
30    impl Error for MyError {
31        fn provide<'a>(&'a self, request: &mut Request<'a>) {
32            provide_ref_backtrace(request, &self.0);
33        }
34    }
35};
36
37// Include in sccache cache key.
38#[cfg(anyhow_build_probe)]
39const _: Option<&str> = option_env!("RUSTC_BOOTSTRAP");
40
41pub fn request_ref_backtrace(err: &dyn Error) -> Option<&Backtrace> {
42    request_ref::<Backtrace>(err)
43}
44
45fn request_ref<'a, T>(err: &'a (impl Error + ?Sized)) -> Option<&'a T>
46where
47    T: 'static + ?Sized,
48{
49    error::request_ref::<T>(err)
50}
51
52pub fn provide_ref_backtrace<'a>(request: &mut Request<'a>, backtrace: &'a Backtrace) {
53    Request::provide_ref(request, backtrace);
54}
55
56pub fn provide<'a>(err: &'a (impl Error + ?Sized), request: &mut Request<'a>) {
57    Error::provide(err, request);
58}