Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Replace std::error::Error with core_error::Error #334

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/box_std.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::error::Error;
use core_error::Error;
use std::fmt;
use Fail;

Expand Down
15 changes: 6 additions & 9 deletions src/compat.rs
Original file line number Diff line number Diff line change
@@ -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<E> {
pub(crate) error: E,
Expand All @@ -27,18 +30,12 @@ impl<E> Compat<E> {
}
}

impl<E: Display + Debug> StdError for Compat<E> {}

with_std! {
use std::fmt::Debug;
use std::error::Error as StdError;

use Error;

impl<E: Display + Debug> StdError for Compat<E> {
fn description(&self) -> &'static str {
"An error has occurred."
}
}

impl From<Error> for Box<dyn StdError> {
fn from(error: Error) -> Box<dyn StdError> {
Box::new(Compat { error })
Expand Down
3 changes: 1 addition & 2 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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;

Expand All @@ -67,8 +71,6 @@ with_std! {

mod error;

use std::error::Error as StdError;

pub use error::Error;

/// A common result with an `Error`.
Expand Down Expand Up @@ -269,7 +271,6 @@ impl dyn Fail {
}
}

#[cfg(feature = "std")]
impl<E: StdError + Send + Sync + 'static> Fail for E {}

#[cfg(feature = "std")]
Expand Down
2 changes: 1 addition & 1 deletion src/sync_failure.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use Fail;
use std::error::Error;
use core_error::Error;
use std::fmt::{self, Debug, Display};
use std::sync::Mutex;

Expand Down