forked from rust-lang-deprecated/failure
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
can't work because of rust-lang/rfcs#1053
but would be cool coherent backtrace implementation for Compat<failure::Error>
- Loading branch information
Showing
2 changed files
with
214 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} | ||
} |
Oops, something went wrong.