From 7afbf74128483ddac05376b8d763e48be544c162 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Sat, 12 Jul 2025 12:45:36 +0100 Subject: [PATCH] Add `MaybeSend` bound to async methods on `ObjectLike` trait (sealed) --- src/table.rs | 25 ++++++++++++++++++------- src/traits.rs | 20 ++++++++++++++------ src/userdata/object.rs | 25 ++++++++++++++++++------- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/table.rs b/src/table.rs index 1147b34..ea3260a 100644 --- a/src/table.rs +++ b/src/table.rs @@ -13,7 +13,10 @@ use crate::util::{assert_stack, check_stack, get_metatable_ptr, StackGuard}; use crate::value::{Nil, Value}; #[cfg(feature = "async")] -use futures_util::future::{self, Either, Future}; +use { + crate::types::MaybeSend, + futures_util::future::{self, Either, Future}, +}; #[cfg(feature = "serde")] use { @@ -889,9 +892,9 @@ impl ObjectLike for Table { #[cfg(feature = "async")] #[inline] - fn call_async(&self, args: impl IntoLuaMulti) -> impl Future> + fn call_async(&self, args: impl IntoLuaMulti) -> impl Future> + MaybeSend where - R: FromLuaMulti, + R: FromLuaMulti + MaybeSend, { Function(self.0.copy()).call_async(args) } @@ -905,9 +908,13 @@ impl ObjectLike for Table { } #[cfg(feature = "async")] - fn call_async_method(&self, name: &str, args: impl IntoLuaMulti) -> impl Future> + fn call_async_method( + &self, + name: &str, + args: impl IntoLuaMulti, + ) -> impl Future> + MaybeSend where - R: FromLuaMulti, + R: FromLuaMulti + MaybeSend, { self.call_async_function(name, (self, args)) } @@ -925,9 +932,13 @@ impl ObjectLike for Table { #[cfg(feature = "async")] #[inline] - fn call_async_function(&self, name: &str, args: impl IntoLuaMulti) -> impl Future> + fn call_async_function( + &self, + name: &str, + args: impl IntoLuaMulti, + ) -> impl Future> + MaybeSend where - R: FromLuaMulti, + R: FromLuaMulti + MaybeSend, { match self.get(name) { Ok(Value::Function(func)) => Either::Left(func.call_async(args)), diff --git a/src/traits.rs b/src/traits.rs index 02373e2..0d11dce 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -162,9 +162,9 @@ pub trait ObjectLike: Sealed { /// arguments. #[cfg(feature = "async")] #[cfg_attr(docsrs, doc(cfg(feature = "async")))] - fn call_async(&self, args: impl IntoLuaMulti) -> impl Future> + fn call_async(&self, args: impl IntoLuaMulti) -> impl Future> + MaybeSend where - R: FromLuaMulti; + R: FromLuaMulti + MaybeSend; /// Gets the function associated to key `name` from the object and calls it, /// passing the object itself along with `args` as function arguments. @@ -178,9 +178,13 @@ pub trait ObjectLike: Sealed { /// This might invoke the `__index` metamethod. #[cfg(feature = "async")] #[cfg_attr(docsrs, doc(cfg(feature = "async")))] - fn call_async_method(&self, name: &str, args: impl IntoLuaMulti) -> impl Future> + fn call_async_method( + &self, + name: &str, + args: impl IntoLuaMulti, + ) -> impl Future> + MaybeSend where - R: FromLuaMulti; + R: FromLuaMulti + MaybeSend; /// Gets the function associated to key `name` from the object and calls it, /// passing `args` as function arguments. @@ -196,9 +200,13 @@ pub trait ObjectLike: Sealed { /// This might invoke the `__index` metamethod. #[cfg(feature = "async")] #[cfg_attr(docsrs, doc(cfg(feature = "async")))] - fn call_async_function(&self, name: &str, args: impl IntoLuaMulti) -> impl Future> + fn call_async_function( + &self, + name: &str, + args: impl IntoLuaMulti, + ) -> impl Future> + MaybeSend where - R: FromLuaMulti; + R: FromLuaMulti + MaybeSend; /// Converts the object to a string in a human-readable format. /// diff --git a/src/userdata/object.rs b/src/userdata/object.rs index 6418f1e..7dd9f71 100644 --- a/src/userdata/object.rs +++ b/src/userdata/object.rs @@ -8,7 +8,10 @@ use crate::value::Value; use crate::Function; #[cfg(feature = "async")] -use futures_util::future::{self, Either, Future}; +use { + crate::types::MaybeSend, + futures_util::future::{self, Either, Future}, +}; impl ObjectLike for AnyUserData { #[inline] @@ -35,9 +38,9 @@ impl ObjectLike for AnyUserData { #[cfg(feature = "async")] #[inline] - fn call_async(&self, args: impl IntoLuaMulti) -> impl Future> + fn call_async(&self, args: impl IntoLuaMulti) -> impl Future> + MaybeSend where - R: FromLuaMulti, + R: FromLuaMulti + MaybeSend, { Function(self.0.copy()).call_async(args) } @@ -51,9 +54,13 @@ impl ObjectLike for AnyUserData { } #[cfg(feature = "async")] - fn call_async_method(&self, name: &str, args: impl IntoLuaMulti) -> impl Future> + fn call_async_method( + &self, + name: &str, + args: impl IntoLuaMulti, + ) -> impl Future> + MaybeSend where - R: FromLuaMulti, + R: FromLuaMulti + MaybeSend, { self.call_async_function(name, (self, args)) } @@ -72,9 +79,13 @@ impl ObjectLike for AnyUserData { } #[cfg(feature = "async")] - fn call_async_function(&self, name: &str, args: impl IntoLuaMulti) -> impl Future> + fn call_async_function( + &self, + name: &str, + args: impl IntoLuaMulti, + ) -> impl Future> + MaybeSend where - R: FromLuaMulti, + R: FromLuaMulti + MaybeSend, { match self.get(name) { Ok(Value::Function(func)) => Either::Left(func.call_async(args)),