1 use std::sync::Arc;
2 
3 /// TLS error
4 pub struct Error(Arc<String>);
5 
6 impl Error {
7     /// Creates a new error with the given message.
msg<M>(message: M) -> Self where M: ToString,8     pub fn msg<M>(message: M) -> Self
9     where
10         M: ToString,
11     {
12         Self(Arc::new(message.to_string()))
13     }
14 }
15 impl Clone for Error {
clone(&self) -> Self16     fn clone(&self) -> Self {
17         Self(Arc::clone(&self.0))
18     }
19 }
20 impl std::fmt::Debug for Error {
fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result21     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22         std::fmt::Debug::fmt(&self.0, f)
23     }
24 }
25 impl std::fmt::Display for Error {
fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result26     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27         std::fmt::Display::fmt(&self.0, f)
28     }
29 }
30 impl std::error::Error for Error {}
31 impl From<std::io::Error> for Error {
from(err: std::io::Error) -> Self32     fn from(err: std::io::Error) -> Self {
33         // Try to recover the original error:
34         match err.downcast::<Error>() {
35             Ok(e) => e,
36             Err(io_err) => Self::msg(io_err),
37         }
38     }
39 }
40 impl From<Error> for std::io::Error {
from(err: Error) -> Self41     fn from(err: Error) -> Self {
42         std::io::Error::other(err)
43     }
44 }
45