diff --git a/.travis.yml b/.travis.yml index d544a67..53c70e9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,12 @@ language: rust rust: - - 1.18.0 + - 1.31.0 - stable - beta - nightly cache: cargo script: - - if [ "$TRAVIS_RUST_VERSION" == "1.18.0" ]; then cp Cargo.lock.ci Cargo.lock; fi + - if [ "$TRAVIS_RUST_VERSION" == "1.31.0" ]; then cp Cargo.lock.ci Cargo.lock; fi - cargo test - cargo test --features backtrace - cargo check --no-default-features diff --git a/Cargo.lock.ci b/Cargo.lock.ci index f995735..1508fa5 100644 --- a/Cargo.lock.ci +++ b/Cargo.lock.ci @@ -31,17 +31,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "failure" -version = "0.1.5" +version = "0.1.6" dependencies = [ "backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", - "failure_derive 0.1.5", + "failure_derive 0.1.6", ] [[package]] name = "failure_derive" -version = "0.1.5" +version = "0.1.6" dependencies = [ - "failure 0.1.5", + "failure 0.1.6", "proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", "syn 0.15.23 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 2366e55..55feb0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,11 +6,11 @@ homepage = "https://rust-lang-nursery.github.io/failure/" license = "MIT OR Apache-2.0" name = "failure" repository = "https://github.com/rust-lang-nursery/failure" -version = "0.1.5" +version = "0.1.6" [dependencies.failure_derive] optional = true -version = "0.1.5" +version = "0.1.6" path = "./failure_derive" [dependencies.backtrace] diff --git a/README.md b/README.md index 2f22832..56df9a2 100644 --- a/README.md +++ b/README.md @@ -100,8 +100,8 @@ pub fn read_toolchains(path: PathBuf) -> Result ## Requirements Both failure and failure_derive are intended to compile on all stable versions -of Rust newer than 1.18.0, as well as the latest beta and the latest nightly. -If either crate fails to compile on any version newer than 1.18.0, please open +of Rust newer than 1.31.0, as well as the latest beta and the latest nightly. +If either crate fails to compile on any version newer than 1.31.0, please open an issue. failure is **no_std** compatible, though some aspects of it (primarily the diff --git a/failure_derive/Cargo.toml b/failure_derive/Cargo.toml index 8280f66..ec45dc0 100644 --- a/failure_derive/Cargo.toml +++ b/failure_derive/Cargo.toml @@ -6,14 +6,14 @@ name = "failure_derive" repository = "https://github.com/withoutboats/failure_derive" homepage = "https://rust-lang-nursery.github.io/failure/" documentation = "https://docs.rs/failure" -version = "0.1.5" +version = "0.1.6" build = "build.rs" [dependencies] -quote = "0.6.3" -syn = "0.15.0" -synstructure = "0.10.0" -proc-macro2 = "0.4.8" +quote = "1" +syn = "1.0.3" +synstructure = "0.12.0" +proc-macro2 = "1" [dev-dependencies.failure] version = "0.1.0" diff --git a/failure_derive/src/lib.rs b/failure_derive/src/lib.rs index 7df10d1..fe9e76c 100644 --- a/failure_derive/src/lib.rs +++ b/failure_derive/src/lib.rs @@ -25,6 +25,12 @@ impl Error { } } +impl From for Error { + fn from(e: syn::Error) -> Error { + Error(e.to_compile_error()) + } +} + decl_derive!([Fail, attributes(fail, cause)] => fail_derive); fn fail_derive(s: synstructure::Structure) -> TokenStream { @@ -120,7 +126,7 @@ fn display_body(s: &synstructure::Structure) -> Result { + syn::NestedMeta::Meta(syn::Meta::NameValue(ref nv)) if nv.path.is_ident("display") => { nv.lit.clone() } _ => { @@ -131,12 +137,12 @@ fn display_body(s: &synstructure::Structure) -> Result { - let bi = &v.bindings()[i.value() as usize]; + syn::NestedMeta::Lit(syn::Lit::Int(ref i)) => { + let bi = &v.bindings()[i.base10_parse::()?]; Ok(quote!(#bi)) } - syn::NestedMeta::Meta(syn::Meta::Word(ref id)) => { - let id_s = id.to_string(); + syn::NestedMeta::Meta(syn::Meta::Path(ref path)) => { + let id_s = path.get_ident().map(syn::Ident::to_string).unwrap_or("".to_string()); if id_s.starts_with("_") { if let Ok(idx) = id_s[1..].parse::() { let bi = match v.bindings().get(idx) { @@ -160,15 +166,16 @@ fn display_body(s: &synstructure::Structure) -> Result Result Result, Error> { let mut error_msg = None; for attr in attrs { - if let Some(meta) = attr.interpret_meta() { - if meta.name() == "fail" { + if let Ok(meta) = attr.parse_meta() { + if meta.path().is_ident("fail") { if error_msg.is_some() { return Err(Error::new( meta.span(), @@ -223,7 +230,7 @@ fn is_backtrace(bi: &&synstructure::BindingInfo) -> bool { segments: ref path, .. }, }) => path.last().map_or(false, |s| { - s.value().ident == "Backtrace" && s.value().arguments.is_empty() + s.ident == "Backtrace" && s.arguments.is_empty() }), _ => false, } @@ -232,18 +239,18 @@ fn is_backtrace(bi: &&synstructure::BindingInfo) -> bool { fn is_cause(bi: &&synstructure::BindingInfo) -> bool { let mut found_cause = false; for attr in &bi.ast().attrs { - if let Some(meta) = attr.interpret_meta() { - if meta.name() == "cause" { + if let Ok(meta) = attr.parse_meta() { + if meta.path().is_ident("cause") { if found_cause { panic!("Cannot have two `cause` attributes"); } found_cause = true; } - if meta.name() == "fail" { + if meta.path().is_ident("fail") { if let syn::Meta::List(ref list) = meta { if let Some(ref pair) = list.nested.first() { - if let &&syn::NestedMeta::Meta(syn::Meta::Word(ref word)) = pair.value() { - if word == "cause" { + if let &&syn::NestedMeta::Meta(syn::Meta::Path(ref path)) = pair { + if path.is_ident("cause") { if found_cause { panic!("Cannot have two `cause` attributes"); } diff --git a/failure_derive/tests/wraps.rs b/failure_derive/tests/wraps.rs index 2049273..9144325 100644 --- a/failure_derive/tests/wraps.rs +++ b/failure_derive/tests/wraps.rs @@ -58,8 +58,8 @@ fn wrap_backtrace_error() { .and_then(|err| err.downcast_ref::()) .is_some()); assert!(err.backtrace().is_some()); - assert!(err.backtrace().is_empty()); - assert_eq!(err.backtrace().is_empty(), err.backtrace().to_string().trim().is_empty()); + assert!(err.backtrace().unwrap().is_empty()); + assert!(err.backtrace().unwrap().to_string().trim().is_empty()); } #[derive(Fail, Debug)] @@ -93,6 +93,6 @@ fn wrap_enum_error() { .and_then(|err| err.downcast_ref::()) .is_some()); assert!(err.backtrace().is_some()); - assert!(err.backtrace().is_empty()); - assert_eq!(err.backtrace().is_empty(), err.backtrace().to_string().trim().is_empty()); + assert!(err.backtrace().unwrap().is_empty()); + assert!(err.backtrace().unwrap().to_string().trim().is_empty()); } diff --git a/src/as_fail.rs b/src/as_fail.rs index dd53a46..6e4172b 100644 --- a/src/as_fail.rs +++ b/src/as_fail.rs @@ -8,20 +8,20 @@ use Fail; /// custom cause. pub trait AsFail { /// Converts a reference to `Self` into a dynamic trait object of `Fail`. - fn as_fail(&self) -> &Fail; + fn as_fail(&self) -> &dyn Fail; } impl AsFail for T where T: Fail, { - fn as_fail(&self) -> &Fail { + fn as_fail(&self) -> &dyn Fail { self } } -impl AsFail for Fail { - fn as_fail(&self) -> &Fail { +impl AsFail for dyn Fail { + fn as_fail(&self) -> &dyn Fail { self } } @@ -30,7 +30,7 @@ with_std! { use error::Error; impl AsFail for Error { - fn as_fail(&self) -> &Fail { + fn as_fail(&self) -> &dyn Fail { self.as_fail() } } diff --git a/src/box_std.rs b/src/box_std.rs index a58ae66..05891db 100644 --- a/src/box_std.rs +++ b/src/box_std.rs @@ -2,7 +2,7 @@ use std::error::Error; use std::fmt; use Fail; -pub struct BoxStd(pub Box); +pub struct BoxStd(pub Box); impl fmt::Display for BoxStd { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/src/compat.rs b/src/compat.rs index c0fc045..dec5383 100644 --- a/src/compat.rs +++ b/src/compat.rs @@ -39,14 +39,14 @@ with_std! { } } - impl From for Box { - fn from(error: Error) -> Box { + impl From for Box { + fn from(error: Error) -> Box { Box::new(Compat { error }) } } - impl From for Box { - fn from(error: Error) -> Box { + impl From for Box { + fn from(error: Error) -> Box { Box::new(Compat { error }) } } diff --git a/src/context.rs b/src/context.rs index 9ab0cc4..b5977e9 100644 --- a/src/context.rs +++ b/src/context.rs @@ -112,7 +112,7 @@ with_std! { self.failure.as_cause().and_then(|x| x.name()) } - fn cause(&self) -> Option<&Fail> { + fn cause(&self) -> Option<&dyn Fail> { self.failure.as_cause() } @@ -146,7 +146,7 @@ with_std! { } } - fn as_cause(&self) -> Option<&Fail> { + fn as_cause(&self) -> Option<&dyn Fail> { match *self { Either::This(_) => None, Either::That(ref error) => Some(error.as_fail()) diff --git a/src/error/error_impl.rs b/src/error/error_impl.rs index d1644ca..6a95d92 100644 --- a/src/error/error_impl.rs +++ b/src/error/error_impl.rs @@ -4,7 +4,7 @@ use Fail; use backtrace::Backtrace; pub(crate) struct ErrorImpl { - inner: Box>, + inner: Box>, } struct Inner { @@ -25,11 +25,11 @@ impl From for ErrorImpl { } impl ErrorImpl { - pub(crate) fn failure(&self) -> &Fail { + pub(crate) fn failure(&self) -> &dyn Fail { &self.inner.failure } - pub(crate) fn failure_mut(&mut self) -> &mut Fail { + pub(crate) fn failure_mut(&mut self) -> &mut dyn Fail { &mut self.inner.failure } diff --git a/src/error/mod.rs b/src/error/mod.rs index 152807b..842dbba 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -60,13 +60,13 @@ impl Error { /// } /// ``` #[cfg(feature = "std")] - pub fn from_boxed_compat(err: Box) -> Error { + pub fn from_boxed_compat(err: Box) -> Error { Error::from(BoxStd(err)) } /// Return a reference to the underlying failure that this `Error` /// contains. - pub fn as_fail(&self) -> &Fail { + pub fn as_fail(&self) -> &dyn Fail { self.imp.failure() } @@ -82,7 +82,7 @@ impl Error { /// This method has been deprecated in favor of the [Error::as_fail] method, /// which does the same thing. #[deprecated(since = "0.1.2", note = "please use 'as_fail()' method instead")] - pub fn cause(&self) -> &Fail { + pub fn cause(&self) -> &dyn Fail { self.as_fail() } @@ -133,7 +133,7 @@ impl Error { /// Returns the "root cause" of this error - the last value in the /// cause chain which does not return an underlying `cause`. - pub fn find_root_cause(&self) -> &Fail { + pub fn find_root_cause(&self) -> &dyn Fail { self.as_fail().find_root_cause() } @@ -173,7 +173,7 @@ impl Error { /// Deprecated alias to `find_root_cause`. #[deprecated(since = "0.1.2", note = "please use the 'find_root_cause()' method instead")] - pub fn root_cause(&self) -> &Fail { + pub fn root_cause(&self) -> &dyn Fail { ::find_root_cause(self.as_fail()) } @@ -201,8 +201,8 @@ impl Debug for Error { } } -impl AsRef for Error { - fn as_ref(&self) -> &Fail { +impl AsRef for Error { + fn as_ref(&self) -> &dyn Fail { self.as_fail() } } diff --git a/src/lib.rs b/src/lib.rs index 02c239f..41a45ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -126,7 +126,7 @@ pub trait Fail: Display + Debug + Send + Sync + 'static { /// `Some` when it can return a **different** failure. Users may loop /// over the cause chain, and returning `self` would result in an infinite /// loop. - fn cause(&self) -> Option<&Fail> { + fn cause(&self) -> Option<&dyn Fail> { None } @@ -186,7 +186,7 @@ pub trait Fail: Display + Debug + Send + Sync + 'static { since = "0.1.2", note = "please use the 'find_root_cause()' method instead" )] - fn root_cause(&self) -> &Fail + fn root_cause(&self) -> &dyn Fail where Self: Sized, { @@ -199,13 +199,13 @@ pub trait Fail: Display + Debug + Send + Sync + 'static { } } -impl Fail { +impl dyn Fail { /// Attempts to downcast this failure to a concrete type by reference. /// /// If the underlying error is not of type `T`, this will return `None`. pub fn downcast_ref(&self) -> Option<&T> { if self.__private_get_type_id__() == TypeId::of::() { - unsafe { Some(&*(self as *const Fail as *const T)) } + unsafe { Some(&*(self as *const dyn Fail as *const T)) } } else { None } @@ -217,7 +217,7 @@ impl Fail { /// If the underlying error is not of type `T`, this will return `None`. pub fn downcast_mut(&mut self) -> Option<&mut T> { if self.__private_get_type_id__() == TypeId::of::() { - unsafe { Some(&mut *(self as *mut Fail as *mut T)) } + unsafe { Some(&mut *(self as *mut dyn Fail as *mut T)) } } else { None } @@ -231,7 +231,7 @@ impl Fail { /// /// This is equivalent to iterating over `iter_causes()` and taking /// the last item. - pub fn find_root_cause(&self) -> &Fail { + pub fn find_root_cause(&self) -> &dyn Fail { find_root_cause(self) } @@ -258,7 +258,7 @@ impl Fail { since = "0.1.2", note = "please use the 'find_root_cause()' method instead" )] - pub fn root_cause(&self) -> &Fail { + pub fn root_cause(&self) -> &dyn Fail { find_root_cause(self) } @@ -273,8 +273,8 @@ impl Fail { impl Fail for E {} #[cfg(feature = "std")] -impl Fail for Box { - fn cause(&self) -> Option<&Fail> { +impl Fail for Box { + fn cause(&self) -> Option<&dyn Fail> { (**self).cause() } @@ -285,12 +285,12 @@ impl Fail for Box { /// A iterator over the causes of a `Fail` pub struct Causes<'f> { - fail: Option<&'f Fail>, + fail: Option<&'f dyn Fail>, } impl<'f> Iterator for Causes<'f> { - type Item = &'f Fail; - fn next(&mut self) -> Option<&'f Fail> { + type Item = &'f dyn Fail; + fn next(&mut self) -> Option<&'f dyn Fail> { self.fail.map(|fail| { self.fail = fail.cause(); fail @@ -298,7 +298,7 @@ impl<'f> Iterator for Causes<'f> { } } -fn find_root_cause(mut fail: &Fail) -> &Fail { +fn find_root_cause(mut fail: &dyn Fail) -> &dyn Fail { while let Some(cause) = fail.cause() { fail = cause; } diff --git a/tests/fail_compat.rs b/tests/fail_compat.rs index 1b3bc7c..81f84be 100644 --- a/tests/fail_compat.rs +++ b/tests/fail_compat.rs @@ -12,12 +12,12 @@ fn return_failure() -> Result<(), failure::Error> { Err(err.into()) } -fn return_error() -> Result<(), Box> { +fn return_error() -> Result<(), Box> { return_failure()?; Ok(()) } -fn return_error_send_sync() -> Result<(), Box> { +fn return_error_send_sync() -> Result<(), Box> { return_failure()?; Ok(()) }