mlua/userdata.rs
1//! Lua userdata handling.
2//!
3//! This module provides types for creating and working with Lua userdata from Rust.
4
5use std::any::TypeId;
6use std::ffi::CStr;
7use std::fmt;
8use std::hash::Hash;
9use std::os::raw::{c_char, c_void};
10
11use crate::Either;
12use crate::error::{Error, Result};
13use crate::function::Function;
14use crate::state::Lua;
15use crate::string::LuaString;
16use crate::table::{Table, TablePairs};
17use crate::traits::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti};
18use crate::types::{MaybeSend, MaybeSync, ValueRef};
19use crate::util::{StackGuard, check_stack, get_userdata, push_string, short_type_name, take_userdata};
20use crate::value::Value;
21
22#[cfg(feature = "async")]
23use std::future::Future;
24
25#[cfg(feature = "serde")]
26use {
27 serde::ser::{self, Serialize, Serializer},
28 std::result::Result as StdResult,
29};
30
31// Re-export for convenience
32pub(crate) use cell::UserDataStorage;
33pub use r#ref::{UserDataOwned, UserDataRef, UserDataRefMut};
34pub use registry::UserDataRegistry;
35pub(crate) use registry::{RawUserDataRegistry, UserDataProxy};
36pub(crate) use util::{
37 TypeIdHints, borrow_userdata_scoped, borrow_userdata_scoped_mut, collect_userdata,
38 init_userdata_metatable,
39};
40
41/// Kinds of metamethods that can be overridden.
42///
43/// Currently, this mechanism does not allow overriding the `__gc` metamethod, since there is
44/// generally no need to do so: [`UserData`] implementors can instead just implement `Drop`.
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
46#[non_exhaustive]
47pub enum MetaMethod {
48 /// The `+` operator.
49 Add,
50 /// The `-` operator.
51 Sub,
52 /// The `*` operator.
53 Mul,
54 /// The `/` operator.
55 Div,
56 /// The `%` operator.
57 Mod,
58 /// The `^` operator.
59 Pow,
60 /// The unary minus (`-`) operator.
61 Unm,
62 /// The floor division (//) operator.
63 #[cfg(any(feature = "lua55", feature = "lua54", feature = "lua53", feature = "luau"))]
64 #[cfg_attr(
65 docsrs,
66 doc(cfg(any(feature = "lua55", feature = "lua54", feature = "lua53", feature = "luau")))
67 )]
68 IDiv,
69 /// The bitwise AND (&) operator.
70 #[cfg(any(feature = "lua55", feature = "lua54", feature = "lua53"))]
71 #[cfg_attr(
72 docsrs,
73 doc(cfg(any(feature = "lua55", feature = "lua54", feature = "lua53")))
74 )]
75 BAnd,
76 /// The bitwise OR (|) operator.
77 #[cfg(any(feature = "lua55", feature = "lua54", feature = "lua53"))]
78 #[cfg_attr(
79 docsrs,
80 doc(cfg(any(feature = "lua55", feature = "lua54", feature = "lua53")))
81 )]
82 BOr,
83 /// The bitwise XOR (binary ~) operator.
84 #[cfg(any(feature = "lua55", feature = "lua54", feature = "lua53"))]
85 #[cfg_attr(
86 docsrs,
87 doc(cfg(any(feature = "lua55", feature = "lua54", feature = "lua53")))
88 )]
89 BXor,
90 /// The bitwise NOT (unary ~) operator.
91 #[cfg(any(feature = "lua55", feature = "lua54", feature = "lua53"))]
92 #[cfg_attr(
93 docsrs,
94 doc(cfg(any(feature = "lua55", feature = "lua54", feature = "lua53")))
95 )]
96 BNot,
97 /// The bitwise left shift (<<) operator.
98 #[cfg(any(feature = "lua55", feature = "lua54", feature = "lua53"))]
99 #[cfg_attr(
100 docsrs,
101 doc(cfg(any(feature = "lua55", feature = "lua54", feature = "lua53")))
102 )]
103 Shl,
104 /// The bitwise right shift (>>) operator.
105 #[cfg(any(feature = "lua55", feature = "lua54", feature = "lua53"))]
106 #[cfg_attr(
107 docsrs,
108 doc(cfg(any(feature = "lua55", feature = "lua54", feature = "lua53")))
109 )]
110 Shr,
111 /// The string concatenation operator `..`.
112 Concat,
113 /// The length operator `#`.
114 Len,
115 /// The `==` operator.
116 Eq,
117 /// The `<` operator.
118 Lt,
119 /// The `<=` operator.
120 Le,
121 /// Index access `obj[key]`.
122 Index,
123 /// Index write access `obj[key] = value`.
124 NewIndex,
125 /// The call "operator" `obj(arg1, args2, ...)`.
126 Call,
127 /// The `__tostring` metamethod.
128 ///
129 /// This is not an operator, but will be called by methods such as `tostring` and `print`.
130 ToString,
131 /// The `__todebugstring` metamethod for debug purposes.
132 ///
133 /// This is an mlua-specific metamethod that can be used to provide debug representation for
134 /// userdata.
135 ToDebugString,
136 /// The `__pairs` metamethod.
137 ///
138 /// This is not an operator, but it will be called by the built-in `pairs` function.
139 #[cfg(any(
140 feature = "lua55",
141 feature = "lua54",
142 feature = "lua53",
143 feature = "lua52",
144 feature = "luajit52"
145 ))]
146 #[cfg_attr(
147 docsrs,
148 doc(cfg(any(
149 feature = "lua55",
150 feature = "lua54",
151 feature = "lua53",
152 feature = "lua52",
153 feature = "luajit52"
154 )))
155 )]
156 Pairs,
157 /// The `__ipairs` metamethod.
158 ///
159 /// This is not an operator, but it will be called by the built-in [`ipairs`] function.
160 ///
161 /// [`ipairs`]: https://www.lua.org/manual/5.2/manual.html#pdf-ipairs
162 #[cfg(any(feature = "lua52", feature = "luajit52", doc))]
163 #[cfg_attr(docsrs, doc(cfg(any(feature = "lua52", feature = "luajit52"))))]
164 IPairs,
165 /// The `__iter` metamethod.
166 ///
167 /// Executed before the iteration begins, and should return an iterator function like `next`
168 /// (or a custom one).
169 #[cfg(any(feature = "luau", doc))]
170 #[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
171 Iter,
172 /// The `__close` metamethod.
173 ///
174 /// Executed when a variable, that marked as to-be-closed, goes out of scope.
175 ///
176 /// More information about to-be-closed variables can be found in the Lua 5.4
177 /// [documentation][lua_doc].
178 ///
179 /// [lua_doc]: https://www.lua.org/manual/5.4/manual.html#3.3.8
180 #[cfg(any(feature = "lua55", feature = "lua54"))]
181 #[cfg_attr(docsrs, doc(cfg(any(feature = "lua55", feature = "lua54"))))]
182 Close,
183 /// The `__name`/`__type` metafield.
184 ///
185 /// This is not a function, but it's value can be used by `tostring` and `typeof` built-in
186 /// functions.
187 #[doc(hidden)]
188 Type,
189}
190
191impl PartialEq<MetaMethod> for &str {
192 fn eq(&self, other: &MetaMethod) -> bool {
193 *self == other.name()
194 }
195}
196
197impl PartialEq<MetaMethod> for String {
198 fn eq(&self, other: &MetaMethod) -> bool {
199 self == other.name()
200 }
201}
202
203impl fmt::Display for MetaMethod {
204 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
205 write!(fmt, "{}", self.name())
206 }
207}
208
209impl MetaMethod {
210 /// Returns Lua metamethod name, usually prefixed by two underscores.
211 pub const fn name(self) -> &'static str {
212 match self {
213 MetaMethod::Add => "__add",
214 MetaMethod::Sub => "__sub",
215 MetaMethod::Mul => "__mul",
216 MetaMethod::Div => "__div",
217 MetaMethod::Mod => "__mod",
218 MetaMethod::Pow => "__pow",
219 MetaMethod::Unm => "__unm",
220
221 #[cfg(any(feature = "lua55", feature = "lua54", feature = "lua53", feature = "luau"))]
222 MetaMethod::IDiv => "__idiv",
223 #[cfg(any(feature = "lua55", feature = "lua54", feature = "lua53"))]
224 MetaMethod::BAnd => "__band",
225 #[cfg(any(feature = "lua55", feature = "lua54", feature = "lua53"))]
226 MetaMethod::BOr => "__bor",
227 #[cfg(any(feature = "lua55", feature = "lua54", feature = "lua53"))]
228 MetaMethod::BXor => "__bxor",
229 #[cfg(any(feature = "lua55", feature = "lua54", feature = "lua53"))]
230 MetaMethod::BNot => "__bnot",
231 #[cfg(any(feature = "lua55", feature = "lua54", feature = "lua53"))]
232 MetaMethod::Shl => "__shl",
233 #[cfg(any(feature = "lua55", feature = "lua54", feature = "lua53"))]
234 MetaMethod::Shr => "__shr",
235
236 MetaMethod::Concat => "__concat",
237 MetaMethod::Len => "__len",
238 MetaMethod::Eq => "__eq",
239 MetaMethod::Lt => "__lt",
240 MetaMethod::Le => "__le",
241 MetaMethod::Index => "__index",
242 MetaMethod::NewIndex => "__newindex",
243 MetaMethod::Call => "__call",
244 MetaMethod::ToString => "__tostring",
245 MetaMethod::ToDebugString => "__todebugstring",
246
247 #[cfg(any(
248 feature = "lua55",
249 feature = "lua54",
250 feature = "lua53",
251 feature = "lua52",
252 feature = "luajit52"
253 ))]
254 MetaMethod::Pairs => "__pairs",
255 #[cfg(any(feature = "lua52", feature = "luajit52"))]
256 MetaMethod::IPairs => "__ipairs",
257 #[cfg(feature = "luau")]
258 MetaMethod::Iter => "__iter",
259
260 #[cfg(any(feature = "lua55", feature = "lua54"))]
261 MetaMethod::Close => "__close",
262
263 #[rustfmt::skip]
264 MetaMethod::Type => if cfg!(feature = "luau") { "__type" } else { "__name" },
265 }
266 }
267
268 pub(crate) const fn as_cstr(self) -> &'static CStr {
269 match self {
270 #[rustfmt::skip]
271 MetaMethod::Type => if cfg!(feature = "luau") { c"__type" } else { c"__name" },
272 _ => unreachable!(),
273 }
274 }
275
276 pub(crate) fn validate(name: &str) -> Result<&str> {
277 match name {
278 "__gc" => Err(Error::MetaMethodRestricted(name.to_string())),
279 "__metatable" => Err(Error::MetaMethodRestricted(name.to_string())),
280 _ if name.starts_with("__mlua") => Err(Error::MetaMethodRestricted(name.to_string())),
281 name => Ok(name),
282 }
283 }
284}
285
286impl AsRef<str> for MetaMethod {
287 fn as_ref(&self) -> &str {
288 self.name()
289 }
290}
291
292impl From<MetaMethod> for String {
293 #[inline]
294 fn from(method: MetaMethod) -> Self {
295 method.name().to_owned()
296 }
297}
298
299/// Method registry for [`UserData`] implementors.
300pub trait UserDataMethods<T> {
301 /// Add a regular method which accepts a `&T` as the first parameter.
302 ///
303 /// Regular methods are implemented by overriding the `__index` metamethod and returning the
304 /// accessed method. This allows them to be used with the expected `userdata:method()` syntax.
305 ///
306 /// If `add_meta_method` is used to set the `__index` metamethod, the `__index` metamethod will
307 /// be used as a fall-back if no regular method is found.
308 fn add_method<M, A, R>(&mut self, name: impl Into<String>, method: M)
309 where
310 M: Fn(&Lua, &T, A) -> Result<R> + MaybeSend + 'static,
311 A: FromLuaMulti,
312 R: IntoLuaMulti;
313
314 /// Add a regular method which accepts a `&mut T` as the first parameter.
315 ///
316 /// Refer to [`add_method`] for more information about the implementation.
317 ///
318 /// [`add_method`]: UserDataMethods::add_method
319 fn add_method_mut<M, A, R>(&mut self, name: impl Into<String>, method: M)
320 where
321 M: FnMut(&Lua, &mut T, A) -> Result<R> + MaybeSend + 'static,
322 A: FromLuaMulti,
323 R: IntoLuaMulti;
324
325 /// Add a method which accepts `T` as the first parameter.
326 ///
327 /// The userdata `T` will be moved out of the userdata container. This is useful for
328 /// methods that need to consume the userdata.
329 ///
330 /// The method can be called only once per userdata instance, subsequent calls will result in a
331 /// [`Error::UserDataDestructed`] error.
332 fn add_method_once<M, A, R>(&mut self, name: impl Into<String>, method: M)
333 where
334 T: 'static,
335 M: Fn(&Lua, T, A) -> Result<R> + MaybeSend + 'static,
336 A: FromLuaMulti,
337 R: IntoLuaMulti,
338 {
339 let name = name.into();
340 let method_name = format!("{}.{name}", short_type_name::<T>());
341 self.add_function(name, move |lua, (ud, args): (AnyUserData, A)| {
342 let this = (ud.take()).map_err(|err| Error::bad_self_argument(&method_name, err))?;
343 method(lua, this, args)
344 });
345 }
346
347 /// Add an async method which accepts a `&T` as the first parameter and returns [`Future`].
348 ///
349 /// Refer to [`add_method`] for more information about the implementation.
350 ///
351 /// [`add_method`]: UserDataMethods::add_method
352 #[cfg(feature = "async")]
353 #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
354 fn add_async_method<M, A, MR, R>(&mut self, name: impl Into<String>, method: M)
355 where
356 T: 'static,
357 M: Fn(Lua, UserDataRef<T>, A) -> MR + MaybeSend + 'static,
358 A: FromLuaMulti,
359 MR: Future<Output = Result<R>> + MaybeSend + 'static,
360 R: IntoLuaMulti;
361
362 /// Add an async method which accepts a `&mut T` as the first parameter and returns [`Future`].
363 ///
364 /// Refer to [`add_method`] for more information about the implementation.
365 ///
366 /// [`add_method`]: UserDataMethods::add_method
367 #[cfg(feature = "async")]
368 #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
369 fn add_async_method_mut<M, A, MR, R>(&mut self, name: impl Into<String>, method: M)
370 where
371 T: 'static,
372 M: Fn(Lua, UserDataRefMut<T>, A) -> MR + MaybeSend + 'static,
373 A: FromLuaMulti,
374 MR: Future<Output = Result<R>> + MaybeSend + 'static,
375 R: IntoLuaMulti;
376
377 /// Add an async method which accepts a `T` as the first parameter and returns [`Future`].
378 ///
379 /// The userdata `T` will be moved out of the userdata container. This is useful for
380 /// methods that need to consume the userdata.
381 ///
382 /// The method can be called only once per userdata instance, subsequent calls will result in a
383 /// [`Error::UserDataDestructed`] error.
384 #[cfg(feature = "async")]
385 #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
386 fn add_async_method_once<M, A, MR, R>(&mut self, name: impl Into<String>, method: M)
387 where
388 T: 'static,
389 M: Fn(Lua, T, A) -> MR + MaybeSend + 'static,
390 A: FromLuaMulti,
391 MR: Future<Output = Result<R>> + MaybeSend + 'static,
392 R: IntoLuaMulti,
393 {
394 let name = name.into();
395 let method_name = format!("{}.{name}", short_type_name::<T>());
396 self.add_async_function(name, move |lua, (ud, args): (AnyUserData, A)| {
397 match (ud.take()).map_err(|err| Error::bad_self_argument(&method_name, err)) {
398 Ok(this) => either::Either::Left(method(lua, this, args)),
399 Err(err) => either::Either::Right(async move { Err(err) }),
400 }
401 });
402 }
403
404 /// Add a regular method as a function which accepts generic arguments.
405 ///
406 /// The first argument will be a [`AnyUserData`] of type `T` if the method is called with Lua
407 /// method syntax: `my_userdata:my_method(arg1, arg2)`, or it is passed in as the first
408 /// argument: `my_userdata.my_method(my_userdata, arg1, arg2)`.
409 fn add_function<F, A, R>(&mut self, name: impl Into<String>, function: F)
410 where
411 F: Fn(&Lua, A) -> Result<R> + MaybeSend + 'static,
412 A: FromLuaMulti,
413 R: IntoLuaMulti;
414
415 /// Add a regular method as a mutable function which accepts generic arguments.
416 ///
417 /// This is a version of [`add_function`] that accepts a `FnMut` argument.
418 ///
419 /// [`add_function`]: UserDataMethods::add_function
420 fn add_function_mut<F, A, R>(&mut self, name: impl Into<String>, function: F)
421 where
422 F: FnMut(&Lua, A) -> Result<R> + MaybeSend + 'static,
423 A: FromLuaMulti,
424 R: IntoLuaMulti;
425
426 /// Add a regular method as an async function which accepts generic arguments and returns
427 /// [`Future`].
428 ///
429 /// This is an async version of [`add_function`].
430 ///
431 /// [`add_function`]: UserDataMethods::add_function
432 #[cfg(feature = "async")]
433 #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
434 fn add_async_function<F, A, FR, R>(&mut self, name: impl Into<String>, function: F)
435 where
436 F: Fn(Lua, A) -> FR + MaybeSend + 'static,
437 A: FromLuaMulti,
438 FR: Future<Output = Result<R>> + MaybeSend + 'static,
439 R: IntoLuaMulti;
440
441 /// Add a metamethod which accepts a `&T` as the first parameter.
442 ///
443 /// # Note
444 ///
445 /// This can cause an error with certain binary metamethods that can trigger if only the right
446 /// side has a metatable. To prevent this, use [`add_meta_function`].
447 ///
448 /// [`add_meta_function`]: UserDataMethods::add_meta_function
449 fn add_meta_method<M, A, R>(&mut self, name: impl Into<String>, method: M)
450 where
451 M: Fn(&Lua, &T, A) -> Result<R> + MaybeSend + 'static,
452 A: FromLuaMulti,
453 R: IntoLuaMulti;
454
455 /// Add a metamethod as a function which accepts a `&mut T` as the first parameter.
456 ///
457 /// # Note
458 ///
459 /// This can cause an error with certain binary metamethods that can trigger if only the right
460 /// side has a metatable. To prevent this, use [`add_meta_function`].
461 ///
462 /// [`add_meta_function`]: UserDataMethods::add_meta_function
463 fn add_meta_method_mut<M, A, R>(&mut self, name: impl Into<String>, method: M)
464 where
465 M: FnMut(&Lua, &mut T, A) -> Result<R> + MaybeSend + 'static,
466 A: FromLuaMulti,
467 R: IntoLuaMulti;
468
469 /// Add an async metamethod which accepts a `&T` as the first parameter and returns [`Future`].
470 ///
471 /// This is an async version of [`add_meta_method`].
472 ///
473 /// [`add_meta_method`]: UserDataMethods::add_meta_method
474 #[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))]
475 #[cfg_attr(
476 docsrs,
477 doc(cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau")))))
478 )]
479 fn add_async_meta_method<M, A, MR, R>(&mut self, name: impl Into<String>, method: M)
480 where
481 T: 'static,
482 M: Fn(Lua, UserDataRef<T>, A) -> MR + MaybeSend + 'static,
483 A: FromLuaMulti,
484 MR: Future<Output = Result<R>> + MaybeSend + 'static,
485 R: IntoLuaMulti;
486
487 /// Add an async metamethod which accepts a `&mut T` as the first parameter and returns
488 /// [`Future`].
489 ///
490 /// This is an async version of [`add_meta_method_mut`].
491 ///
492 /// [`add_meta_method_mut`]: UserDataMethods::add_meta_method_mut
493 #[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))]
494 #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
495 fn add_async_meta_method_mut<M, A, MR, R>(&mut self, name: impl Into<String>, method: M)
496 where
497 T: 'static,
498 M: Fn(Lua, UserDataRefMut<T>, A) -> MR + MaybeSend + 'static,
499 A: FromLuaMulti,
500 MR: Future<Output = Result<R>> + MaybeSend + 'static,
501 R: IntoLuaMulti;
502
503 /// Add a metamethod which accepts generic arguments.
504 ///
505 /// Metamethods for binary operators can be triggered if either the left or right argument to
506 /// the binary operator has a metatable, so the first argument here is not necessarily a
507 /// userdata of type `T`.
508 fn add_meta_function<F, A, R>(&mut self, name: impl Into<String>, function: F)
509 where
510 F: Fn(&Lua, A) -> Result<R> + MaybeSend + 'static,
511 A: FromLuaMulti,
512 R: IntoLuaMulti;
513
514 /// Add a metamethod as a mutable function which accepts generic arguments.
515 ///
516 /// This is a version of [`add_meta_function`] that accepts a `FnMut` argument.
517 ///
518 /// [`add_meta_function`]: UserDataMethods::add_meta_function
519 fn add_meta_function_mut<F, A, R>(&mut self, name: impl Into<String>, function: F)
520 where
521 F: FnMut(&Lua, A) -> Result<R> + MaybeSend + 'static,
522 A: FromLuaMulti,
523 R: IntoLuaMulti;
524
525 /// Add a metamethod which accepts generic arguments and returns [`Future`].
526 ///
527 /// This is an async version of [`add_meta_function`].
528 ///
529 /// [`add_meta_function`]: UserDataMethods::add_meta_function
530 #[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))]
531 #[cfg_attr(
532 docsrs,
533 doc(cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau")))))
534 )]
535 fn add_async_meta_function<F, A, FR, R>(&mut self, name: impl Into<String>, function: F)
536 where
537 F: Fn(Lua, A) -> FR + MaybeSend + 'static,
538 A: FromLuaMulti,
539 FR: Future<Output = Result<R>> + MaybeSend + 'static,
540 R: IntoLuaMulti;
541}
542
543/// Field registry for [`UserData`] implementors.
544pub trait UserDataFields<T> {
545 /// Add a static field to the [`UserData`].
546 ///
547 /// Static fields are implemented by updating the `__index` metamethod and returning the
548 /// accessed field. This allows them to be used with the expected `userdata.field` syntax.
549 ///
550 /// Static fields are usually shared between all instances of the [`UserData`] of the same type.
551 ///
552 /// If `add_meta_method` is used to set the `__index` metamethod, it will
553 /// be used as a fall-back if no regular field or method are found.
554 fn add_field<V>(&mut self, name: impl Into<String>, value: V)
555 where
556 V: IntoLua + 'static;
557
558 /// Add a regular field getter as a method which accepts a `&T` as the parameter.
559 ///
560 /// Regular field getters are implemented by overriding the `__index` metamethod and returning
561 /// the accessed field. This allows them to be used with the expected `userdata.field` syntax.
562 ///
563 /// If `add_meta_method` is used to set the `__index` metamethod, the `__index` metamethod will
564 /// be used as a fall-back if no regular field or method are found.
565 fn add_field_method_get<M, R>(&mut self, name: impl Into<String>, method: M)
566 where
567 M: Fn(&Lua, &T) -> Result<R> + MaybeSend + 'static,
568 R: IntoLua;
569
570 /// Add a regular field setter as a method which accepts a `&mut T` as the first parameter.
571 ///
572 /// Regular field setters are implemented by overriding the `__newindex` metamethod and setting
573 /// the accessed field. This allows them to be used with the expected `userdata.field = value`
574 /// syntax.
575 ///
576 /// If `add_meta_method` is used to set the `__newindex` metamethod, the `__newindex` metamethod
577 /// will be used as a fall-back if no regular field is found.
578 fn add_field_method_set<M, A>(&mut self, name: impl Into<String>, method: M)
579 where
580 M: FnMut(&Lua, &mut T, A) -> Result<()> + MaybeSend + 'static,
581 A: FromLua;
582
583 /// Add a regular field getter as a function which accepts a generic [`AnyUserData`] of type `T`
584 /// argument.
585 fn add_field_function_get<F, R>(&mut self, name: impl Into<String>, function: F)
586 where
587 F: Fn(&Lua, AnyUserData) -> Result<R> + MaybeSend + 'static,
588 R: IntoLua;
589
590 /// Add a regular field setter as a function which accepts a generic [`AnyUserData`] of type `T`
591 /// first argument.
592 fn add_field_function_set<F, A>(&mut self, name: impl Into<String>, function: F)
593 where
594 F: FnMut(&Lua, AnyUserData, A) -> Result<()> + MaybeSend + 'static,
595 A: FromLua;
596
597 /// Add a metatable field.
598 ///
599 /// This will initialize the metatable field with `value` on [`UserData`] creation.
600 ///
601 /// # Note
602 ///
603 /// `mlua` will trigger an error on an attempt to define a protected metamethod,
604 /// like `__gc` or `__metatable`.
605 fn add_meta_field<V>(&mut self, name: impl Into<String>, value: V)
606 where
607 V: IntoLua + 'static;
608
609 /// Add a metatable field computed from `f`.
610 ///
611 /// This will initialize the metatable field from `f` on [`UserData`] creation.
612 ///
613 /// # Note
614 ///
615 /// `mlua` will trigger an error on an attempt to define a protected metamethod,
616 /// like `__gc` or `__metatable`.
617 fn add_meta_field_with<F, R>(&mut self, name: impl Into<String>, f: F)
618 where
619 F: FnOnce(&Lua) -> Result<R> + 'static,
620 R: IntoLua;
621}
622
623/// Trait for custom userdata types.
624///
625/// By implementing this trait, a struct becomes eligible for use inside Lua code.
626///
627/// Implementation of [`IntoLua`] is automatically provided, [`FromLua`] needs to be implemented
628/// manually.
629///
630///
631/// # Examples
632///
633/// ```
634/// # use mlua::{Lua, Result, UserData};
635/// # fn main() -> Result<()> {
636/// # let lua = Lua::new();
637/// struct MyUserData;
638///
639/// impl UserData for MyUserData {}
640///
641/// // `MyUserData` now implements `IntoLua`:
642/// lua.globals().set("myobject", MyUserData)?;
643///
644/// lua.load("assert(type(myobject) == 'userdata')").exec()?;
645/// # Ok(())
646/// # }
647/// ```
648///
649/// Custom fields, methods and operators can be provided by implementing `add_fields` or
650/// `add_methods` (refer to [`UserDataFields`] and [`UserDataMethods`] for more information):
651///
652/// ```
653/// # use mlua::{Lua, MetaMethod, Result, UserData, UserDataFields, UserDataMethods};
654/// # fn main() -> Result<()> {
655/// # let lua = Lua::new();
656/// struct MyUserData(i32);
657///
658/// impl UserData for MyUserData {
659/// fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {
660/// fields.add_field_method_get("val", |_, this| Ok(this.0));
661/// }
662///
663/// fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
664/// methods.add_method_mut("add", |_, mut this, value: i32| {
665/// this.0 += value;
666/// Ok(())
667/// });
668///
669/// methods.add_meta_method(MetaMethod::Add, |_, this, value: i32| {
670/// Ok(this.0 + value)
671/// });
672/// }
673/// }
674///
675/// lua.globals().set("myobject", MyUserData(123))?;
676///
677/// lua.load(r#"
678/// assert(myobject.val == 123)
679/// myobject:add(7)
680/// assert(myobject.val == 130)
681/// assert(myobject + 10 == 140)
682/// "#).exec()?;
683/// # Ok(())
684/// # }
685/// ```
686pub trait UserData: Sized {
687 /// Adds custom fields specific to this userdata.
688 #[allow(unused_variables)]
689 fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {}
690
691 /// Adds custom methods and operators specific to this userdata.
692 #[allow(unused_variables)]
693 fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {}
694
695 /// Registers this type for use in Lua.
696 ///
697 /// This method is responsible for calling `add_fields` and `add_methods` on the provided
698 /// [`UserDataRegistry`].
699 fn register(registry: &mut UserDataRegistry<Self>) {
700 Self::add_fields(registry);
701 Self::add_methods(registry);
702 }
703}
704
705/// Handle to an internal Lua userdata for any type that implements [`UserData`].
706///
707/// Similar to [`std::any::Any`], this provides an interface for dynamic type checking via the
708/// [`is`] and [`borrow`] methods.
709///
710/// # Note
711///
712/// This API should only be used when necessary. Implementing [`UserData`] already allows defining
713/// methods which check the type and acquire a borrow behind the scenes.
714///
715/// [`is`]: crate::AnyUserData::is
716/// [`borrow`]: crate::AnyUserData::borrow
717#[derive(Clone, PartialEq)]
718pub struct AnyUserData(pub(crate) ValueRef);
719
720impl AnyUserData {
721 /// Checks whether the type of this userdata is `T`.
722 #[inline]
723 pub fn is<T: 'static>(&self) -> bool {
724 let type_id = self.type_id();
725 // We do not use wrapped types here, rather prefer to check the "real" type of the userdata
726 matches!(type_id, Some(type_id) if type_id == TypeId::of::<T>())
727 }
728
729 /// Checks whether the type of this userdata is a [proxy object] for `T`.
730 ///
731 /// [proxy object]: crate::Lua::create_proxy
732 #[inline]
733 pub fn is_proxy<T: 'static>(&self) -> bool {
734 self.is::<UserDataProxy<T>>()
735 }
736
737 /// Borrow this userdata immutably if it is of type `T`.
738 ///
739 /// # Errors
740 ///
741 /// Returns a [`UserDataBorrowError`] if the userdata is already mutably borrowed.
742 /// Returns a [`DataTypeMismatch`] if the userdata is not of type `T` or if it's
743 /// scoped.
744 ///
745 /// [`UserDataBorrowError`]: crate::Error::UserDataBorrowError
746 /// [`DataTypeMismatch`]: crate::Error::UserDataTypeMismatch
747 #[inline]
748 pub fn borrow<T: 'static>(&self) -> Result<UserDataRef<T>> {
749 let lua = self.0.lua.lock();
750 unsafe { UserDataRef::borrow_from_stack(&lua, lua.ref_thread(), self.0.index) }
751 }
752
753 /// Borrow this userdata immutably if it is of type `T`, passing the borrowed value
754 /// to the closure.
755 ///
756 /// This method is the only way to borrow scoped userdata (created inside [`Lua::scope`]).
757 pub fn borrow_scoped<T: 'static, R>(&self, f: impl FnOnce(&T) -> R) -> Result<R> {
758 let lua = self.0.lua.lock();
759 let type_id = lua.get_userdata_ref_type_id(&self.0)?;
760 let type_hints = TypeIdHints::new::<T>();
761 unsafe { borrow_userdata_scoped(lua.ref_thread(), self.0.index, type_id, type_hints, f) }
762 }
763
764 /// Borrow this userdata mutably if it is of type `T`.
765 ///
766 /// # Errors
767 ///
768 /// Returns a [`UserDataBorrowMutError`] if the userdata cannot be mutably borrowed.
769 /// Returns a [`UserDataTypeMismatch`] if the userdata is not of type `T` or if it's
770 /// scoped.
771 ///
772 /// [`UserDataBorrowMutError`]: crate::Error::UserDataBorrowMutError
773 /// [`UserDataTypeMismatch`]: crate::Error::UserDataTypeMismatch
774 #[inline]
775 pub fn borrow_mut<T: 'static>(&self) -> Result<UserDataRefMut<T>> {
776 let lua = self.0.lua.lock();
777 unsafe { UserDataRefMut::borrow_from_stack(&lua, lua.ref_thread(), self.0.index) }
778 }
779
780 /// Borrow this userdata mutably if it is of type `T`, passing the borrowed value
781 /// to the closure.
782 ///
783 /// This method is the only way to borrow scoped userdata (created inside [`Lua::scope`]).
784 pub fn borrow_mut_scoped<T: 'static, R>(&self, f: impl FnOnce(&mut T) -> R) -> Result<R> {
785 let lua = self.0.lua.lock();
786 let type_id = lua.get_userdata_ref_type_id(&self.0)?;
787 let type_hints = TypeIdHints::new::<T>();
788 unsafe { borrow_userdata_scoped_mut(lua.ref_thread(), self.0.index, type_id, type_hints, f) }
789 }
790
791 /// Takes the value out of this userdata.
792 ///
793 /// Sets the special "destructed" metatable that prevents any further operations with this
794 /// userdata.
795 ///
796 /// Keeps associated user values unchanged (they will be collected by Lua's GC).
797 pub fn take<T: 'static>(&self) -> Result<T> {
798 let lua = self.0.lua.lock();
799 match lua.get_userdata_ref_type_id(&self.0)? {
800 Some(type_id) if type_id == TypeId::of::<T>() => unsafe {
801 let ref_thread = lua.ref_thread();
802 if (*get_userdata::<UserDataStorage<T>>(ref_thread, self.0.index)).has_exclusive_access() {
803 take_userdata::<UserDataStorage<T>>(ref_thread, self.0.index).into_inner()
804 } else {
805 Err(Error::UserDataBorrowMutError)
806 }
807 },
808 _ => Err(Error::UserDataTypeMismatch),
809 }
810 }
811
812 /// Destroys this userdata.
813 ///
814 /// This is similar to [`AnyUserData::take`], but it doesn't require a type.
815 ///
816 /// This method works for non-scoped userdata only.
817 pub fn destroy(&self) -> Result<()> {
818 let lua = self.0.lua.lock();
819 let state = lua.state();
820 unsafe {
821 let _sg = StackGuard::new(state);
822 check_stack(state, 3)?;
823
824 lua.push_userdata_ref(&self.0)?;
825 protect_lua!(state, 1, 1, fn(state) {
826 if ffi::luaL_callmeta(state, -1, cstr!("__gc")) == 0 {
827 ffi::lua_pushboolean(state, 0);
828 }
829 })?;
830 if ffi::lua_isboolean(state, -1) != 0 && ffi::lua_toboolean(state, -1) != 0 {
831 return Ok(());
832 }
833 Err(Error::UserDataBorrowMutError)
834 }
835 }
836
837 /// Sets an associated value to this [`AnyUserData`].
838 ///
839 /// The value may be any Lua value whatsoever, and can be retrieved with [`user_value`].
840 ///
841 /// This is the same as calling [`set_nth_user_value`] with `n` set to 1.
842 ///
843 /// [`user_value`]: AnyUserData::user_value
844 /// [`set_nth_user_value`]: AnyUserData::set_nth_user_value
845 #[inline]
846 pub fn set_user_value(&self, v: impl IntoLua) -> Result<()> {
847 self.set_nth_user_value(1, v)
848 }
849
850 /// Returns an associated value set by [`set_user_value`].
851 ///
852 /// This is the same as calling [`nth_user_value`] with `n` set to 1.
853 ///
854 /// [`set_user_value`]: AnyUserData::set_user_value
855 /// [`nth_user_value`]: AnyUserData::nth_user_value
856 #[inline]
857 pub fn user_value<V: FromLua>(&self) -> Result<V> {
858 self.nth_user_value(1)
859 }
860
861 /// Sets an associated `n`th value to this [`AnyUserData`].
862 ///
863 /// The value may be any Lua value whatsoever, and can be retrieved with [`nth_user_value`].
864 /// `n` starts from 1 and can be up to 65535.
865 ///
866 /// This is supported for all Lua versions using a wrapping table.
867 ///
868 /// [`nth_user_value`]: AnyUserData::nth_user_value
869 pub fn set_nth_user_value(&self, n: usize, v: impl IntoLua) -> Result<()> {
870 if n < 1 || n > u16::MAX as usize {
871 return Err(Error::runtime("user value index out of bounds"));
872 }
873
874 let lua = self.0.lua.lock();
875 let state = lua.state();
876 unsafe {
877 let _sg = StackGuard::new(state);
878 check_stack(state, 5)?;
879
880 lua.push_userdata_ref(&self.0)?;
881 lua.push(v)?;
882
883 // Multiple (extra) user values are emulated by storing them in a table
884 protect_lua!(state, 2, 0, |state| {
885 if ffi::lua_getuservalue(state, -2) != ffi::LUA_TTABLE {
886 // Create a new table to use as uservalue
887 ffi::lua_pop(state, 1);
888 ffi::lua_newtable(state);
889 ffi::lua_pushvalue(state, -1);
890 ffi::lua_setuservalue(state, -4);
891 }
892 ffi::lua_pushvalue(state, -2);
893 ffi::lua_rawseti(state, -2, n as ffi::lua_Integer);
894 })?;
895
896 Ok(())
897 }
898 }
899
900 /// Returns an associated `n`th value set by [`set_nth_user_value`].
901 ///
902 /// `n` starts from 1 and can be up to 65535.
903 ///
904 /// This is supported for all Lua versions using a wrapping table.
905 ///
906 /// [`set_nth_user_value`]: AnyUserData::set_nth_user_value
907 pub fn nth_user_value<V: FromLua>(&self, n: usize) -> Result<V> {
908 if n < 1 || n > u16::MAX as usize {
909 return Err(Error::runtime("user value index out of bounds"));
910 }
911
912 let lua = self.0.lua.lock();
913 let state = lua.state();
914 unsafe {
915 let _sg = StackGuard::new(state);
916 check_stack(state, 4)?;
917
918 lua.push_userdata_ref(&self.0)?;
919
920 // Multiple (extra) user values are emulated by storing them in a table
921 if ffi::lua_getuservalue(state, -1) != ffi::LUA_TTABLE {
922 return V::from_lua(Value::Nil, lua.lua());
923 }
924 ffi::lua_rawgeti(state, -1, n as ffi::lua_Integer);
925
926 V::from_lua(lua.pop_value(), lua.lua())
927 }
928 }
929
930 /// Sets an associated value to this [`AnyUserData`] by name.
931 ///
932 /// The value can be retrieved with [`named_user_value`].
933 ///
934 /// [`named_user_value`]: AnyUserData::named_user_value
935 pub fn set_named_user_value(&self, name: &str, v: impl IntoLua) -> Result<()> {
936 let lua = self.0.lua.lock();
937 let state = lua.state();
938 unsafe {
939 let _sg = StackGuard::new(state);
940 check_stack(state, 5)?;
941
942 lua.push_userdata_ref(&self.0)?;
943 lua.push(v)?;
944
945 // Multiple (extra) user values are emulated by storing them in a table
946 protect_lua!(state, 2, 0, |state| {
947 if ffi::lua_getuservalue(state, -2) != ffi::LUA_TTABLE {
948 // Create a new table to use as uservalue
949 ffi::lua_pop(state, 1);
950 ffi::lua_newtable(state);
951 ffi::lua_pushvalue(state, -1);
952 ffi::lua_setuservalue(state, -4);
953 }
954 ffi::lua_pushlstring(state, name.as_ptr() as *const c_char, name.len());
955 ffi::lua_pushvalue(state, -3);
956 ffi::lua_rawset(state, -3);
957 })?;
958
959 Ok(())
960 }
961 }
962
963 /// Returns an associated value by name set by [`set_named_user_value`].
964 ///
965 /// [`set_named_user_value`]: AnyUserData::set_named_user_value
966 pub fn named_user_value<V: FromLua>(&self, name: &str) -> Result<V> {
967 let lua = self.0.lua.lock();
968 let state = lua.state();
969 unsafe {
970 let _sg = StackGuard::new(state);
971 check_stack(state, 4)?;
972
973 lua.push_userdata_ref(&self.0)?;
974
975 // Multiple (extra) user values are emulated by storing them in a table
976 if ffi::lua_getuservalue(state, -1) != ffi::LUA_TTABLE {
977 return V::from_lua(Value::Nil, lua.lua());
978 }
979 push_string(state, name.as_bytes(), !lua.unlikely_memory_error())?;
980 ffi::lua_rawget(state, -2);
981
982 V::from_stack(-1, &lua)
983 }
984 }
985
986 /// Returns a metatable of this [`AnyUserData`].
987 ///
988 /// Returned [`UserDataMetatable`] object wraps the original metatable and
989 /// provides safe access to its methods.
990 ///
991 /// For `T: 'static` returned metatable is shared among all instances of type `T`.
992 #[inline]
993 pub fn metatable(&self) -> Result<UserDataMetatable> {
994 self.raw_metatable().map(UserDataMetatable)
995 }
996
997 /// Returns a raw metatable of this [`AnyUserData`].
998 fn raw_metatable(&self) -> Result<Table> {
999 let lua = self.0.lua.lock();
1000 let ref_thread = lua.ref_thread();
1001 unsafe {
1002 // Check that userdata is registered and not destructed
1003 // All registered userdata types have a non-empty metatable
1004 let _type_id = lua.get_userdata_ref_type_id(&self.0)?;
1005
1006 ffi::lua_getmetatable(ref_thread, self.0.index);
1007 Ok(Table(lua.pop_ref_thread()))
1008 }
1009 }
1010
1011 /// Converts this userdata to a generic C pointer.
1012 ///
1013 /// There is no way to convert the pointer back to its original value.
1014 ///
1015 /// Typically this function is used only for hashing and debug information.
1016 #[inline]
1017 pub fn to_pointer(&self) -> *const c_void {
1018 self.0.to_pointer()
1019 }
1020
1021 /// Returns [`TypeId`] of this userdata if it is registered and `'static`.
1022 ///
1023 /// This method is not available for scoped userdata.
1024 #[inline]
1025 pub fn type_id(&self) -> Option<TypeId> {
1026 let lua = self.0.lua.lock();
1027 lua.get_userdata_ref_type_id(&self.0).ok().flatten()
1028 }
1029
1030 /// Returns a type name of this userdata (from a metatable field).
1031 ///
1032 /// If no type name is set, returns `userdata`.
1033 pub fn type_name(&self) -> Result<LuaString> {
1034 let lua = self.0.lua.lock();
1035 let state = lua.state();
1036 unsafe {
1037 let _sg = StackGuard::new(state);
1038 check_stack(state, 3)?;
1039
1040 lua.push_userdata_ref(&self.0)?;
1041 let protect = !lua.unlikely_memory_error();
1042 let name_type = if protect {
1043 protect_lua!(state, 1, 1, |state| {
1044 ffi::luaL_getmetafield(state, -1, MetaMethod::Type.as_cstr().as_ptr())
1045 })?
1046 } else {
1047 ffi::luaL_getmetafield(state, -1, MetaMethod::Type.as_cstr().as_ptr())
1048 };
1049 match name_type {
1050 ffi::LUA_TSTRING => Ok(LuaString(lua.pop_ref())),
1051 _ => lua.create_string(b"userdata"),
1052 }
1053 }
1054 }
1055
1056 pub(crate) fn equals(&self, other: &Self) -> Result<bool> {
1057 // Uses lua_rawequal() under the hood
1058 if self == other {
1059 return Ok(true);
1060 }
1061
1062 let mt = self.raw_metatable()?;
1063 if mt != other.raw_metatable()? {
1064 return Ok(false);
1065 }
1066
1067 if mt.contains_key("__eq")? {
1068 return mt.get::<Function>("__eq")?.call((self, other));
1069 }
1070
1071 Ok(false)
1072 }
1073
1074 /// Returns `true` if this [`AnyUserData`] is serializable (e.g. was created using
1075 /// [`Lua::create_ser_userdata`]).
1076 #[cfg(feature = "serde")]
1077 pub(crate) fn is_serializable(&self) -> bool {
1078 let lua = self.0.lua.lock();
1079 let is_serializable = || unsafe {
1080 // Userdata must be registered and not destructed
1081 let _ = lua.get_userdata_ref_type_id(&self.0)?;
1082 let ud = &*get_userdata::<UserDataStorage<()>>(lua.ref_thread(), self.0.index);
1083 Ok::<_, Error>((*ud).is_serializable())
1084 };
1085 is_serializable().unwrap_or(false)
1086 }
1087
1088 unsafe fn invoke_tostring_dbg(&self) -> Result<Option<String>> {
1089 let lua = self.0.lua.lock();
1090 let state = lua.state();
1091 let _guard = StackGuard::new(state);
1092 check_stack(state, 3)?;
1093
1094 lua.push_ref(&self.0);
1095 protect_lua!(state, 1, 1, fn(state) {
1096 // Try `__todebugstring` metamethod first, then `__tostring`
1097 #[allow(clippy::collapsible_if)]
1098 if ffi::luaL_callmeta(state, -1, cstr!("__todebugstring")) == 0 {
1099 if ffi::luaL_callmeta(state, -1, cstr!("__tostring")) == 0 {
1100 ffi::lua_pushnil(state);
1101 }
1102 }
1103 })?;
1104 Ok(lua.pop_value().as_string().map(|s| s.to_string_lossy()))
1105 }
1106
1107 pub(crate) fn fmt_pretty(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1108 // Try converting to a (debug) string first, with fallback to `__name/__type`
1109 match unsafe { self.invoke_tostring_dbg() } {
1110 Ok(Some(s)) => write!(fmt, "{s}"),
1111 _ => {
1112 let name = self.type_name().ok();
1113 let name = (name.as_ref())
1114 .map(|s| Either::Left(s.display()))
1115 .unwrap_or(Either::Right("userdata"));
1116 write!(fmt, "{name}: {:?}", self.to_pointer())
1117 }
1118 }
1119 }
1120}
1121
1122impl fmt::Debug for AnyUserData {
1123 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1124 if fmt.alternate() {
1125 return self.fmt_pretty(fmt);
1126 }
1127 fmt.debug_tuple("AnyUserData").field(&self.0).finish()
1128 }
1129}
1130
1131/// Handle to a [`AnyUserData`] metatable.
1132#[derive(Clone, Debug)]
1133pub struct UserDataMetatable(pub(crate) Table);
1134
1135impl UserDataMetatable {
1136 /// Gets the value associated to `key` from the metatable.
1137 ///
1138 /// If no value is associated to `key`, returns the `Nil` value.
1139 /// Access to restricted metamethods such as `__gc` or `__metatable` will cause an error.
1140 pub fn get<V: FromLua>(&self, key: impl AsRef<str>) -> Result<V> {
1141 self.0.raw_get(MetaMethod::validate(key.as_ref())?)
1142 }
1143
1144 /// Sets a key-value pair in the metatable.
1145 ///
1146 /// If the value is `Nil`, this will effectively remove the `key`.
1147 /// Access to restricted metamethods such as `__gc` or `__metatable` will cause an error.
1148 /// Setting `__index` or `__newindex` metamethods is also restricted because their values are
1149 /// cached for `mlua` internal usage.
1150 pub fn set(&self, key: impl AsRef<str>, value: impl IntoLua) -> Result<()> {
1151 let key = MetaMethod::validate(key.as_ref())?;
1152 // `__index` and `__newindex` cannot be changed in runtime, because values are cached
1153 if key == MetaMethod::Index || key == MetaMethod::NewIndex {
1154 return Err(Error::MetaMethodRestricted(key.to_string()));
1155 }
1156 self.0.raw_set(key, value)
1157 }
1158
1159 /// Checks whether the metatable contains a non-nil value for `key`.
1160 pub fn contains(&self, key: impl AsRef<str>) -> Result<bool> {
1161 self.0.contains_key(MetaMethod::validate(key.as_ref())?)
1162 }
1163
1164 /// Returns an iterator over the pairs of the metatable.
1165 ///
1166 /// The pairs are wrapped in a [`Result`], since they are lazily converted to `V` type.
1167 ///
1168 /// [`Result`]: crate::Result
1169 pub fn pairs<V: FromLua>(&self) -> UserDataMetatablePairs<'_, V> {
1170 UserDataMetatablePairs(self.0.pairs())
1171 }
1172}
1173
1174/// An iterator over the pairs of a [`AnyUserData`] metatable.
1175///
1176/// It skips restricted metamethods, such as `__gc` or `__metatable`.
1177///
1178/// This struct is created by the [`UserDataMetatable::pairs`] method.
1179pub struct UserDataMetatablePairs<'a, V>(TablePairs<'a, String, V>);
1180
1181impl<V> Iterator for UserDataMetatablePairs<'_, V>
1182where
1183 V: FromLua,
1184{
1185 type Item = Result<(String, V)>;
1186
1187 fn next(&mut self) -> Option<Self::Item> {
1188 loop {
1189 match self.0.next()? {
1190 Ok((key, value)) => {
1191 // Skip restricted metamethods
1192 if MetaMethod::validate(&key).is_ok() {
1193 break Some(Ok((key, value)));
1194 }
1195 }
1196 Err(e) => break Some(Err(e)),
1197 }
1198 }
1199 }
1200}
1201
1202#[cfg(feature = "serde")]
1203impl Serialize for AnyUserData {
1204 fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error>
1205 where
1206 S: Serializer,
1207 {
1208 let lua = self.0.lua.lock();
1209 unsafe {
1210 let _ = lua
1211 .get_userdata_ref_type_id(&self.0)
1212 .map_err(ser::Error::custom)?;
1213 let ud = &*get_userdata::<UserDataStorage<()>>(lua.ref_thread(), self.0.index);
1214 ud.serialize(serializer)
1215 }
1216 }
1217}
1218
1219struct WrappedUserdata<F: FnOnce(&Lua) -> Result<AnyUserData>>(F);
1220
1221impl AnyUserData {
1222 /// Wraps any Rust type, returning an opaque type that implements [`IntoLua`] trait.
1223 ///
1224 /// This function uses [`Lua::create_any_userdata`] under the hood.
1225 pub fn wrap<T: MaybeSend + MaybeSync + 'static>(data: T) -> impl IntoLua {
1226 WrappedUserdata(move |lua| lua.create_any_userdata(data))
1227 }
1228
1229 /// Wraps any Rust type that implements [`Serialize`], returning an opaque type that implements
1230 /// [`IntoLua`] trait.
1231 ///
1232 /// This function uses [`Lua::create_ser_any_userdata`] under the hood.
1233 #[cfg(feature = "serde")]
1234 #[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
1235 pub fn wrap_ser<T: Serialize + MaybeSend + MaybeSync + 'static>(data: T) -> impl IntoLua {
1236 WrappedUserdata(move |lua| lua.create_ser_any_userdata(data))
1237 }
1238}
1239
1240impl<F> IntoLua for WrappedUserdata<F>
1241where
1242 F: for<'l> FnOnce(&'l Lua) -> Result<AnyUserData>,
1243{
1244 fn into_lua(self, lua: &Lua) -> Result<Value> {
1245 (self.0)(lua).map(Value::UserData)
1246 }
1247}
1248
1249mod cell;
1250mod lock;
1251mod object;
1252mod r#ref;
1253mod registry;
1254mod util;
1255
1256#[cfg(test)]
1257mod assertions {
1258 use super::*;
1259
1260 #[cfg(not(feature = "send"))]
1261 static_assertions::assert_not_impl_any!(AnyUserData: Send);
1262 #[cfg(feature = "send")]
1263 static_assertions::assert_impl_all!(AnyUserData: Send, Sync);
1264}