From ca258c5d1fa13523c40ff8b2f46fa33c79110579 Mon Sep 17 00:00:00 2001 From: Without Boats Date: Mon, 20 Aug 2018 20:18:29 +0200 Subject: [PATCH 1/3] Add Error::source method per RFC 2504. --- src/libstd/error.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/src/libstd/error.rs b/src/libstd/error.rs index 29534696abc5b..af5a0571c884e 100644 --- a/src/libstd/error.rs +++ b/src/libstd/error.rs @@ -138,7 +138,72 @@ pub trait Error: Debug + Display { /// } /// ``` #[stable(feature = "rust1", since = "1.0.0")] - fn cause(&self) -> Option<&dyn Error> { None } + #[rustc_deprecated(since = "1.33.0", reason = "replaced by Error::source, which can support \ + downcasting")] + fn cause(&self) -> Option<&dyn Error> { + self.source() + } + + /// The lower-level source of this error, if any. + /// + /// # Examples + /// + /// ``` + /// use std::error::Error; + /// use std::fmt; + /// + /// #[derive(Debug)] + /// struct SuperError { + /// side: SuperErrorSideKick, + /// } + /// + /// impl fmt::Display for SuperError { + /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + /// write!(f, "SuperError is here!") + /// } + /// } + /// + /// impl Error for SuperError { + /// fn description(&self) -> &str { + /// "I'm the superhero of errors" + /// } + /// + /// fn source(&self) -> Option<&dyn (Error + 'static)> { + /// Some(&self.side) + /// } + /// } + /// + /// #[derive(Debug)] + /// struct SuperErrorSideKick; + /// + /// impl fmt::Display for SuperErrorSideKick { + /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + /// write!(f, "SuperErrorSideKick is here!") + /// } + /// } + /// + /// impl Error for SuperErrorSideKick { + /// fn description(&self) -> &str { + /// "I'm SuperError side kick" + /// } + /// } + /// + /// fn get_super_error() -> Result<(), SuperError> { + /// Err(SuperError { side: SuperErrorSideKick }) + /// } + /// + /// fn main() { + /// match get_super_error() { + /// Err(e) => { + /// println!("Error: {}", e.description()); + /// println!("Caused by: {}", e.source().unwrap()); + /// } + /// _ => println!("No error"), + /// } + /// } + /// ``` + #[stable(feature = "rust1", since = "1.30.0")] + fn source(&self) -> Option<&(dyn Error + 'static)> { None } /// Get the `TypeId` of `self` #[doc(hidden)] From a49e1ed0cf3300a998499a125ed4b4d9464bc0cd Mon Sep 17 00:00:00 2001 From: Without Boats Date: Mon, 20 Aug 2018 23:27:25 +0200 Subject: [PATCH 2/3] Fix feature name. --- src/libstd/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/error.rs b/src/libstd/error.rs index af5a0571c884e..86d45e03f2544 100644 --- a/src/libstd/error.rs +++ b/src/libstd/error.rs @@ -202,7 +202,7 @@ pub trait Error: Debug + Display { /// } /// } /// ``` - #[stable(feature = "rust1", since = "1.30.0")] + #[stable(feature = "error_source", since = "1.30.0")] fn source(&self) -> Option<&(dyn Error + 'static)> { None } /// Get the `TypeId` of `self` From e2e4f57bf84ca3cbdb91ba9d235c12b46666d090 Mon Sep 17 00:00:00 2001 From: Without Boats Date: Tue, 21 Aug 2018 00:58:52 +0200 Subject: [PATCH 3/3] Correctly parenthesize dyn Error + 'static. --- src/libstd/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/error.rs b/src/libstd/error.rs index 86d45e03f2544..3de4a1bd4170b 100644 --- a/src/libstd/error.rs +++ b/src/libstd/error.rs @@ -168,7 +168,7 @@ pub trait Error: Debug + Display { /// "I'm the superhero of errors" /// } /// - /// fn source(&self) -> Option<&dyn (Error + 'static)> { + /// fn source(&self) -> Option<&(dyn Error + 'static)> { /// Some(&self.side) /// } /// }