diff options
author | Alex Orlenko <zxteam@protonmail.com> | 2023-07-11 00:20:52 +0100 |
---|---|---|
committer | Alex Orlenko <zxteam@protonmail.com> | 2023-07-11 00:20:52 +0100 |
commit | 4adc3116f9ba2a374d2e1a69558994f23302658c (patch) | |
tree | 7fb54c185915738b9f17b99e9f9c259606a01470 | |
parent | 8e0bdc9934055c52182c3ab25691ed028f9d4db6 (diff) | |
download | mlua-4adc3116f9ba2a374d2e1a69558994f23302658c.zip |
Add error-send feature
-rw-r--r-- | Cargo.toml | 5 | ||||
-rw-r--r-- | src/error.rs | 13 |
2 files changed, 13 insertions, 5 deletions
@@ -37,7 +37,8 @@ luau-vector4 = ["luau", "ffi/luau-vector4"] vendored = ["ffi/vendored"] module = ["mlua_derive", "ffi/module"] async = ["futures-util"] -send = [] +send = ["error-send"] +error-send = [] serialize = ["serde", "erased-serde", "serde-value"] macros = ["mlua_derive/macros"] unstable = [] @@ -85,7 +86,7 @@ required-features = ["async", "serialize", "macros"] [[example]] name = "async_http_server" -required-features = ["async", "macros"] +required-features = ["async", "macros", "error-send"] [[example]] name = "async_tcp_server" diff --git a/src/error.rs b/src/error.rs index f04a440..dc44686 100644 --- a/src/error.rs +++ b/src/error.rs @@ -9,6 +9,11 @@ use std::sync::Arc; use crate::private::Sealed; +#[cfg(feature = "error-send")] +type StdErrorObj = dyn StdError + Send + Sync; +#[cfg(not(feature = "error-send"))] +type StdErrorObj = dyn StdError; + /// Error type returned by `mlua` methods. #[derive(Debug, Clone)] #[non_exhaustive] @@ -189,7 +194,7 @@ pub enum Error { /// Returning `Err(ExternalError(...))` from a Rust callback will raise the error as a Lua /// error. The Rust code that originally invoked the Lua code then receives a `CallbackError`, /// from which the original error (and a stack traceback) can be recovered. - ExternalError(Arc<dyn StdError + Send + Sync>), + ExternalError(Arc<StdErrorObj>), /// An error with additional context. WithContext { /// A string containing additional context. @@ -345,7 +350,7 @@ impl Error { /// Wraps an external error object. #[inline] - pub fn external<T: Into<Box<dyn StdError + Send + Sync>>>(err: T) -> Self { + pub fn external<T: Into<Box<StdErrorObj>>>(err: T) -> Self { Error::ExternalError(err.into().into()) } @@ -386,16 +391,18 @@ impl Error { } } +/// Trait for converting [`std::error::Error`] into Lua [`Error`]. pub trait ExternalError { fn into_lua_err(self) -> Error; } -impl<E: Into<Box<dyn StdError + Send + Sync>>> ExternalError for E { +impl<E: Into<Box<StdErrorObj>>> ExternalError for E { fn into_lua_err(self) -> Error { Error::external(self) } } +/// Trait for converting [`std::result::Result`] into Lua [`Result`]. pub trait ExternalResult<T> { fn into_lua_err(self) -> Result<T>; } |