diff options
author | Alex Orlenko <zxteam@protonmail.com> | 2021-09-26 00:37:32 +0100 |
---|---|---|
committer | Alex Orlenko <zxteam@protonmail.com> | 2021-11-04 00:57:49 +0000 |
commit | d7d987fa14f529a317e04b6ec85655787b12d2f6 (patch) | |
tree | 7f33ba91022d65dd4b6fe99f800f3cd63eb489e9 /src/userdata.rs | |
parent | 4d3ac6d8c5bc2fbc916959fd18fc39e9e8f5c3b4 (diff) | |
download | mlua-d7d987fa14f529a317e04b6ec85655787b12d2f6.zip |
Add async meta methods for all Lua except 51
Diffstat (limited to 'src/userdata.rs')
-rw-r--r-- | src/userdata.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/userdata.rs b/src/userdata.rs index f6bc0e6..fce6973 100644 --- a/src/userdata.rs +++ b/src/userdata.rs @@ -395,6 +395,25 @@ pub trait UserDataMethods<'lua, T: UserData> { R: ToLuaMulti<'lua>, M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> Result<R>; + /// Add an async metamethod which accepts a `T` as the first parameter and returns Future. + /// The passed `T` is cloned from the original value. + /// + /// This is an async version of [`add_meta_method`]. + /// + /// Requires `feature = "async"` + /// + /// [`add_meta_method`]: #method.add_meta_method + #[cfg(all(feature = "async", not(feature = "lua51")))] + #[cfg_attr(docsrs, doc(cfg(feature = "async")))] + fn add_async_meta_method<S, A, R, M, MR>(&mut self, name: S, method: M) + where + T: Clone, + S: Into<MetaMethod>, + A: FromLuaMulti<'lua>, + R: ToLuaMulti<'lua>, + M: 'static + MaybeSend + Fn(&'lua Lua, T, A) -> MR, + MR: 'lua + Future<Output = Result<R>>; + /// Add a metamethod which accepts generic arguments. /// /// Metamethods for binary operators can be triggered if either the left or right argument to @@ -419,6 +438,23 @@ pub trait UserDataMethods<'lua, T: UserData> { R: ToLuaMulti<'lua>, F: 'static + MaybeSend + FnMut(&'lua Lua, A) -> Result<R>; + /// Add a metamethod which accepts generic arguments and returns Future. + /// + /// This is an async version of [`add_meta_function`]. + /// + /// Requires `feature = "async"` + /// + /// [`add_meta_function`]: #method.add_meta_function + #[cfg(all(feature = "async", not(feature = "lua51")))] + #[cfg_attr(docsrs, doc(cfg(feature = "async")))] + fn add_async_meta_function<S, A, R, F, FR>(&mut self, name: S, function: F) + where + S: Into<MetaMethod>, + A: FromLuaMulti<'lua>, + R: ToLuaMulti<'lua>, + F: 'static + MaybeSend + Fn(&'lua Lua, A) -> FR, + FR: 'lua + Future<Output = Result<R>>; + // // Below are internal methods used in generated code // @@ -432,6 +468,15 @@ pub trait UserDataMethods<'lua, T: UserData> { #[doc(hidden)] fn add_meta_callback(&mut self, _meta: MetaMethod, _callback: Callback<'lua, 'static>) {} + + #[doc(hidden)] + #[cfg(feature = "async")] + fn add_async_meta_callback( + &mut self, + _meta: MetaMethod, + _callback: AsyncCallback<'lua, 'static>, + ) { + } } /// Field registry for [`UserData`] implementors. |