From ea8171169aec77043d132ae5799692d0305d2d59 Mon Sep 17 00:00:00 2001 From: Andrii Radyk Date: Tue, 14 Jan 2020 02:28:35 +0100 Subject: [PATCH] allow Error::description to be used for rust below 1.42 (#285) --- build.rs | 4 ++++ src/error_chain.rs | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 008ec09ab..612f2091d 100644 --- a/build.rs +++ b/build.rs @@ -10,6 +10,10 @@ fn main() { println!("cargo:rustc-cfg=has_error_source"); } + if is_min_version("1.42").unwrap_or(false) { + println!("cargo:rustc-cfg=has_error_description_deprecated"); + } + // So we can get the build profile for has_backtrace_depending_on_env test if let Ok(profile) = env::var("PROFILE") { println!("cargo:rustc-cfg=build={:?}", profile); diff --git a/src/error_chain.rs b/src/error_chain.rs index 18a352e94..4f99243e7 100644 --- a/src/error_chain.rs +++ b/src/error_chain.rs @@ -68,6 +68,21 @@ macro_rules! impl_error_chain_cause_or_source { }; } +/// Conditional usage of deprecated Error::description +#[doc(hidden)] +#[cfg(has_error_description_deprecated)] +#[macro_export(local_inner_macros)] +macro_rules! call_to_deprecated_description { + ($e:ident) => { "" }; +} + +#[doc(hidden)] +#[cfg(not(has_error_description_deprecated))] +#[macro_export(local_inner_macros)] +macro_rules! call_to_deprecated_description { + ($e:ident) => { ::std::error::Error::description($e) }; +} + /// Prefer to use `error_chain` instead of this macro. #[doc(hidden)] #[macro_export(local_inner_macros)] @@ -301,6 +316,7 @@ macro_rules! impl_error_chain_processed { } impl ::std::error::Error for $error_name { + #[cfg(not(has_error_description_deprecated))] fn description(&self) -> &str { self.description() } @@ -369,7 +385,7 @@ macro_rules! impl_error_chain_processed { $( $(#[$meta_foreign_links])* $foreign_link_variant(err: $foreign_link_error_path) { - description(::std::error::Error::description(err)) + description(call_to_deprecated_description!(err)) display("{}", err) } ) *