diff options
| author | bors <bors@rust-lang.org> | 2018-09-01 18:08:45 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-09-01 18:08:45 +0000 |
| commit | f39f218ec33d93e8a1b0ac4282f62ee35e02c18a (patch) | |
| tree | e914134013b2f01af35a165cc497337e32c658c8 /src/libstd | |
| parent | 839d99c861dbabfd4fa97b66ede9a97b8255d179 (diff) | |
| parent | e2e4f57bf84ca3cbdb91ba9d235c12b46666d090 (diff) | |
| download | rust-f39f218ec33d93e8a1b0ac4282f62ee35e02c18a.tar.gz rust-f39f218ec33d93e8a1b0ac4282f62ee35e02c18a.zip | |
Auto merge of #53533 - withoutboats:error-source, r=withoutboats
Add Error::source method per RFC 2504. This implements part of RFC 2504. * Adds `Error::source`, a replacement for `Error::cause` with the "right" signature, which will be instantly stable. * Deprecates `Error::cause` in 1.33 (this choice was based on the precedent in #52994, which we haven't finalized). * Redefines `Error::cause` to delegate to `Error::source` (the delegation can only go in this direction, not the other). @rfcbot fcp merge
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/error.rs | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/src/libstd/error.rs b/src/libstd/error.rs index 29534696abc..3de4a1bd417 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 = "error_source", since = "1.30.0")] + fn source(&self) -> Option<&(dyn Error + 'static)> { None } /// Get the `TypeId` of `self` #[doc(hidden)] |
