diff options
author | Alex Orlenko <zxteam@protonmail.com> | 2023-06-15 00:34:41 +0100 |
---|---|---|
committer | Alex Orlenko <zxteam@protonmail.com> | 2023-06-15 00:34:41 +0100 |
commit | 9fdba541e983ddee34c602863df64fd3fe9f26bf (patch) | |
tree | 9299f07c634a14dd52392802b85c59ae417bf39d /src/scope.rs | |
parent | cf0524aa23603a5c366d7905ba7c1fac45ee093b (diff) | |
download | mlua-9fdba541e983ddee34c602863df64fd3fe9f26bf.zip |
Update `UserDataMethods::add_async_method()` functions to take `&T` as second argument instead of cloning `T`.
New functions: `UserDataMethods::add_async_method_mut()`, `UserDataMethods::add_async_meta_method_mut()`.
Diffstat (limited to 'src/scope.rs')
-rw-r--r-- | src/scope.rs | 48 |
1 files changed, 40 insertions, 8 deletions
diff --git a/src/scope.rs b/src/scope.rs index 3d4cb94..a6f8b48 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -611,12 +611,28 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> for NonStaticUserDataMethods<'l } #[cfg(feature = "async")] - fn add_async_method<M, A, MR, R>(&mut self, _name: impl AsRef<str>, _method: M) + fn add_async_method<'s, M, A, MR, R>(&mut self, _name: impl AsRef<str>, _method: M) where - T: Clone, - M: Fn(&'lua Lua, T, A) -> MR + MaybeSend + 'static, + 'lua: 's, + T: 'static, + M: Fn(&'lua Lua, &'s T, A) -> MR + MaybeSend + 'static, + A: FromLuaMulti<'lua>, + MR: Future<Output = Result<R>> + 's, + R: IntoLuaMulti<'lua>, + { + // The panic should never happen as async non-static code wouldn't compile + // Non-static lifetime must be bounded to 'lua lifetime + panic!("asynchronous methods are not supported for non-static userdata") + } + + #[cfg(feature = "async")] + fn add_async_method_mut<'s, M, A, MR, R>(&mut self, _name: impl AsRef<str>, _method: M) + where + 'lua: 's, + T: 'static, + M: Fn(&'lua Lua, &'s mut T, A) -> MR + MaybeSend + 'static, A: FromLuaMulti<'lua>, - MR: Future<Output = Result<R>> + 'lua, + MR: Future<Output = Result<R>> + 's, R: IntoLuaMulti<'lua>, { // The panic should never happen as async non-static code wouldn't compile @@ -686,12 +702,28 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> for NonStaticUserDataMethods<'l } #[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))] - fn add_async_meta_method<M, A, MR, R>(&mut self, _name: impl AsRef<str>, _method: M) + fn add_async_meta_method<'s, M, A, MR, R>(&mut self, _name: impl AsRef<str>, _method: M) where - T: Clone, - M: Fn(&'lua Lua, T, A) -> MR + MaybeSend + 'static, + 'lua: 's, + T: 'static, + M: Fn(&'lua Lua, &'s T, A) -> MR + MaybeSend + 'static, + A: FromLuaMulti<'lua>, + MR: Future<Output = Result<R>> + 's, + R: IntoLuaMulti<'lua>, + { + // The panic should never happen as async non-static code wouldn't compile + // Non-static lifetime must be bounded to 'lua lifetime + panic!("asynchronous meta methods are not supported for non-static userdata") + } + + #[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))] + fn add_async_meta_method_mut<'s, M, A, MR, R>(&mut self, _name: impl AsRef<str>, _method: M) + where + 'lua: 's, + T: 'static, + M: Fn(&'lua Lua, &'s mut T, A) -> MR + MaybeSend + 'static, A: FromLuaMulti<'lua>, - MR: Future<Output = Result<R>> + 'lua, + MR: Future<Output = Result<R>> + 's, R: IntoLuaMulti<'lua>, { // The panic should never happen as async non-static code wouldn't compile |