From 86458f3a29430b557e9fffb4492126f8cb3f5233 Mon Sep 17 00:00:00 2001 From: roblabla Date: Sun, 27 Oct 2019 17:30:02 +0000 Subject: [PATCH 1/3] Replace std::error::Error with core_error::Error To promote compatibility across error-handling crates, use core_error::Error as the StdError trait. When using the std feature, core_error simply reexports the std crate. Otherwise, it provide its own Error trait with all the std-only features removed, and implements the trait on all the error structs found in libcore. This PR brings two immediate benefits to no_std users: It makes the Compat struct into a proper compatibility layer, and it makes the core errors compatible with the Fail trait. --- Cargo.toml | 6 +++++- src/box_std.rs | 2 +- src/compat.rs | 13 ++++--------- src/error/mod.rs | 3 +-- src/lib.rs | 7 ++++--- src/sync_failure.rs | 2 +- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 55feb0a..394eb56 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-rc2" + [workspace] members = [".", "failure_derive"] [features] default = ["std", "derive"] #small-error = ["std"] -std = ["backtrace"] +std = ["backtrace", "alloc", "core-error/std"] +alloc = ["core-error/alloc"] derive = ["failure_derive"] 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..febf62c 100644 --- a/src/compat.rs +++ b/src/compat.rs @@ -1,4 +1,5 @@ -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. /// @@ -27,18 +28,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; From 9a33e56642c28e4292a2dc097ebd8e7432bd195b Mon Sep 17 00:00:00 2001 From: roblabla Date: Sat, 2 Nov 2019 13:46:51 +0000 Subject: [PATCH 2/3] Add some documentation about core-error --- README.md | 4 +++- src/compat.rs | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) 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/compat.rs b/src/compat.rs index febf62c..775cdcb 100644 --- a/src/compat.rs +++ b/src/compat.rs @@ -5,6 +5,8 @@ use core_error::Error as StdError; /// /// `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, From 17fc9100aaa69ebd42b221bc6ac5a4703037359d Mon Sep 17 00:00:00 2001 From: roblabla Date: Fri, 22 Nov 2019 16:23:57 +0000 Subject: [PATCH 3/3] Fix compile error on old rust versions --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 394eb56..802b130 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ optional = true version = "0.3.3" [dependencies.core-error] -version = "0.0.1-rc2" +version = "0.0.1-rc3" [workspace] members = [".", "failure_derive"] @@ -26,6 +26,6 @@ members = [".", "failure_derive"] [features] default = ["std", "derive"] #small-error = ["std"] -std = ["backtrace", "alloc", "core-error/std"] +std = ["backtrace", "core-error/std"] alloc = ["core-error/alloc"] -derive = ["failure_derive"] +derive = ["failure_derive"] \ No newline at end of file