diff --git a/Cargo.toml b/Cargo.toml index 2366e55..14a35b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,11 +6,12 @@ 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.2.0" +edition = "2018" [dependencies.failure_derive] optional = true -version = "0.1.5" +version = "0.2.0" path = "./failure_derive" [dependencies.backtrace] diff --git a/README.md b/README.md index 2f22832..df850b1 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ use std::collections::HashMap; use std::path::PathBuf; use std::str::FromStr; -use failure::Error; +use failure::DefaultError; // This is a new error type that you've created. It represents the ways a // toolchain could be invalid. @@ -49,11 +49,11 @@ use failure::Error; // We don't do any other magic like creating new types. #[derive(Debug, Fail)] enum ToolchainError { - #[fail(display = "invalid toolchain name: {}", name)] + #[error(display = "invalid toolchain name: {}", name)] InvalidToolchainName { name: String, }, - #[fail(display = "unknown toolchain version: {}", version)] + #[error(display = "unknown toolchain version: {}", version)] UnknownToolchainVersion { version: String, } diff --git a/book/src/custom-fail.md b/book/src/custom-fail.md index 324c041..1635dc0 100644 --- a/book/src/custom-fail.md +++ b/book/src/custom-fail.md @@ -14,8 +14,8 @@ To implement this pattern, you should define your own type that implements example: ```rust -#[derive(Fail, Debug)] -#[fail(display = "Input was invalid UTF-8")] +#[derive(Error, Debug)] +#[error(display = "Input was invalid UTF-8")] pub struct Utf8Error; ``` @@ -24,8 +24,8 @@ case. It can be an enum with a different variant for each possible error, and it can carry data with more precise information about the error. For example: ```rust -#[derive(Fail, Debug)] -#[fail(display = "Input was invalid UTF-8 at index {}", index)] +#[derive(Error, Debug)] +#[error(display = "Input was invalid UTF-8 at index {}", index)] pub struct Utf8Error { index: usize, } @@ -50,11 +50,11 @@ be inclined to add it as a variant on your own error type. When you do that, you should tag the underlying error as the `#[fail(cause)]` of your error: ```rust -#[derive(Fail, Debug)] +#[derive(Error, Debug)] pub enum MyError { - #[fail(display = "Input was invalid UTF-8 at index {}", _0)] + #[error(display = "Input was invalid UTF-8 at index {}", _0)] Utf8Error(usize), - #[fail(display = "{}", _0)] + #[error(display = "{}", _0)] Io(#[fail(cause)] io::Error), } ``` diff --git a/book/src/derive-fail.md b/book/src/derive-fail.md index 6fffd99..755d7c5 100644 --- a/book/src/derive-fail.md +++ b/book/src/derive-fail.md @@ -15,7 +15,7 @@ In its smallest form, deriving Fail looks like this: use std::fmt; -#[derive(Fail, Debug)] +#[derive(Error, Debug)] struct MyError; impl fmt::Display for MyError { @@ -36,8 +36,8 @@ You can derive an implementation of `Display` with a special attribute: ```rust #[macro_use] extern crate failure; -#[derive(Fail, Debug)] -#[fail(display = "An error occurred.")] +#[derive(Error, Debug)] +#[error(display = "An error occurred.")] struct MyError; ``` @@ -54,8 +54,8 @@ formatting and printing macros: ```rust #[macro_use] extern crate failure; -#[derive(Fail, Debug)] -#[fail(display = "An error occurred with error code {}. ({})", code, message)] +#[derive(Error, Debug)] +#[error(display = "An error occurred with error code {}. ({})", code, message)] struct MyError { code: i32, message: String, @@ -81,13 +81,13 @@ prefixed with an underscore: ```rust #[macro_use] extern crate failure; -#[derive(Fail, Debug)] -#[fail(display = "An error occurred with error code {}.", _0)] +#[derive(Error, Debug)] +#[error(display = "An error occurred with error code {}.", _0)] struct MyError(i32); -#[derive(Fail, Debug)] -#[fail(display = "An error occurred with error code {} ({}).", _0, _1)] +#[derive(Error, Debug)] +#[error(display = "An error occurred with error code {} ({}).", _0, _1)] struct MyOtherError(i32, String); ``` @@ -100,13 +100,13 @@ will match over the enum to generate the correct error message. For example: ```rust #[macro_use] extern crate failure; -#[derive(Fail, Debug)] +#[derive(Error, Debug)] enum MyError { - #[fail(display = "{} is not a valid version.", _0)] + #[error(display = "{} is not a valid version.", _0)] InvalidVersion(u32), - #[fail(display = "IO error: {}", error)] + #[error(display = "IO error: {}", error)] IoError { error: io::Error }, - #[fail(display = "An unknown error has occurred.")] + #[error(display = "An unknown error has occurred.")] UnknownError, } ``` @@ -122,19 +122,19 @@ field with the type `Backtrace`. This works for both structs and enums. use failure::Backtrace; /// MyError::backtrace will return a reference to the backtrace field -#[derive(Fail, Debug)] -#[fail(display = "An error occurred.")] +#[derive(Error, Debug)] +#[error(display = "An error occurred.")] struct MyError { backtrace: Backtrace, } /// MyEnumError::backtrace will return a reference to the backtrace only if it /// is Variant2, otherwise it will return None. -#[derive(Fail, Debug)] +#[derive(Error, Debug)] enum MyEnumError { - #[fail(display = "An error occurred.")] + #[error(display = "An error occurred.")] Variant1, - #[fail(display = "A different error occurred.")] + #[error(display = "A different error occurred.")] Variant2(Backtrace), } ``` @@ -159,19 +159,19 @@ This can be used in fields of enums as well as structs. use std::io; /// MyError::cause will return a reference to the io_error field -#[derive(Fail, Debug)] -#[fail(display = "An error occurred.")] +#[derive(Error, Debug)] +#[error(display = "An error occurred.")] struct MyError { #[fail(cause)] io_error: io::Error, } /// MyEnumError::cause will return a reference only if it is Variant2, /// otherwise it will return None. -#[derive(Fail, Debug)] +#[derive(Error, Debug)] enum MyEnumError { - #[fail(display = "An error occurred.")] + #[error(display = "An error occurred.")] Variant1, - #[fail(display = "A different error occurred.")] + #[error(display = "A different error occurred.")] Variant2(#[fail(cause)] io::Error), } ``` diff --git a/book/src/error-errorkind.md b/book/src/error-errorkind.md index 4f1cc4c..1c3d820 100644 --- a/book/src/error-errorkind.md +++ b/book/src/error-errorkind.md @@ -27,7 +27,7 @@ enum MyErrorKind { // A plain enum with no data in any of its variants // // For example: - #[fail(display = "A contextual error message.")] + #[error(display = "A contextual error message.")] OneVariant, // ... } diff --git a/book/src/intro.md b/book/src/intro.md index 318477a..9a8ab10 100644 --- a/book/src/intro.md +++ b/book/src/intro.md @@ -20,7 +20,7 @@ use std::collections::HashMap; use std::path::PathBuf; use std::str::FromStr; -use failure::Error; +use failure::DefaultError; // This is a new error type that you've created. It represents the ways a // toolchain could be invalid. @@ -29,11 +29,11 @@ use failure::Error; // We don't do any other magic like creating new types. #[derive(Debug, Fail)] enum ToolchainError { - #[fail(display = "invalid toolchain name: {}", name)] + #[error(display = "invalid toolchain name: {}", name)] InvalidToolchainName { name: String, }, - #[fail(display = "unknown toolchain version: {}", version)] + #[error(display = "unknown toolchain version: {}", version)] UnknownToolchainVersion { version: String, } diff --git a/book/src/use-error.md b/book/src/use-error.md index a0a2944..7e721c4 100644 --- a/book/src/use-error.md +++ b/book/src/use-error.md @@ -16,7 +16,7 @@ functions: use std::io; use std::io::BufRead; -use failure::Error; +use failure::DefaultError; use failure::err_msg; fn my_function() -> Result<(), Error> { diff --git a/examples/bail_ensure.rs b/examples/bail_ensure.rs index 05c399b..0135d12 100644 --- a/examples/bail_ensure.rs +++ b/examples/bail_ensure.rs @@ -1,14 +1,14 @@ #[macro_use] extern crate failure; -use failure::Error; +use failure::DefaultError; -fn bailer() -> Result<(), Error> { +fn bailer() -> Result<(), DefaultError> { // bail!("ruh roh"); bail!("ruh {}", "roh"); } -fn ensures() -> Result<(), Error> { +fn ensures() -> Result<(), DefaultError> { ensure!(true, "true is false"); ensure!(false, "false is false"); Ok(()) diff --git a/examples/error_as_cause.rs b/examples/error_as_cause.rs index 24e5b06..98cf23a 100644 --- a/examples/error_as_cause.rs +++ b/examples/error_as_cause.rs @@ -1,18 +1,19 @@ -#[macro_use] -extern crate failure; +// #[macro_use] +// extern crate failure; -use failure::{err_msg, Error, Fail}; +// use failure::{err_msg, Error, Fail}; -#[derive(Debug, Fail)] -#[fail(display = "my wrapping error")] -struct WrappingError(#[fail(cause)] Error); +// #[derive(Debug, Fail)] +// #[error(display = "my wrapping error")] +// struct WrappingError(#[fail(cause)] Error); -fn bad_function() -> Result<(), WrappingError> { - Err(WrappingError(err_msg("this went bad"))) -} +// fn bad_function() -> Result<(), WrappingError> { +// Err(WrappingError(err_msg("this went bad"))) +// } fn main() { - for cause in Fail::iter_causes(&bad_function().unwrap_err()) { - println!("{}", cause); - } + // for cause in Fail::iter_causes(&bad_function().unwrap_err()) { + // println!("{}", cause); + // } + println!("Hello!"); } diff --git a/examples/simple.rs b/examples/simple.rs index fc39601..b7c55cb 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -3,13 +3,13 @@ extern crate failure; use failure::Fail; -#[derive(Debug, Fail)] -#[fail(display = "my error")] +#[derive(Error, Debug)] +#[error(display = "my error")] struct MyError; -#[derive(Debug, Fail)] -#[fail(display = "my wrapping error")] -struct WrappingError(#[fail(cause)] MyError); +#[derive(Error, Debug)] +#[error(display = "my wrapping error")] +struct WrappingError(#[error(cause)] MyError); fn bad_function() -> Result<(), WrappingError> { Err(WrappingError(MyError)) diff --git a/failure_derive/Cargo.toml b/failure_derive/Cargo.toml index 8280f66..3f4aa1c 100644 --- a/failure_derive/Cargo.toml +++ b/failure_derive/Cargo.toml @@ -6,8 +6,9 @@ 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.2.0" build = "build.rs" +edition = "2018" [dependencies] quote = "0.6.3" @@ -16,7 +17,7 @@ synstructure = "0.10.0" proc-macro2 = "0.4.8" [dev-dependencies.failure] -version = "0.1.0" +version = "0.2.0" path = ".." [lib] diff --git a/failure_derive/src/lib.rs b/failure_derive/src/lib.rs index 7df10d1..51d2bfa 100644 --- a/failure_derive/src/lib.rs +++ b/failure_derive/src/lib.rs @@ -11,11 +11,11 @@ use syn::LitStr; use syn::spanned::Spanned; #[derive(Debug)] -struct Error(TokenStream); +struct DeriveError(TokenStream); -impl Error { - fn new(span: Span, message: &str) -> Error { - Error(quote_spanned! { span => +impl DeriveError { + fn new(span: Span, message: &str) -> DeriveError { + DeriveError(quote_spanned! { span => compile_error!(#message); }) } @@ -25,7 +25,7 @@ impl Error { } } -decl_derive!([Fail, attributes(fail, cause)] => fail_derive); +decl_derive!([Error, attributes(error, cause)] => fail_derive); fn fail_derive(s: synstructure::Structure) -> TokenStream { match fail_derive_impl(s) { @@ -34,7 +34,7 @@ fn fail_derive(s: synstructure::Structure) -> TokenStream { } } -fn fail_derive_impl(s: synstructure::Structure) -> Result { +fn fail_derive_impl(s: synstructure::Structure) -> Result { let make_dyn = if cfg!(has_dyn_trait) { quote! { &dyn } } else { @@ -98,7 +98,7 @@ fn fail_derive_impl(s: synstructure::Structure) -> Result { }) } -fn display_body(s: &synstructure::Structure) -> Result, Error> { +fn display_body(s: &synstructure::Structure) -> Result, DeriveError> { let mut msgs = s.variants().iter().map(|v| find_error_msg(&v.ast().attrs)); if msgs.all(|msg| msg.map(|m| m.is_none()).unwrap_or(true)) { return Ok(None); @@ -108,12 +108,12 @@ fn display_body(s: &synstructure::Structure) -> Result Result { - return Err(Error::new( + return Err(DeriveError::new( msg.span(), "Fail attribute must begin `display = \"\"` to control the Display message." )); @@ -142,7 +142,7 @@ fn display_body(s: &synstructure::Structure) -> Result bi, None => { - return Err(Error::new( + return Err(DeriveError::new( arg.span(), &format!( "display attempted to access field `{}` in `{}::{}` which \ @@ -164,7 +164,7 @@ fn display_body(s: &synstructure::Structure) -> Result Result { - return Err(Error::new( + return Err(DeriveError::new( arg.span(), "Invalid argument to fail attribute!" )); @@ -189,13 +189,13 @@ fn display_body(s: &synstructure::Structure) -> Result Result, Error> { +fn find_error_msg(attrs: &[syn::Attribute]) -> Result, DeriveError> { let mut error_msg = None; for attr in attrs { if let Some(meta) = attr.interpret_meta() { - if meta.name() == "fail" { + if meta.name() == "error" { if error_msg.is_some() { - return Err(Error::new( + return Err(DeriveError::new( meta.span(), "Cannot have two display attributes" )); @@ -203,7 +203,7 @@ fn find_error_msg(attrs: &[syn::Attribute]) -> Result, Err if let syn::Meta::List(list) = meta { error_msg = Some(list); } else { - return Err(Error::new( + return Err(DeriveError::new( meta.span(), "fail attribute must take a list in parentheses" )); @@ -239,7 +239,7 @@ fn is_cause(bi: &&synstructure::BindingInfo) -> bool { } found_cause = true; } - if meta.name() == "fail" { + if meta.name() == "error" { 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() { diff --git a/failure_derive/tests/backtrace.rs b/failure_derive/tests/backtrace.rs index a307184..6d9a7e8 100644 --- a/failure_derive/tests/backtrace.rs +++ b/failure_derive/tests/backtrace.rs @@ -4,8 +4,8 @@ extern crate failure_derive; use failure::{Backtrace, Fail}; -#[derive(Fail, Debug)] -#[fail(display = "Error code: {}", code)] +#[derive(Error, Debug)] +#[error(display = "Error code: {}", code)] struct BacktraceError { backtrace: Backtrace, code: u32, @@ -22,8 +22,8 @@ fn backtrace_error() { assert!(err.backtrace().is_some()); } -#[derive(Fail, Debug)] -#[fail(display = "An error has occurred.")] +#[derive(Error, Debug)] +#[error(display = "An error has occurred.")] struct BacktraceTupleError(Backtrace); #[test] @@ -34,13 +34,13 @@ fn backtrace_tuple_error() { assert!(err.backtrace().is_some()); } -#[derive(Fail, Debug)] +#[derive(Error, Debug)] enum BacktraceEnumError { - #[fail(display = "Error code: {}", code)] + #[error(display = "Error code: {}", code)] StructVariant { code: i32, backtrace: Backtrace }, - #[fail(display = "Error: {}", _0)] + #[error(display = "Error: {}", _0)] TupleVariant(&'static str, Backtrace), - #[fail(display = "An error has occurred.")] + #[error(display = "An error has occurred.")] UnitVariant, } diff --git a/failure_derive/tests/custom_type_bounds.rs b/failure_derive/tests/custom_type_bounds.rs index fd1c8b9..412e673 100644 --- a/failure_derive/tests/custom_type_bounds.rs +++ b/failure_derive/tests/custom_type_bounds.rs @@ -6,7 +6,7 @@ use std::fmt::Debug; use failure::Fail; #[derive(Debug, Fail)] -#[fail(display = "An error has occurred.")] +#[error(display = "An error has occurred.")] pub struct UnboundedGenericTupleError(T); #[test] @@ -16,7 +16,7 @@ fn unbounded_generic_tuple_error() { } #[derive(Debug, Fail)] -#[fail(display = "An error has occurred: {}", _0)] +#[error(display = "An error has occurred: {}", _0)] pub struct FailBoundsGenericTupleError(T); #[test] @@ -31,7 +31,7 @@ pub trait NoDisplay: 'static + Debug + Send + Sync {} impl NoDisplay for &'static str {} #[derive(Debug, Fail)] -#[fail(display = "An error has occurred: {:?}", _0)] +#[error(display = "An error has occurred: {:?}", _0)] pub struct CustomBoundsGenericTupleError(T); #[test] diff --git a/failure_derive/tests/tests.rs b/failure_derive/tests/tests.rs index 4e73255..ad0fc97 100644 --- a/failure_derive/tests/tests.rs +++ b/failure_derive/tests/tests.rs @@ -2,8 +2,8 @@ extern crate failure; #[macro_use] extern crate failure_derive; -#[derive(Fail, Debug)] -#[fail(display = "An error has occurred.")] +#[derive(Error, Debug)] +#[error(display = "An error has occurred.")] struct UnitError; #[test] @@ -12,8 +12,8 @@ fn unit_struct() { assert_eq!(&s[..], "An error has occurred."); } -#[derive(Fail, Debug)] -#[fail(display = "Error code: {}", code)] +#[derive(Error, Debug)] +#[error(display = "Error code: {}", code)] struct RecordError { code: u32, } @@ -24,8 +24,8 @@ fn record_struct() { assert_eq!(&s[..], "Error code: 0"); } -#[derive(Fail, Debug)] -#[fail(display = "Error code: {}", _0)] +#[derive(Error, Debug)] +#[error(display = "Error code: {}", _0)] struct TupleError(i32); #[test] @@ -34,13 +34,13 @@ fn tuple_struct() { assert_eq!(&s[..], "Error code: 2"); } -#[derive(Fail, Debug)] +#[derive(Error, Debug)] enum EnumError { - #[fail(display = "Error code: {}", code)] + #[error(display = "Error code: {}", code)] StructVariant { code: i32 }, - #[fail(display = "Error: {}", _0)] + #[error(display = "Error: {}", _0)] TupleVariant(&'static str), - #[fail(display = "An error has occurred.")] + #[error(display = "An error has occurred.")] UnitVariant, } diff --git a/failure_derive/tests/wraps.rs b/failure_derive/tests/wraps.rs index 38218a2..9515706 100644 --- a/failure_derive/tests/wraps.rs +++ b/failure_derive/tests/wraps.rs @@ -7,8 +7,8 @@ use std::io; use failure::{Backtrace, Fail}; -#[derive(Fail, Debug)] -#[fail(display = "An error has occurred: {}", inner)] +#[derive(Error, Debug)] +#[error(display = "An error has occurred: {}", inner)] struct WrapError { #[fail(cause)] inner: io::Error, @@ -24,8 +24,8 @@ fn wrap_error() { .is_some()); } -#[derive(Fail, Debug)] -#[fail(display = "An error has occurred: {}", _0)] +#[derive(Error, Debug)] +#[error(display = "An error has occurred: {}", _0)] struct WrapTupleError(#[fail(cause)] io::Error); #[test] @@ -38,8 +38,8 @@ fn wrap_tuple_error() { .is_some()); } -#[derive(Fail, Debug)] -#[fail(display = "An error has occurred: {}", inner)] +#[derive(Error, Debug)] +#[error(display = "An error has occurred: {}", inner)] struct WrapBacktraceError { #[fail(cause)] inner: io::Error, @@ -60,11 +60,11 @@ fn wrap_backtrace_error() { assert!(err.backtrace().is_some()); } -#[derive(Fail, Debug)] +#[derive(Error, Debug)] enum WrapEnumError { - #[fail(display = "An error has occurred: {}", _0)] + #[error(display = "An error has occurred: {}", _0)] Io(#[fail(cause)] io::Error), - #[fail(display = "An error has occurred: {}", inner)] + #[error(display = "An error has occurred: {}", inner)] Fmt { #[fail(cause)] inner: fmt::Error, diff --git a/src/as_fail.rs b/src/as_fail.rs index dd53a46..0763f73 100644 --- a/src/as_fail.rs +++ b/src/as_fail.rs @@ -1,4 +1,4 @@ -use Fail; +use crate::Fail; /// The `AsFail` trait /// @@ -27,7 +27,7 @@ impl AsFail for Fail { } with_std! { - use error::Error; + use std::error::Error; impl AsFail for Error { fn as_fail(&self) -> &Fail { diff --git a/src/box_std.rs b/src/box_std.rs index a58ae66..c21288a 100644 --- a/src/box_std.rs +++ b/src/box_std.rs @@ -1,6 +1,6 @@ use std::error::Error; use std::fmt; -use Fail; +use crate::Fail; pub struct BoxStd(pub Box); diff --git a/src/compat.rs b/src/compat.rs index c0fc045..ce25696 100644 --- a/src/compat.rs +++ b/src/compat.rs @@ -31,7 +31,7 @@ with_std! { use std::fmt::Debug; use std::error::Error as StdError; - use Error; + use crate::DefaultError; impl StdError for Compat { fn description(&self) -> &'static str { @@ -39,14 +39,14 @@ with_std! { } } - impl From for Box { - fn from(error: Error) -> Box { + impl From for Box { + fn from(error: DefaultError) -> Box { Box::new(Compat { error }) } } - impl From for Box { - fn from(error: Error) -> Box { + impl From for Box { + fn from(error: DefaultError) -> Box { Box::new(Compat { error }) } } diff --git a/src/context.rs b/src/context.rs index 9ab0cc4..243e569 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,6 +1,6 @@ use core::fmt::{self, Debug, Display}; -use Fail; +use crate::Fail; without_std! { /// An error with context around it. @@ -63,7 +63,7 @@ without_std! { } with_std! { - use {Error, Backtrace}; + use crate::{DefaultError, backtrace::Backtrace}; /// An error with context around it. /// @@ -75,7 +75,7 @@ with_std! { /// `Debug` impl also prints the underlying error. pub struct Context { context: D, - failure: Either, + failure: Either, } impl Context { @@ -101,7 +101,7 @@ with_std! { } } - pub(crate) fn with_err>(context: D, error: E) -> Context { + pub(crate) fn with_err>(context: D, error: E) -> Context { let failure = Either::That(error.into()); Context { context, failure } } @@ -138,7 +138,7 @@ with_std! { That(B), } - impl Either { + impl Either { fn backtrace(&self) -> &Backtrace { match *self { Either::This(ref backtrace) => backtrace, @@ -154,7 +154,7 @@ with_std! { } } - impl Debug for Either { + impl Debug for Either { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { Either::This(ref backtrace) => write!(f, "{:?}", backtrace), diff --git a/src/error/error_impl.rs b/src/error/error_impl.rs index d1644ca..845c3f7 100644 --- a/src/error/error_impl.rs +++ b/src/error/error_impl.rs @@ -1,7 +1,7 @@ use core::any::TypeId; -use Fail; -use backtrace::Backtrace; +use crate::Fail; +use crate::backtrace::Backtrace; pub(crate) struct ErrorImpl { inner: Box>, diff --git a/src/error/error_impl_small.rs b/src/error/error_impl_small.rs index 6ff7c78..2c57438 100644 --- a/src/error/error_impl_small.rs +++ b/src/error/error_impl_small.rs @@ -3,7 +3,7 @@ use std::heap::{Heap, Alloc, Layout}; use core::mem; use core::ptr; -use Fail; +use crate::Fail; use backtrace::Backtrace; pub(crate) struct ErrorImpl { diff --git a/src/error/mod.rs b/src/error/mod.rs index 152807b..49619e5 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -1,12 +1,12 @@ use core::fmt::{self, Display, Debug}; -use {Causes, Fail}; -use backtrace::Backtrace; -use context::Context; -use compat::Compat; +use crate::{Causes, Fail}; +use crate::Backtrace; +use crate::context::Context; +use crate::compat::Compat; #[cfg(feature = "std")] -use box_std::BoxStd; +use crate::box_std::BoxStd; #[cfg_attr(feature = "small-error", path = "./error_impl_small.rs")] mod error_impl; @@ -16,7 +16,7 @@ use self::error_impl::ErrorImpl; use std::error::Error as StdError; -/// The `Error` type, which can contain any failure. +/// The `DefaultError` type, which can contain any failure. /// /// Functions which accumulate many kinds of errors should return this type. /// All failures can be converted into it, so functions which catch those @@ -26,19 +26,19 @@ use std::error::Error as StdError; /// In addition to implementing `Debug` and `Display`, this type carries `Backtrace` /// information, and can be downcast into the failure that underlies it for /// more detailed inspection. -pub struct Error { +pub struct DefaultError { imp: ErrorImpl, } -impl From for Error { - fn from(failure: F) -> Error { - Error { +impl From for DefaultError { + fn from(failure: F) -> DefaultError { + DefaultError { imp: ErrorImpl::from(failure) } } } -impl Error { +impl DefaultError { /// Creates an `Error` from `Box`. /// /// This method is useful for comparability with code, @@ -48,7 +48,7 @@ impl Error { /// /// ``` /// use std::error::Error as StdError; - /// use failure::Error; + /// use failure::DefaultError as Error; /// /// fn app_fn() -> Result { /// let x = library_fn().map_err(Error::from_boxed_compat)?; @@ -60,8 +60,8 @@ impl Error { /// } /// ``` #[cfg(feature = "std")] - pub fn from_boxed_compat(err: Box) -> Error { - Error::from(BoxStd(err)) + pub fn from_boxed_compat(err: Box) -> DefaultError { + DefaultError::from(BoxStd(err)) } /// Return a reference to the underlying failure that this `Error` @@ -75,11 +75,11 @@ impl Error { self.as_fail().name() } - /// Returns a reference to the underlying cause of this `Error`. Unlike the + /// Returns a reference to the underlying cause of this `DefaultError`. Unlike the /// method on `Fail`, this does not return an `Option`. The `Error` type /// always has an underlying failure. /// - /// This method has been deprecated in favor of the [Error::as_fail] method, + /// This method has been deprecated in favor of the [DefaultError::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 { @@ -95,7 +95,7 @@ impl Error { self.imp.failure().backtrace().unwrap_or(&self.imp.backtrace()) } - /// Provides context for this `Error`. + /// Provides context for this `DefaultError`. /// /// This can provide additional information about this error, appropriate /// to the semantics of the current layer. That is, if you have a @@ -112,12 +112,12 @@ impl Error { Context::with_err(context, self) } - /// Wraps `Error` in a compatibility type. + /// Wraps `DefaultError` in a compatibility type. /// /// This type implements the `Error` trait from `std::error`. If you need /// to pass failure's `Error` to an interface that takes any `Error`, you /// can use this method to get a compatible type. - pub fn compat(self) -> Compat { + pub fn compat(self) -> Compat { Compat { error: self } } @@ -127,8 +127,8 @@ impl Error { /// failure is of the type `T`. For this reason it returns a `Result` - in /// the case that the underlying error is of a different type, the /// original `Error` is returned. - pub fn downcast(self) -> Result { - self.imp.downcast().map_err(|imp| Error { imp }) + pub fn downcast(self) -> Result { + self.imp.downcast().map_err(|imp| DefaultError { imp }) } /// Returns the "root cause" of this error - the last value in the @@ -174,7 +174,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 { - ::find_root_cause(self.as_fail()) + crate::find_root_cause(self.as_fail()) } /// Deprecated alias to `iter_causes`. @@ -184,13 +184,13 @@ impl Error { } } -impl Display for Error { +impl Display for DefaultError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { Display::fmt(&self.imp.failure(), f) } } -impl Debug for Error { +impl Debug for DefaultError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let backtrace = self.imp.backtrace(); if backtrace.is_none() { @@ -201,7 +201,7 @@ impl Debug for Error { } } -impl AsRef for Error { +impl AsRef for DefaultError { fn as_ref(&self) -> &Fail { self.as_fail() } @@ -210,7 +210,7 @@ impl AsRef for Error { #[cfg(test)] mod test { use std::io; - use super::Error; + use super::DefaultError as Error; fn assert_just_data() { } @@ -221,10 +221,12 @@ mod test { #[test] fn methods_seem_to_work() { + use crate::backtrace::Backtrace; + let io_error: io::Error = io::Error::new(io::ErrorKind::NotFound, "test"); let error: Error = io::Error::new(io::ErrorKind::NotFound, "test").into(); assert!(error.downcast_ref::().is_some()); - let _: ::Backtrace = *error.backtrace(); + let _: Backtrace = *error.backtrace(); assert_eq!(format!("{:?}", io_error), format!("{:?}", error)); assert_eq!(format!("{}", io_error), format!("{}", error)); drop(error); diff --git a/src/error_message.rs b/src/error_message.rs index 560d317..45ac3d2 100644 --- a/src/error_message.rs +++ b/src/error_message.rs @@ -1,15 +1,15 @@ use core::fmt::{self, Display, Debug}; -use Fail; -use Error; +use crate::Fail; +use crate::DefaultError; /// Constructs a `Fail` type from a string. /// /// This is a convenient way to turn a string into an error value that /// can be passed around, if you do not want to create a new `Fail` type for /// this use case. -pub fn err_msg(msg: D) -> Error { - Error::from(ErrorMessage { msg }) +pub fn err_msg(msg: D) -> DefaultError { + DefaultError::from(ErrorMessage { msg }) } /// A `Fail` type that just contains an error message. You can construct @@ -21,7 +21,7 @@ struct ErrorMessage { impl Fail for ErrorMessage { fn name(&self) -> Option<&str> { - Some("failure::ErrorMessage") + Some("failure::DefaultErrorMessage") } } diff --git a/src/lib.rs b/src/lib.rs index 02c239f..5110899 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,11 +44,11 @@ mod result_ext; use core::any::TypeId; use core::fmt::{Debug, Display}; -pub use as_fail::AsFail; -pub use backtrace::Backtrace; -pub use compat::Compat; -pub use context::Context; -pub use result_ext::ResultExt; +pub use crate::as_fail::AsFail; +pub use crate::backtrace::Backtrace; +pub use crate::compat::Compat; +pub use crate::context::Context; +pub use crate::result_ext::ResultExt; #[cfg(feature = "failure_derive")] #[allow(unused_imports)] @@ -63,20 +63,20 @@ with_std! { extern crate core; mod sync_failure; - pub use sync_failure::SyncFailure; + pub use crate::sync_failure::SyncFailure; mod error; use std::error::Error as StdError; - pub use error::Error; + pub use crate::error::DefaultError; /// A common result with an `Error`. - pub type Fallible = Result; + pub type Fallible = Result; mod macros; mod error_message; - pub use error_message::err_msg; + pub use crate::error_message::err_msg; } /// The `Fail` trait. diff --git a/src/result_ext.rs b/src/result_ext.rs index f4125cd..e0e2981 100644 --- a/src/result_ext.rs +++ b/src/result_ext.rs @@ -1,6 +1,6 @@ use core::fmt::Display; -use {Compat, Context, Fail}; +use crate::{Compat, Context, Fail}; /// Extension methods for `Result`. pub trait ResultExt { @@ -22,7 +22,7 @@ pub trait ResultExt { /// # /// # extern crate failure; /// # - /// # use tests::failure::ResultExt; + /// # use crate::tests::failure::ResultExt; /// # /// # #[derive(Debug)] /// struct CustomError; @@ -45,7 +45,7 @@ pub trait ResultExt { /// # /// # pub fn run_test() { /// - /// let x = (|| -> Result<(), failure::Error> { + /// let x = (|| -> Result<(), failure::DefaultError> { /// Err(CustomError).compat()? /// })().with_context(|e| { /// format!("An error occured: {}", e) @@ -81,13 +81,13 @@ pub trait ResultExt { /// # /// # use failure::{self, ResultExt}; /// # - /// #[derive(Fail, Debug)] - /// #[fail(display = "")] + /// #[derive(Error, Debug)] + /// #[error(display = "")] /// struct CustomError; /// # /// # pub fn run_test() { /// - /// let x = (|| -> Result<(), failure::Error> { + /// let x = (|| -> Result<(), failure::DefaultError> { /// Err(CustomError)? /// })().context(format!("An error occured")).unwrap_err(); /// @@ -124,13 +124,13 @@ pub trait ResultExt { /// # /// # use failure::{self, ResultExt}; /// # - /// #[derive(Fail, Debug)] - /// #[fail(display = "My custom error message")] + /// #[derive(Error, Debug)] + /// #[error(display = "My custom error message")] /// struct CustomError; /// # /// # pub fn run_test() { /// - /// let x = (|| -> Result<(), failure::Error> { + /// let x = (|| -> Result<(), failure::DefaultError> { /// Err(CustomError)? /// })().with_context(|e| { /// format!("An error occured: {}", e) @@ -177,10 +177,10 @@ where } with_std! { - use Error; + use crate::DefaultError; - impl ResultExt for Result { - fn compat(self) -> Result> { + impl ResultExt for Result { + fn compat(self) -> Result> { self.map_err(|err| err.compat()) } @@ -191,7 +191,7 @@ with_std! { } fn with_context(self, f: F) -> Result> where - F: FnOnce(&Error) -> D, + F: FnOnce(&DefaultError) -> D, D: Display + Send + Sync + 'static { self.map_err(|failure| { diff --git a/src/sync_failure.rs b/src/sync_failure.rs index 63e966c..39b154a 100644 --- a/src/sync_failure.rs +++ b/src/sync_failure.rs @@ -1,4 +1,4 @@ -use Fail; +use crate::Fail; use std::error::Error; use std::fmt::{self, Debug, Display}; use std::sync::Mutex; @@ -27,7 +27,7 @@ impl SyncFailure { /// /// # use std::error::Error as StdError; /// # use std::fmt::{self, Display}; - /// use failure::{Error, SyncFailure}; + /// use failure::{DefaultError, SyncFailure}; /// use std::cell::RefCell; /// /// #[derive(Debug)] @@ -56,7 +56,7 @@ impl SyncFailure { /// # Ok(()) /// } /// - /// fn my_function() -> Result<(), Error> { + /// fn my_function() -> Result<(), DefaultError> { /// // without the map_err here, we end up with a compile error /// // complaining that NonSyncError doesn't implement Sync. /// returns_error().map_err(SyncFailure::new)?; diff --git a/tests/basic_fail.rs b/tests/basic_fail.rs index 574886d..ee5fc08 100644 --- a/tests/basic_fail.rs +++ b/tests/basic_fail.rs @@ -5,8 +5,8 @@ use failure::Fail; #[test] fn test_name() { - #[derive(Fail, Debug)] - #[fail(display = "my error")] + #[derive(Error, Debug)] + #[error(display = "my error")] struct MyError; let err = MyError; diff --git a/tests/fail_compat.rs b/tests/fail_compat.rs index 1b3bc7c..d35db82 100644 --- a/tests/fail_compat.rs +++ b/tests/fail_compat.rs @@ -3,9 +3,9 @@ extern crate failure; use failure::Fail; -fn return_failure() -> Result<(), failure::Error> { - #[derive(Fail, Debug)] - #[fail(display = "my error")] +fn return_failure() -> Result<(), failure::DefaultError> { + #[derive(Error, Debug)] + #[error(display = "my error")] struct MyError; let err = MyError; diff --git a/tests/macro_trailing_comma.rs b/tests/macro_trailing_comma.rs index 6d8b80e..35cb8ce 100644 --- a/tests/macro_trailing_comma.rs +++ b/tests/macro_trailing_comma.rs @@ -13,7 +13,7 @@ extern crate failure; // can treat it as a Result-returning function. macro_rules! wrap_early_return { ($expr:expr) => {{ - fn func() -> Result<(), failure::Error> { + fn func() -> Result<(), failure::DefaultError> { let _ = $expr; #[allow(unreachable_code)]