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

Add Error::from_std method #159

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions failure-1.X/src/box_std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use Fail;
use std::error::Error;
use std::fmt::{self, Debug, Display};

pub(crate) struct BoxStd(pub(crate) Box<Error + Send + Sync + 'static>);

impl Display for BoxStd {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.0, f)
}
}

impl Debug for BoxStd {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Debug::fmt(&self.0, f)
}
}

impl Fail for BoxStd {}
26 changes: 26 additions & 0 deletions failure-1.X/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ use core::fmt::{self, Display, Debug};

use core::mem;
use core::ptr;
use std::error::Error as StdError;

use {Causes, Fail};
use backtrace::Backtrace;
use context::Context;
use compat::Compat;
use box_std::BoxStd;

/// The `Error` type, which can contain any failure.
///
Expand Down Expand Up @@ -40,6 +42,30 @@ impl<F: Fail> From<F> for Error {
}

impl Error {
/// Creates an `Error` from `Box<std::error::Error>`.
///
/// This method is useful for comparability with code,
/// which does not use the `Fail` trait.
///
/// # Example
///
/// ```
/// use std::error::Error as StdError;
/// use failure::Error;
///
/// fn app_fn() -> Result<i32, Error> {
/// let x = library_fn().map_err(Error::from_compat)?;
/// Ok(x * 2)
/// }
///
/// fn library_fn() -> Result<i32, Box<StdError + Sync + Send + 'static>> {
/// Ok(92)
/// }
/// ```
pub fn from_compat(err: Box<StdError + Sync + Send + 'static>) -> Error {
Error::from(BoxStd(err))
}

/// Returns a reference to the underlying cause of this `Error`. Unlike the
/// method on `Fail`, this does not return an `Option`. The `Error` type
/// always has an underlying failure.
Expand Down
2 changes: 2 additions & 0 deletions failure-1.X/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ with_std! {
mod sync_failure;
pub use sync_failure::SyncFailure;

mod box_std;

#[cfg_attr(feature = "small-error", path = "./small_error.rs")]
mod error;

Expand Down
28 changes: 27 additions & 1 deletion failure-1.X/src/small_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ use std::heap::{Heap, Alloc, Layout};

use core::mem;
use core::ptr;
use std::error::Error as StdError;

use {Causes, Fail};
use backtrace::Backtrace;
use context::Context;
use compat::Compat;
use box_std::BoxStd;

/// The `Error` type, which can contain any failure.
///
Expand Down Expand Up @@ -56,7 +58,7 @@ struct TraitObject {

impl<F: Fail> From<F> for Error {
fn from(failure: F) -> Error {
unsafe {
unsafe {
let ptr: *mut InnerRaw<F> = match Heap.alloc(Layout::new::<InnerRaw<F>>()) {
Ok(p) => p as *mut InnerRaw<F>,
Err(e) => Heap.oom(e),
Expand Down Expand Up @@ -102,6 +104,30 @@ impl Inner {
}

impl Error {
/// Creates an `Error` from `Box<std::error::Error>`.
///
/// This method is useful for comparability with code,
/// which does not use the `Fail` trait.
///
/// # Example
///
/// ```
/// use std::error::Error as StdError;
/// use failure::Error;
///
/// fn app_fn() -> Result<i32, Error> {
/// let x = library_fn().map_err(Error::from_compat)?;
/// Ok(x * 2)
/// }
///
/// fn library_fn() -> Result<i32, Box<StdError + Sync + Send + 'static>> {
/// Ok(92)
/// }
/// ```
pub fn from_compat(err: Box<StdError + Sync + Send + 'static>) -> Error {
Error::from(BoxStd(err))
}

/// Returns a reference to the underlying cause of this `Error`. Unlike the
/// method on `Fail`, this does not return an `Option`. The `Error` type
/// always has an underlying failure.
Expand Down