From 4cf22dfa2139f072e0ee937de343a0b0b0a77a22 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Wed, 27 Feb 2019 13:18:02 -0800 Subject: [PATCH] feat(error): implement `Error::source` when available Closes #1768 --- Cargo.toml | 3 +++ build.rs | 9 +++++++++ src/error.rs | 10 ++++++++++ 3 files changed, 22 insertions(+) create mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index 7591cb53e4..71fa608ecb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,9 @@ tokio-threadpool = { version = "0.1.3", optional = true } tokio-timer = { version = "0.2", optional = true } want = "0.0.6" +[build-dependencies] +rustc_version = "0.2" + [dev-dependencies] futures-timer = "0.1" num_cpus = "1.0" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000000..c34535fe4d --- /dev/null +++ b/build.rs @@ -0,0 +1,9 @@ +extern crate rustc_version; + +use rustc_version::{version, Version}; + +fn main() { + if version().unwrap() >= Version::parse("1.30.0").unwrap() { + println!("cargo:rustc-cfg=error_source"); + } +} diff --git a/src/error.rs b/src/error.rs index 226dee4a4d..5bb75a3446 100644 --- a/src/error.rs +++ b/src/error.rs @@ -323,6 +323,7 @@ impl StdError for Error { } } + #[cfg(not(error_source))] fn cause(&self) -> Option<&StdError> { self .inner @@ -330,6 +331,15 @@ impl StdError for Error { .as_ref() .map(|cause| &**cause as &StdError) } + + #[cfg(error_source)] + fn source(&self) -> Option<&(StdError + 'static)> { + self + .inner + .cause + .as_ref() + .map(|cause| &**cause as &(StdError + 'static)) + } } #[doc(hidden)]