Skip to content

Commit

Permalink
feat(error): implement Error::source when available
Browse files Browse the repository at this point in the history
Closes #1768
  • Loading branch information
sfackler authored and seanmonstar committed Feb 27, 2019
1 parent 0bf30cc commit 4cf22df
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}
10 changes: 10 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,23 @@ impl StdError for Error {
}
}

#[cfg(not(error_source))]
fn cause(&self) -> Option<&StdError> {
self
.inner
.cause
.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)]
Expand Down

0 comments on commit 4cf22df

Please sign in to comment.