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
15pub trait TypedUserData: Sized {
17 #[allow(unused_variables)]
19 fn add_documentation<F: TypedDataDocumentation<Self>>(docs: &mut F) {}
20
21 #[allow(unused_variables)]
27 fn add_methods<T: TypedDataMethods<Self>>(methods: &mut T) {}
28
29 #[allow(unused_variables)]
35 fn add_fields<F: TypedDataFields<Self>>(fields: &mut F) {}
36}
37
38pub trait TypedDataDocumentation<T: TypedUserData> {
40 fn add(&mut self, doc: &str) -> &mut Self;
41}
42
43pub trait TypedDataMethods<T> {
45 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 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 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 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 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 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 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 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 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 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 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 fn document(&mut self, doc: impl IntoDocComment) -> &mut Self;
137
138 fn param(&mut self, name: impl std::fmt::Display, doc: impl IntoDocComment) -> &mut Self;
142
143 fn param_as(&mut self, ty: impl Into<Type>, name: impl std::fmt::Display, doc: impl IntoDocComment) -> &mut Self;
148
149 fn ret(&mut self, doc: impl IntoDocComment) -> &mut Self;
153
154 fn ret_as(&mut self, ty: impl Into<Type>, doc: impl IntoDocComment) -> &mut Self;
159
160 fn index<I: Typed>(&mut self, idx: isize, doc: impl IntoDocComment) -> &mut Self;
162
163 fn index_as(&mut self, idx: isize, ty: impl Into<Type>, doc: impl IntoDocComment) -> &mut Self;
165}
166
167pub trait TypedDataFields<T> {
169 fn document(&mut self, doc: impl IntoDocComment) -> &mut Self;
171
172 fn coerce(&mut self, ty: impl Into<Type>) -> &mut Self;
176
177 fn add_field<V>(&mut self, name: impl Into<String>, value: V)
179 where
180 V: IntoLua + Clone + 'static + Typed;
181
182 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 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 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 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 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 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 fn add_meta_field<V>(&mut self, meta: impl Into<String>, value: V)
230 where V: IntoLua + Typed + 'static;
231
232 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}