Skip to main content

wasmtime_wasi_tls/
error.rs

1use std::sync::Arc;
2
3/// TLS error
4pub struct Error(Arc<String>);
5
6impl Error {
7    /// Creates a new error with the given message.
8    pub fn msg<M>(message: M) -> Self
9    where
10        M: ToString,
11    {
12        Self(Arc::new(message.to_string()))
13    }
14}
15impl Clone for Error {
16    fn clone(&self) -> Self {
17        Self(Arc::clone(&self.0))
18    }
19}
20impl std::fmt::Debug for Error {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        std::fmt::Debug::fmt(&self.0, f)
23    }
24}
25impl std::fmt::Display for Error {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        std::fmt::Display::fmt(&self.0, f)
28    }
29}
30impl std::error::Error for Error {}
31impl From<std::io::Error> for Error {
32    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}
40impl From<Error> for std::io::Error {
41    fn from(err: Error) -> Self {
42        std::io::Error::other(err)
43    }
44}