diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-05-23 18:25:01 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-05-28 16:37:59 +0000 |
| commit | b5dffdf062a38f280c8d13a19e2574330e5190fc (patch) | |
| tree | c307c04f692574da74eea781c065d859d0deb29c | |
| parent | ec6674bd044b866f388d6e68f8380e669c067193 (diff) | |
| download | mullvadvpn-b5dffdf062a38f280c8d13a19e2574330e5190fc.tar.xz mullvadvpn-b5dffdf062a38f280c8d13a19e2574330e5190fc.zip | |
Create `BoxedError` type
| -rw-r--r-- | talpid-types/src/lib.rs | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/talpid-types/src/lib.rs b/talpid-types/src/lib.rs index 42a2dc4cf7..569bc94fb9 100644 --- a/talpid-types/src/lib.rs +++ b/talpid-types/src/lib.rs @@ -8,6 +8,8 @@ #![deny(rust_2018_idioms)] +use std::{error::Error, fmt}; + pub mod net; pub mod tunnel; @@ -20,7 +22,7 @@ pub trait ErrorExt { fn display_chain_with_msg(&self, msg: &str) -> String; } -impl<E: std::error::Error> ErrorExt for E { +impl<E: Error> ErrorExt for E { fn display_chain(&self) -> String { let mut s = format!("Error: {}", self); let mut source = self.source(); @@ -41,3 +43,28 @@ impl<E: std::error::Error> ErrorExt for E { s } } + +#[derive(Debug)] +pub struct BoxedError(Box<dyn Error>); + +impl fmt::Display for BoxedError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +impl Error for BoxedError { + fn description(&self) -> &str { + self.0.description() + } + + fn source(&self) -> Option<&(dyn Error + 'static)> { + self.0.source() + } +} + +impl BoxedError { + pub fn new(error: impl Error + 'static) -> Self { + BoxedError(Box::new(error)) + } +} |
