]> git.proxmox.com Git - rustc.git/blob - src/vendor/failure/src/compat.rs
New upstream version 1.28.0~beta.14+dfsg1
[rustc.git] / src / vendor / failure / src / compat.rs
1 use core::fmt::{self, Display};
2
3 /// A compatibility wrapper around an error type from this crate.
4 ///
5 /// `Compat` implements `std::error::Error`, allowing the types from this
6 /// crate to be passed to interfaces that expect a type of that trait.
7 #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)]
8 pub struct Compat<E> {
9 pub(crate) error: E,
10 }
11
12 impl<E: Display> Display for Compat<E> {
13 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14 Display::fmt(&self.error, f)
15 }
16 }
17
18 impl<E> Compat<E> {
19 /// Unwraps this into the inner error.
20 pub fn into_inner(self) -> E {
21 self.error
22 }
23 }
24
25 with_std! {
26 use std::fmt::Debug;
27 use std::error::Error as StdError;
28
29 use Error;
30
31 impl<E: Display + Debug> StdError for Compat<E> {
32 fn description(&self) -> &'static str {
33 "An error has occurred."
34 }
35 }
36
37 impl From<Error> for Box<StdError> {
38 fn from(error: Error) -> Box<StdError> {
39 Box::new(Compat { error })
40 }
41 }
42 }