Skip to main content

mlua_extras/typed/class/
mod.rs

1use mlua::{AnyUserData, FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, Lua};
2#[cfg(feature = "async")]
3use mlua::{UserDataRef, UserDataRefMut};
4
5use crate::{MaybeSend, typed::IntoDocComment};
6
7use super::{Typed, TypedMultiValue, Type};
8
9mod wrapped;
10mod standard;
11
12pub use wrapped::WrappedBuilder;
13pub use standard::{TypedClassBuilder, TypedClass};
14
15/// Typed variant of [`mlua::UserData`]
16pub trait TypedUserData: Sized {
17    /// Add documentation to the type itself
18    #[allow(unused_variables)]
19    fn add_documentation<F: TypedDataDocumentation<Self>>(docs: &mut F) {}
20
21    ///same as [`mlua::UserData::add_methods`].
22    ///Refer to its documentation on how to use it.
23    ///
24    ///only difference is that it takes a [TypedDataMethods],
25    ///which is the typed version of [`mlua::UserDataMethods`]
26    #[allow(unused_variables)]
27    fn add_methods<T: TypedDataMethods<Self>>(methods: &mut T) {}
28
29    /// same as [`mlua::UserData::add_fields`].
30    /// Refer to its documentation on how to use it.
31    ///
32    /// only difference is that it takes a [TypedDataFields],
33    /// which is the typed version of [`mlua::UserDataFields`]
34    #[allow(unused_variables)]
35    fn add_fields<F: TypedDataFields<Self>>(fields: &mut F) {}
36}
37
38/// Used inside of [`TypedUserData`] to add doc comments to the userdata type itself
39pub trait TypedDataDocumentation<T: TypedUserData> {
40    fn add(&mut self, doc: &str) -> &mut Self;
41}
42
43/// Typed variant of [`mlua::UserDataMethods`]
44pub trait TypedDataMethods<T> {
45    /// Exposes a method to lua
46    fn add_method<S, A, R, M>(&mut self, name: S, method: M)
47    where
48        S: Into<String>,
49        A: FromLuaMulti + TypedMultiValue,
50        R: IntoLuaMulti + TypedMultiValue,
51        M: 'static + MaybeSend + Fn(&Lua, &T, A) -> mlua::Result<R>;
52
53    /// Exposes a method to lua that has a mutable reference to Self
54    fn add_method_mut<S, A, R, M>(&mut self, name: S, method: M)
55    where
56        S: Into<String>,
57        A: FromLuaMulti + TypedMultiValue,
58        R: IntoLuaMulti + TypedMultiValue,
59        M: 'static + MaybeSend + FnMut(&Lua, &mut T, A) -> mlua::Result<R>;
60
61    #[cfg(feature = "async")]
62    ///exposes an async method to lua
63    fn add_async_method<S: Into<String>, A, R, M, MR>(&mut self, name: S, method: M)
64    where
65        T: 'static,
66        M: Fn(Lua, UserDataRef<T>, A) -> MR + MaybeSend + 'static,
67        A: FromLuaMulti + TypedMultiValue,
68        MR: std::future::Future<Output = mlua::Result<R>> + MaybeSend + 'static,
69        R: IntoLuaMulti + TypedMultiValue;
70
71    #[cfg(feature = "async")]
72    ///exposes an async method to lua
73    fn add_async_method_mut<S: Into<String>, A, R, M, MR>(&mut self, name: S, method: M)
74    where
75        T: 'static,
76        M: Fn(Lua, UserDataRefMut<T>, A) -> MR + MaybeSend + 'static,
77        A: FromLuaMulti + TypedMultiValue,
78        MR: std::future::Future<Output = mlua::Result<R>> + MaybeSend + 'static,
79        R: IntoLuaMulti + TypedMultiValue;
80
81    ///Exposes a function to lua (its a method that does not take Self)
82    fn add_function<S, A, R, F>(&mut self, name: S, function: F)
83    where
84        S: Into<String>,
85        A: FromLuaMulti + TypedMultiValue,
86        R: IntoLuaMulti + TypedMultiValue,
87        F: 'static + MaybeSend + Fn(&Lua, A) -> mlua::Result<R>;
88
89    ///Exposes a mutable function to lua
90    fn add_function_mut<S, A, R, F>(&mut self, name: S, function: F)
91    where
92        S: Into<String>,
93        A: FromLuaMulti + TypedMultiValue,
94        R: IntoLuaMulti + TypedMultiValue,
95        F: 'static + MaybeSend + FnMut(&Lua, A) -> mlua::Result<R>;
96
97    #[cfg(feature = "async")]
98    ///exposes an async function to lua
99    fn add_async_function<S, A, R, F, FR>(&mut self, name: S, function: F)
100    where
101        S: Into<String>,
102        A: FromLuaMulti + TypedMultiValue,
103        R: IntoLuaMulti + TypedMultiValue,
104        F: 'static + MaybeSend + Fn(Lua, A) -> FR,
105        FR: 'static + MaybeSend + std::future::Future<Output = mlua::Result<R>>;
106
107    ///Exposes a meta method to lua [http://lua-users.org/wiki/MetatableEvents](http://lua-users.org/wiki/MetatableEvents)
108    fn add_meta_method<A, R, M>(&mut self, meta: impl Into<String>, method: M)
109    where
110        A: FromLuaMulti + TypedMultiValue,
111        R: IntoLuaMulti + TypedMultiValue,
112        M: 'static + MaybeSend + Fn(&Lua, &T, A) -> mlua::Result<R>;
113
114    ///Exposes a meta and mutable method to lua [http://lua-users.org/wiki/MetatableEvents](http://lua-users.org/wiki/MetatableEvents)
115    fn add_meta_method_mut<A, R, M>(&mut self, meta: impl Into<String>, method: M)
116    where
117        A: FromLuaMulti + TypedMultiValue,
118        R: IntoLuaMulti + TypedMultiValue,
119        M: 'static + MaybeSend + FnMut(&Lua, &mut T, A) -> mlua::Result<R>;
120
121    ///Exposes a meta function to lua [http://lua-users.org/wiki/MetatableEvents](http://lua-users.org/wiki/MetatableEvents)
122    fn add_meta_function<A, R, F>(&mut self, meta: impl Into<String>, function: F)
123    where
124        A: FromLuaMulti + TypedMultiValue,
125        R: IntoLuaMulti + TypedMultiValue,
126        F: 'static + MaybeSend + Fn(&Lua, A) -> mlua::Result<R>;
127
128    ///Exposes a meta and mutable function to lua [http://lua-users.org/wiki/MetatableEvents](http://lua-users.org/wiki/MetatableEvents)
129    fn add_meta_function_mut<A, R, F>(&mut self, meta: impl Into<String>, function: F)
130    where
131        A: FromLuaMulti + TypedMultiValue,
132        R: IntoLuaMulti + TypedMultiValue,
133        F: 'static + MaybeSend + FnMut(&Lua, A) -> mlua::Result<R>;
134
135    /// Adds documentation to the next method/function that gets added
136    fn document(&mut self, doc: impl IntoDocComment) -> &mut Self;
137
138    /// Adds a param name and doc comment to the next method/function that gets added.
139    /// 
140    /// These will be applied to the params in the order they were defined.
141    fn param(&mut self, name: impl std::fmt::Display, doc: impl IntoDocComment) -> &mut Self;
142
143    /// Adds a param name and doc comment to the next method/function that gets added.
144    /// Will also add an override type to the param.
145    /// 
146    /// These will be applied to the params in the order they were defined.
147    fn param_as(&mut self, ty: impl Into<Type>, name: impl std::fmt::Display, doc: impl IntoDocComment) -> &mut Self;
148
149    /// Adds a return doc comment to the next method/function that gets added.
150    /// 
151    /// These will be applied to the returns in the order they were defined.
152    fn ret(&mut self, doc: impl IntoDocComment) -> &mut Self;
153    
154    /// Adds a return doc comment to the next method/function that gets added.
155    /// Will also add an override type to the return.
156    /// 
157    /// These will be applied to the returns in the order they were defined.
158    fn ret_as(&mut self, ty: impl Into<Type>, doc: impl IntoDocComment) -> &mut Self;
159
160    /// Adds an index field with a type and doc comment to the class definition
161    fn index<I: Typed>(&mut self, idx: isize, doc: impl IntoDocComment) -> &mut Self;
162
163    /// Adds an index field with a type and doc comment to the class definition
164    fn index_as(&mut self, idx: isize, ty: impl Into<Type>, doc: impl IntoDocComment) -> &mut Self;
165}
166
167/// Typed variant of [`mlua::UserDataFields`]
168pub trait TypedDataFields<T> {
169    ///Adds documentation to the next field that gets added
170    fn document(&mut self, doc: impl IntoDocComment) -> &mut Self;
171
172    /// Adds a type to the queued overrides.
173    /// 
174    /// It will be used on the next field and will override the type that is automatically used.
175    fn coerce(&mut self, ty: impl Into<Type>) -> &mut Self;
176
177    /// Typed version of [add_field](mlua::UserDataFields::add_field)
178    fn add_field<V>(&mut self, name: impl Into<String>, value: V)
179    where
180        V: IntoLua + Clone + 'static + Typed;
181
182    /// Typed version of [add_field_method_get](mlua::UserDataFields::add_field_method_get)
183    fn add_field_method_get<S, R, M>(&mut self, name: S, method: M)
184    where
185        S: Into<String>,
186        R: IntoLua + Typed,
187        M: 'static + MaybeSend + Fn(&Lua, &T) -> mlua::Result<R>;
188
189    /// Typed version of [dd_field_method_set](mlua::UserDataFields::add_field_method_set)
190    fn add_field_method_set<S, A, M>(&mut self, name: S, method: M)
191    where
192        S: Into<String>,
193        A: FromLua + Typed,
194        M: 'static + MaybeSend + FnMut(&Lua, &mut T, A) -> mlua::Result<()>;
195
196    /// 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
197    fn add_field_method_get_set<S, R, A, GET, SET>(&mut self, name: S, get: GET, set: SET)
198    where
199        S: Into<String>,
200        R: IntoLua + Typed,
201        A: FromLua + Typed,
202        GET: 'static + MaybeSend + Fn(& Lua, &T) -> mlua::Result<R>,
203        SET: 'static + MaybeSend + Fn(& Lua, &mut T, A) -> mlua::Result<()>;
204
205    /// Typed version of [add_field_function_get](mlua::UserDataFields::add_field_function_get)
206    fn add_field_function_get<S, R, F>(&mut self, name: S, function: F)
207    where
208        S: Into<String>,
209        R: IntoLua + Typed,
210        F: 'static + MaybeSend + Fn(&Lua, AnyUserData) -> mlua::Result<R>;
211
212    /// Typed version of [add_field_function_set](mlua::UserDataFields::add_field_function_set)
213    fn add_field_function_set<S, A, F>(&mut self, name: S, function: F)
214    where
215        S: Into<String>,
216        A: FromLua + Typed,
217        F: 'static + MaybeSend + FnMut(&Lua, AnyUserData, A) -> mlua::Result<()>;
218
219    /// 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
220    fn add_field_function_get_set<S, R, A, GET, SET>(&mut self, name: S, get: GET, set: SET)
221    where
222        S: Into<String>,
223        R: IntoLua + Typed,
224        A: FromLua + Typed,
225        GET: 'static + MaybeSend + Fn(&Lua, AnyUserData) -> mlua::Result<R>,
226        SET: 'static + MaybeSend + Fn(&Lua, AnyUserData, A) -> mlua::Result<()>;
227
228    /// Typed version of [add_meta_field](mlua::UserDataFields::add_meta_field)
229    fn add_meta_field<V>(&mut self, meta: impl Into<String>, value: V)
230    where V: IntoLua + Typed + 'static;
231
232    /// Typed version of [add_meta_field](mlua::UserDataFields::add_meta_field_with)
233    fn add_meta_field_with<R, F>(&mut self, meta: impl Into<String>, f: F)
234    where
235        F: 'static + MaybeSend + Fn(&Lua) -> mlua::Result<R>,
236        R: IntoLua + Typed + 'static;
237}