Skip to content

Commit

Permalink
Add error-send feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
khvzak committed Oct 23, 2024
1 parent 0d31a1c commit 8d8d521
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ luau-vector4 = ["luau", "ffi/luau-vector4"]
vendored = ["ffi/vendored"]
module = ["dep:mlua_derive", "ffi/module"]
async = ["dep:futures-util"]
send = ["parking_lot/send_guard"]
send = ["parking_lot/send_guard", "error-send"]
error-send = []
serialize = ["dep:serde", "dep:erased-serde", "dep:serde-value"]
macros = ["mlua_derive/macros"]
anyhow = ["dep:anyhow"]
anyhow = ["dep:anyhow", "error-send"]

[dependencies]
mlua_derive = { version = "=0.10.0-rc.1", optional = true, path = "mlua_derive" }
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Below is a list of the available feature flags. By default `mlua` does not enabl
* `module`: enable module mode (building loadable `cdylib` library for Lua)
* `async`: enable async/await support (any executor can be used, eg. [tokio] or [async-std])
* `send`: make `mlua::Lua: Send + Sync` (adds [`Send`] requirement to `mlua::Function` and `mlua::UserData`)
* `error-send`: make `mlua:Error: Send + Sync`
* `serialize`: add serialization and deserialization support to `mlua` types using [serde] framework
* `macros`: enable procedural macros (such as `chunk!`)
* `anyhow`: enable `anyhow::Error` conversion into Lua
Expand Down
22 changes: 19 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ use std::sync::Arc;

use crate::private::Sealed;

#[cfg(feature = "error-send")]
type DynStdError = dyn StdError + Send + Sync;

#[cfg(not(feature = "error-send"))]
type DynStdError = dyn StdError;

/// Error type returned by `mlua` methods.
#[derive(Debug, Clone)]
#[non_exhaustive]
Expand Down Expand Up @@ -191,7 +197,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<DynStdError>),
/// An error with additional context.
WithContext {
/// A string containing additional context.
Expand Down Expand Up @@ -345,7 +351,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<DynStdError>>>(err: T) -> Self {
Error::ExternalError(err.into().into())
}

Expand Down Expand Up @@ -406,7 +412,7 @@ pub trait ExternalError {
fn into_lua_err(self) -> Error;
}

impl<E: Into<Box<dyn StdError + Send + Sync>>> ExternalError for E {
impl<E: Into<Box<DynStdError>>> ExternalError for E {
fn into_lua_err(self) -> Error {
Error::external(self)
}
Expand Down Expand Up @@ -552,3 +558,13 @@ impl<'a> Iterator for Chain<'a> {
}
}
}

#[cfg(test)]
mod assertions {
use super::*;

#[cfg(not(feature = "error-send"))]
static_assertions::assert_not_impl_any!(Error: Send, Sync);
#[cfg(feature = "send")]
static_assertions::assert_impl_all!(Error: Send, Sync);
}

0 comments on commit 8d8d521

Please sign in to comment.