diff --git a/Cargo.toml b/Cargo.toml index 55feb0a..802b130 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,11 +17,15 @@ path = "./failure_derive" optional = true version = "0.3.3" +[dependencies.core-error] +version = "0.0.1-rc3" + [workspace] members = [".", "failure_derive"] [features] default = ["std", "derive"] #small-error = ["std"] -std = ["backtrace"] -derive = ["failure_derive"] +std = ["backtrace", "core-error/std"] +alloc = ["core-error/alloc"] +derive = ["failure_derive"] \ No newline at end of file diff --git a/README.md b/README.md index 56df9a2..8742f1f 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,9 @@ 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 -`Error` type) will not be available in no_std mode. +`Error` type) will not be available in no_std mode. Additionally, uses of the +`std::error::Error` trait (such as in the `Compat` struct) are replaced with +the [core_error] trait to be compatible with other error handling crates. ## License diff --git a/src/box_std.rs b/src/box_std.rs index 05891db..952dd4e 100644 --- a/src/box_std.rs +++ b/src/box_std.rs @@ -1,4 +1,4 @@ -use std::error::Error; +use core_error::Error; use std::fmt; use Fail; diff --git a/src/compat.rs b/src/compat.rs index dec5383..775cdcb 100644 --- a/src/compat.rs +++ b/src/compat.rs @@ -1,9 +1,12 @@ -use core::fmt::{self, Display}; +use core::fmt::{self, Display, Debug}; +use core_error::Error as StdError; /// A compatibility wrapper around an error type from this crate. /// /// `Compat` implements `std::error::Error`, allowing the types from this /// crate to be passed to interfaces that expect a type of that trait. +/// +/// In `no_std`, `Compat` implements `core_error::Error`. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)] pub struct Compat { pub(crate) error: E, @@ -27,18 +30,12 @@ impl Compat { } } +impl StdError for Compat {} + with_std! { - use std::fmt::Debug; - use std::error::Error as StdError; use Error; - impl StdError for Compat { - fn description(&self) -> &'static str { - "An error has occurred." - } - } - impl From for Box { fn from(error: Error) -> Box { Box::new(Compat { error }) diff --git a/src/error/mod.rs b/src/error/mod.rs index 842dbba..aec62c4 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -12,8 +12,7 @@ use box_std::BoxStd; mod error_impl; use self::error_impl::ErrorImpl; -#[cfg(feature = "std")] -use std::error::Error as StdError; +use core_error::Error as StdError; /// The `Error` type, which can contain any failure. diff --git a/src/lib.rs b/src/lib.rs index 41a45ba..e432eb0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,6 +33,8 @@ macro_rules! without_std { ($($i:item)*) => ($(#[cfg(not(feature = "std"))]$i)*) #[doc(hidden)] pub extern crate core as _core; +extern crate core_error; + mod as_fail; mod backtrace; #[cfg(feature = "std")] @@ -59,6 +61,8 @@ extern crate failure_derive; #[doc(hidden)] pub use failure_derive::*; +use core_error::Error as StdError; + with_std! { extern crate core; @@ -67,8 +71,6 @@ with_std! { mod error; - use std::error::Error as StdError; - pub use error::Error; /// A common result with an `Error`. @@ -269,7 +271,6 @@ impl dyn Fail { } } -#[cfg(feature = "std")] impl Fail for E {} #[cfg(feature = "std")] diff --git a/src/sync_failure.rs b/src/sync_failure.rs index 63e966c..37a22e9 100644 --- a/src/sync_failure.rs +++ b/src/sync_failure.rs @@ -1,5 +1,5 @@ use Fail; -use std::error::Error; +use core_error::Error; use std::fmt::{self, Debug, Display}; use std::sync::Mutex;