Skip to content

Commit 8d8d521

Browse files
committed
Add error-send feature flag
1 parent 0d31a1c commit 8d8d521

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ luau-vector4 = ["luau", "ffi/luau-vector4"]
3838
vendored = ["ffi/vendored"]
3939
module = ["dep:mlua_derive", "ffi/module"]
4040
async = ["dep:futures-util"]
41-
send = ["parking_lot/send_guard"]
41+
send = ["parking_lot/send_guard", "error-send"]
42+
error-send = []
4243
serialize = ["dep:serde", "dep:erased-serde", "dep:serde-value"]
4344
macros = ["mlua_derive/macros"]
44-
anyhow = ["dep:anyhow"]
45+
anyhow = ["dep:anyhow", "error-send"]
4546

4647
[dependencies]
4748
mlua_derive = { version = "=0.10.0-rc.1", optional = true, path = "mlua_derive" }

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Below is a list of the available feature flags. By default `mlua` does not enabl
5555
* `module`: enable module mode (building loadable `cdylib` library for Lua)
5656
* `async`: enable async/await support (any executor can be used, eg. [tokio] or [async-std])
5757
* `send`: make `mlua::Lua: Send + Sync` (adds [`Send`] requirement to `mlua::Function` and `mlua::UserData`)
58+
* `error-send`: make `mlua:Error: Send + Sync`
5859
* `serialize`: add serialization and deserialization support to `mlua` types using [serde] framework
5960
* `macros`: enable procedural macros (such as `chunk!`)
6061
* `anyhow`: enable `anyhow::Error` conversion into Lua

src/error.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ use std::sync::Arc;
99

1010
use crate::private::Sealed;
1111

12+
#[cfg(feature = "error-send")]
13+
type DynStdError = dyn StdError + Send + Sync;
14+
15+
#[cfg(not(feature = "error-send"))]
16+
type DynStdError = dyn StdError;
17+
1218
/// Error type returned by `mlua` methods.
1319
#[derive(Debug, Clone)]
1420
#[non_exhaustive]
@@ -191,7 +197,7 @@ pub enum Error {
191197
/// Returning `Err(ExternalError(...))` from a Rust callback will raise the error as a Lua
192198
/// error. The Rust code that originally invoked the Lua code then receives a `CallbackError`,
193199
/// from which the original error (and a stack traceback) can be recovered.
194-
ExternalError(Arc<dyn StdError + Send + Sync>),
200+
ExternalError(Arc<DynStdError>),
195201
/// An error with additional context.
196202
WithContext {
197203
/// A string containing additional context.
@@ -345,7 +351,7 @@ impl Error {
345351

346352
/// Wraps an external error object.
347353
#[inline]
348-
pub fn external<T: Into<Box<dyn StdError + Send + Sync>>>(err: T) -> Self {
354+
pub fn external<T: Into<Box<DynStdError>>>(err: T) -> Self {
349355
Error::ExternalError(err.into().into())
350356
}
351357

@@ -406,7 +412,7 @@ pub trait ExternalError {
406412
fn into_lua_err(self) -> Error;
407413
}
408414

409-
impl<E: Into<Box<dyn StdError + Send + Sync>>> ExternalError for E {
415+
impl<E: Into<Box<DynStdError>>> ExternalError for E {
410416
fn into_lua_err(self) -> Error {
411417
Error::external(self)
412418
}
@@ -552,3 +558,13 @@ impl<'a> Iterator for Chain<'a> {
552558
}
553559
}
554560
}
561+
562+
#[cfg(test)]
563+
mod assertions {
564+
use super::*;
565+
566+
#[cfg(not(feature = "error-send"))]
567+
static_assertions::assert_not_impl_any!(Error: Send, Sync);
568+
#[cfg(feature = "send")]
569+
static_assertions::assert_impl_all!(Error: Send, Sync);
570+
}

0 commit comments

Comments
 (0)