Skip to content

Commit

Permalink
can't work because of rust-lang/rfcs#1053
Browse files Browse the repository at this point in the history
but would be cool
coherent backtrace implementation for Compat<failure::Error>
  • Loading branch information
Ten0 committed Dec 27, 2018
1 parent 8f8f92f commit 07bf235
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 216 deletions.
65 changes: 36 additions & 29 deletions src/compat.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,54 @@
use core::fmt::{self, Display};

use Error;
use Fail;

/// 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.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)]
pub struct Compat<E> {
pub(crate) error: E,
pub(crate) error: E,
}

impl<E: Display> Display for Compat<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.error, f)
}
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.error, f)
}
}

impl<E> Compat<E> {
/// Unwraps this into the inner error.
pub fn into_inner(self) -> E {
self.error
}

/// Gets a reference to the inner error.
pub fn get_ref(&self) -> &E {
&self.error
}
/// Unwraps this into the inner error.
pub fn into_inner(self) -> E {
self.error
}

/// Gets a reference to the inner error.
pub fn get_ref(&self) -> &E {
&self.error
}
}

impl Fail for Compat<Error> {
fn backtrace(&self) -> Option<&crate::Backtrace> {
Some(self.error.backtrace())
}
}

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<StdError> {
fn from(error: Error) -> Box<StdError> {
Box::new(Compat { error })
}
}
use std::fmt::Debug;
use std::error::Error as StdError;

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

impl From<Error> for Box<StdError> {
fn from(error: Error) -> Box<StdError> {
Box::new(Compat { error })
}
}
}
Loading

0 comments on commit 07bf235

Please sign in to comment.