diff options
| -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)) + } +} |
