mlua_extras/typed/class/
mod.rs

1use mlua::{AnyUserData, FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, Lua, MetaMethod};
2
3use crate::MaybeSend;
4
5use super::{generator::FunctionBuilder, Typed, TypedMultiValue};
6
7mod wrapped;
8mod standard;
9
10pub use wrapped::WrappedBuilder;
11pub use standard::TypedClassBuilder;
12
13/// Typed variant of [`UserData`]
14pub trait TypedUserData: Sized {
15    /// Add documentation to the type itself
16    #[allow(unused_variables)]
17    fn add_documentation<F: TypedDataDocumentation<Self>>(docs: &mut F) {}
18
19    ///same as [UserData::add_methods].
20    ///Refer to its documentation on how to use it.
21    ///
22    ///only difference is that it takes a [TypedDataMethods],
23    ///which is the typed version of [UserDataMethods]
24    #[allow(unused_variables)]
25    fn add_methods<'lua, T: TypedDataMethods<'lua, Self>>(methods: &mut T) {}
26
27    /// same as [UserData::add_fields].
28    /// Refer to its documentation on how to use it.
29    ///
30    /// only difference is that it takes a [TypedDataFields],
31    /// which is the typed version of [UserDataFields]
32    #[allow(unused_variables)]
33    fn add_fields<'lua, F: TypedDataFields<'lua, Self>>(fields: &mut F) {}
34}
35
36/// Used inside of [`TypedUserData`] to add doc comments to the userdata type itself
37pub trait TypedDataDocumentation<T: TypedUserData> {
38    fn add(&mut self, doc: &str) -> &mut Self;
39}
40
41/// Typed variant of [`UserDataFields`]
42pub trait TypedDataMethods<'lua, T> {
43    /// Exposes a method to lua
44    fn add_method<S, A, R, M>(&mut self, name: &S, method: M)
45    where
46        S: ?Sized + AsRef<str>,
47        A: FromLuaMulti<'lua> + TypedMultiValue,
48        R: IntoLuaMulti<'lua> + TypedMultiValue,
49        M: 'static + MaybeSend + Fn(&'lua Lua, &T, A) -> mlua::Result<R>;
50
51    /// Exposes a method to lua
52    ///
53    /// Pass an additional callback that allows for param names, param doc comments, and return doc
54    /// comments to be specified.
55    fn add_method_with<S, A, R, M, G>(&mut self, name: &S, method: M, generator: G)
56    where
57        S: ?Sized + AsRef<str>,
58        A: FromLuaMulti<'lua> + TypedMultiValue,
59        R: IntoLuaMulti<'lua> + TypedMultiValue,
60        M: 'static + MaybeSend + Fn(&'lua Lua, &T, A) -> mlua::Result<R>,
61        G: Fn(&mut FunctionBuilder<A, R>);
62
63    /// Exposes a method to lua that has a mutable reference to Self
64    fn add_method_mut<S, A, R, M>(&mut self, name: &S, method: M)
65    where
66        S: ?Sized + AsRef<str>,
67        A: FromLuaMulti<'lua> + TypedMultiValue,
68        R: IntoLuaMulti<'lua> + TypedMultiValue,
69        M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> mlua::Result<R>;
70
71    /// Exposes a method to lua that has a mutable reference to Self
72    ///
73    /// Pass an additional callback that allows for param names, param doc comments, and return doc
74    /// comments to be specified.
75    fn add_method_mut_with<S, A, R, M, G>(&mut self, name: &S, method: M, generator: G)
76    where
77        S: ?Sized + AsRef<str>,
78        A: FromLuaMulti<'lua> + TypedMultiValue,
79        R: IntoLuaMulti<'lua> + TypedMultiValue,
80        M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> mlua::Result<R>,
81        G: Fn(&mut FunctionBuilder<A, R>);
82
83    #[cfg(feature = "async")]
84    ///exposes an async method to lua
85    fn add_async_method<'s, S: ?Sized + AsRef<str>, A, R, M, MR>(&mut self, name: &S, method: M)
86    where
87        'lua: 's,
88        T: 'static,
89        M: Fn(&'lua Lua, &'s T, A) -> MR + MaybeSend + 'static,
90        A: FromLuaMulti<'lua> + TypedMultiValue,
91        MR: std::future::Future<Output = mlua::Result<R>> + 's,
92        R: IntoLuaMulti<'lua> + TypedMultiValue;
93
94    #[cfg(feature = "async")]
95    ///exposes an async method to lua
96    ///
97    /// Pass an additional callback that allows for param names, param doc comments, and return doc
98    /// comments to be specified.
99    fn add_async_method_with<'s, S: ?Sized + AsRef<str>, A, R, M, MR, G>(&mut self, name: &S, method: M, generator: G)
100    where
101        'lua: 's,
102        T: 'static,
103        M: Fn(&'lua Lua, &'s T, A) -> MR + MaybeSend + 'static,
104        A: FromLuaMulti<'lua> + TypedMultiValue,
105        MR: std::future::Future<Output = mlua::Result<R>> + 's,
106        R: IntoLuaMulti<'lua> + TypedMultiValue,
107        G: Fn(&mut FunctionBuilder<A, R>);
108
109    #[cfg(feature = "async")]
110    ///exposes an async method to lua
111    fn add_async_method_mut<'s, S: ?Sized + AsRef<str>, A, R, M, MR>(&mut self, name: &S, method: M)
112    where
113        'lua: 's,
114        T: 'static,
115        M: Fn(&'lua Lua, &'s mut T, A) -> MR + MaybeSend + 'static,
116        A: FromLuaMulti<'lua> + TypedMultiValue,
117        MR: std::future::Future<Output = mlua::Result<R>> + 's,
118        R: IntoLuaMulti<'lua> + TypedMultiValue;
119
120    #[cfg(feature = "async")]
121    ///exposes an async method to lua
122    ///
123    /// Pass an additional callback that allows for param names, param doc comments, and return doc
124    /// comments to be specified.
125    fn add_async_method_mut_with<'s, S: ?Sized + AsRef<str>, A, R, M, MR, G>(&mut self, name: &S, method: M, generator: G)
126    where
127        'lua: 's,
128        T: 'static,
129        M: Fn(&'lua Lua, &'s mut T, A) -> MR + MaybeSend + 'static,
130        A: FromLuaMulti<'lua> + TypedMultiValue,
131        MR: std::future::Future<Output = mlua::Result<R>> + 's,
132        R: IntoLuaMulti<'lua> + TypedMultiValue,
133        G: Fn(&mut FunctionBuilder<A, R>);
134
135    ///Exposes a function to lua (its a method that does not take Self)
136    fn add_function<S, A, R, F>(&mut self, name: &S, function: F)
137    where
138        S: ?Sized + AsRef<str>,
139        A: FromLuaMulti<'lua> + TypedMultiValue,
140        R: IntoLuaMulti<'lua> + TypedMultiValue,
141        F: 'static + MaybeSend + Fn(&'lua Lua, A) -> mlua::Result<R>;
142
143    ///Exposes a function to lua (its a method that does not take Self)
144    ///
145    /// Pass an additional callback that allows for param names, param doc comments, and return doc
146    /// comments to be specified.
147    fn add_function_with<S, A, R, F, G>(&mut self, name: &S, function: F, generator: G)
148    where
149        S: ?Sized + AsRef<str>,
150        A: FromLuaMulti<'lua> + TypedMultiValue,
151        R: IntoLuaMulti<'lua> + TypedMultiValue,
152        F: 'static + MaybeSend + Fn(&'lua Lua, A) -> mlua::Result<R>,
153        G: Fn(&mut FunctionBuilder<A, R>);
154
155    ///Exposes a mutable function to lua
156    fn add_function_mut<S, A, R, F>(&mut self, name: &S, function: F)
157    where
158        S: ?Sized + AsRef<str>,
159        A: FromLuaMulti<'lua> + TypedMultiValue,
160        R: IntoLuaMulti<'lua> + TypedMultiValue,
161        F: 'static + MaybeSend + FnMut(&'lua Lua, A) -> mlua::Result<R>;
162
163    ///Exposes a mutable function to lua
164    ///
165    /// Pass an additional callback that allows for param names, param doc comments, and return doc
166    /// comments to be specified.
167    fn add_function_mut_with<S, A, R, F, G>(&mut self, name: &S, function: F, generator: G)
168    where
169        S: ?Sized + AsRef<str>,
170        A: FromLuaMulti<'lua> + TypedMultiValue,
171        R: IntoLuaMulti<'lua> + TypedMultiValue,
172        F: 'static + MaybeSend + FnMut(&'lua Lua, A) -> mlua::Result<R>,
173        G: Fn(&mut FunctionBuilder<A, R>);
174
175    #[cfg(feature = "async")]
176    ///exposes an async function to lua
177    fn add_async_function<S: ?Sized, A, R, F, FR>(&mut self, name: &S, function: F)
178    where
179        S: AsRef<str>,
180        A: FromLuaMulti<'lua> + TypedMultiValue,
181        R: IntoLuaMulti<'lua> + TypedMultiValue,
182        F: 'static + MaybeSend + Fn(&'lua Lua, A) -> FR,
183        FR: 'lua + std::future::Future<Output = mlua::Result<R>>;
184
185    #[cfg(feature = "async")]
186    ///exposes an async function to lua
187    ///
188    /// Pass an additional callback that allows for param names, param doc comments, and return doc
189    /// comments to be specified.
190    fn add_async_function_with<S: ?Sized, A, R, F, FR, G>(&mut self, name: &S, function: F, generator: G)
191    where
192        S: AsRef<str>,
193        A: FromLuaMulti<'lua> + TypedMultiValue,
194        R: IntoLuaMulti<'lua> + TypedMultiValue,
195        F: 'static + MaybeSend + Fn(&'lua Lua, A) -> FR,
196        FR: 'lua + std::future::Future<Output = mlua::Result<R>>,
197        G: Fn(&mut FunctionBuilder<A, R>);
198
199    ///Exposes a meta method to lua [http://lua-users.org/wiki/MetatableEvents](http://lua-users.org/wiki/MetatableEvents)
200    fn add_meta_method<A, R, M>(&mut self, meta: MetaMethod, method: M)
201    where
202        A: FromLuaMulti<'lua> + TypedMultiValue,
203        R: IntoLuaMulti<'lua> + TypedMultiValue,
204        M: 'static + MaybeSend + Fn(&'lua Lua, &T, A) -> mlua::Result<R>;
205
206    ///Exposes a meta method to lua [http://lua-users.org/wiki/MetatableEvents](http://lua-users.org/wiki/MetatableEvents)
207    ///
208    /// Pass an additional callback that allows for param names, param doc comments, and return doc
209    /// comments to be specified.
210    fn add_meta_method_with<A, R, M, G>(&mut self, meta: MetaMethod, method: M, generator: G)
211    where
212        A: FromLuaMulti<'lua> + TypedMultiValue,
213        R: IntoLuaMulti<'lua> + TypedMultiValue,
214        M: 'static + MaybeSend + Fn(&'lua Lua, &T, A) -> mlua::Result<R>,
215        G: Fn(&mut FunctionBuilder<A, R>);
216
217    ///Exposes a meta and mutable method to lua [http://lua-users.org/wiki/MetatableEvents](http://lua-users.org/wiki/MetatableEvents)
218    fn add_meta_method_mut<A, R, M>(&mut self, meta: MetaMethod, method: M)
219    where
220        A: FromLuaMulti<'lua> + TypedMultiValue,
221        R: IntoLuaMulti<'lua> + TypedMultiValue,
222        M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> mlua::Result<R>;
223
224    ///Exposes a meta and mutable method to lua [http://lua-users.org/wiki/MetatableEvents](http://lua-users.org/wiki/MetatableEvents)
225    ///
226    /// Pass an additional callback that allows for param names, param doc comments, and return doc
227    /// comments to be specified.
228    fn add_meta_method_mut_with<A, R, M, G>(&mut self, meta: MetaMethod, method: M, generator: G)
229    where
230        A: FromLuaMulti<'lua> + TypedMultiValue,
231        R: IntoLuaMulti<'lua> + TypedMultiValue,
232        M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> mlua::Result<R>,
233        G: Fn(&mut FunctionBuilder<A, R>);
234
235    ///Exposes a meta function to lua [http://lua-users.org/wiki/MetatableEvents](http://lua-users.org/wiki/MetatableEvents)
236    fn add_meta_function<A, R, F>(&mut self, meta: MetaMethod, function: F)
237    where
238        A: FromLuaMulti<'lua> + TypedMultiValue,
239        R: IntoLuaMulti<'lua> + TypedMultiValue,
240        F: 'static + MaybeSend + Fn(&'lua Lua, A) -> mlua::Result<R>;
241
242    ///Exposes a meta function to lua [http://lua-users.org/wiki/MetatableEvents](http://lua-users.org/wiki/MetatableEvents)
243    ///
244    /// Pass an additional callback that allows for param names, param doc comments, and return doc
245    /// comments to be specified.
246    fn add_meta_function_with<A, R, F, G>(&mut self, meta: MetaMethod, function: F, generator: G)
247    where
248        A: FromLuaMulti<'lua> + TypedMultiValue,
249        R: IntoLuaMulti<'lua> + TypedMultiValue,
250        F: 'static + MaybeSend + Fn(&'lua Lua, A) -> mlua::Result<R>,
251        G: Fn(&mut FunctionBuilder<A, R>);
252
253    ///Exposes a meta and mutable function to lua [http://lua-users.org/wiki/MetatableEvents](http://lua-users.org/wiki/MetatableEvents)
254    fn add_meta_function_mut<A, R, F>(&mut self, meta: MetaMethod, function: F)
255    where
256        A: FromLuaMulti<'lua> + TypedMultiValue,
257        R: IntoLuaMulti<'lua> + TypedMultiValue,
258        F: 'static + MaybeSend + FnMut(&'lua Lua, A) -> mlua::Result<R>;
259
260    ///Exposes a meta and mutable function to lua [http://lua-users.org/wiki/MetatableEvents](http://lua-users.org/wiki/MetatableEvents)
261    fn add_meta_function_mut_with<A, R, F, G>(&mut self, meta: MetaMethod, function: F, generator: G)
262    where
263        A: FromLuaMulti<'lua> + TypedMultiValue,
264        R: IntoLuaMulti<'lua> + TypedMultiValue,
265        F: 'static + MaybeSend + FnMut(&'lua Lua, A) -> mlua::Result<R>,
266        G: Fn(&mut FunctionBuilder<A, R>);
267
268    ///Adds documentation to the next method/function that gets added
269    fn document(&mut self, doc: &str) -> &mut Self;
270}
271
272/// Typed variant of [`UserDataMethods`]
273pub trait TypedDataFields<'lua, T> {
274    ///Adds documentation to the next field that gets added
275    fn document(&mut self, doc: &str) -> &mut Self;
276
277    /// Typed version of [add_field](mlua::UserDataFields::add_field)
278    fn add_field<V>(&mut self, name: impl AsRef<str>, value: V)
279    where
280        V: IntoLua<'lua> + Clone + 'static + Typed;
281
282    /// Typed version of [add_field_method_get](mlua::UserDataFields::add_field_method_get)
283    fn add_field_method_get<S, R, M>(&mut self, name: &S, method: M)
284    where
285        S: AsRef<str> + ?Sized,
286        R: IntoLua<'lua> + Typed,
287        M: 'static + MaybeSend + Fn(&'lua Lua, &T) -> mlua::Result<R>;
288
289    /// Typed version of [dd_field_method_set](mlua::UserDataFields::add_field_method_set)
290    fn add_field_method_set<S, A, M>(&mut self, name: &S, method: M)
291    where
292        S: AsRef<str> + ?Sized,
293        A: FromLua<'lua> + Typed,
294        M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> mlua::Result<()>;
295
296    /// Typed version of [add_field_method_get](mlua::UserDataFields::add_field_method_get) and [add_field_method_set](mlua::UserDataFields::add_field_method_set) combined
297    fn add_field_method_get_set<S, R, A, GET, SET>(&mut self, name: &S, get: GET, set: SET)
298    where
299        S: AsRef<str> + ?Sized,
300        R: IntoLua<'lua> + Typed,
301        A: FromLua<'lua> + Typed,
302        GET: 'static + MaybeSend + Fn(&'lua Lua, &T) -> mlua::Result<R>,
303        SET: 'static + MaybeSend + Fn(&'lua Lua, &mut T, A) -> mlua::Result<()>;
304
305    /// Typed version of [add_field_function_get](mlua::UserDataFields::add_field_function_get)
306    fn add_field_function_get<S, R, F>(&mut self, name: &S, function: F)
307    where
308        S: AsRef<str> + ?Sized,
309        R: IntoLua<'lua> + Typed,
310        F: 'static + MaybeSend + Fn(&'lua Lua, AnyUserData<'lua>) -> mlua::Result<R>;
311
312    /// Typed version of [add_field_function_set](mlua::UserDataFields::add_field_function_set)
313    fn add_field_function_set<S, A, F>(&mut self, name: &S, function: F)
314    where
315        S: AsRef<str> + ?Sized,
316        A: FromLua<'lua> + Typed,
317        F: 'static + MaybeSend + FnMut(&'lua Lua, AnyUserData<'lua>, A) -> mlua::Result<()>;
318
319    /// Typed version of [add_field_function_get](mlua::UserDataFields::add_field_function_get) and [add_field_function_set](mlua::UserDataFields::add_field_function_set) combined
320    fn add_field_function_get_set<S, R, A, GET, SET>(&mut self, name: &S, get: GET, set: SET)
321    where
322        S: AsRef<str> + ?Sized,
323        R: IntoLua<'lua> + Typed,
324        A: FromLua<'lua> + Typed,
325        GET: 'static + MaybeSend + Fn(&'lua Lua, AnyUserData<'lua>) -> mlua::Result<R>,
326        SET: 'static + MaybeSend + Fn(&'lua Lua, AnyUserData<'lua>, A) -> mlua::Result<()>;
327
328    /// Typed version of [add_meta_field](mlua::UserDataFields::add_meta_field)
329    fn add_meta_field<R, F>(&mut self, meta: MetaMethod, f: F)
330    where
331        F: 'static + MaybeSend + Fn(&'lua Lua) -> mlua::Result<R>,
332        R: IntoLua<'lua> + Typed;
333}