mlua/
types.rs

1use std::cell::UnsafeCell;
2use std::os::raw::{c_int, c_void};
3
4#[cfg(not(feature = "luau"))]
5use crate::debug::{Debug, HookTriggers};
6use crate::error::Result;
7use crate::state::{ExtraData, Lua, RawLua};
8
9// Re-export mutex wrappers
10pub(crate) use sync::{ArcReentrantMutexGuard, ReentrantMutex, ReentrantMutexGuard, XRc, XWeak};
11
12#[cfg(all(feature = "async", feature = "send"))]
13pub(crate) type BoxFuture<'a, T> = futures_util::future::BoxFuture<'a, T>;
14
15#[cfg(all(feature = "async", not(feature = "send")))]
16pub(crate) type BoxFuture<'a, T> = futures_util::future::LocalBoxFuture<'a, T>;
17
18pub use app_data::{AppData, AppDataRef, AppDataRefMut};
19pub use either::Either;
20pub use registry_key::RegistryKey;
21pub(crate) use value_ref::ValueRef;
22
23/// Type of Lua integer numbers.
24pub type Integer = ffi::lua_Integer;
25/// Type of Lua floating point numbers.
26pub type Number = ffi::lua_Number;
27
28/// A "light" userdata value. Equivalent to an unmanaged raw pointer.
29#[derive(Debug, Copy, Clone, Eq, PartialEq)]
30pub struct LightUserData(pub *mut c_void);
31
32#[cfg(feature = "send")]
33unsafe impl Send for LightUserData {}
34#[cfg(feature = "send")]
35unsafe impl Sync for LightUserData {}
36
37#[cfg(feature = "send")]
38pub(crate) type Callback = Box<dyn Fn(&RawLua, c_int) -> Result<c_int> + Send + 'static>;
39
40#[cfg(not(feature = "send"))]
41pub(crate) type Callback = Box<dyn Fn(&RawLua, c_int) -> Result<c_int> + 'static>;
42
43pub(crate) type ScopedCallback<'s> = Box<dyn Fn(&RawLua, c_int) -> Result<c_int> + 's>;
44
45pub(crate) struct Upvalue<T> {
46    pub(crate) data: T,
47    pub(crate) extra: XRc<UnsafeCell<ExtraData>>,
48}
49
50pub(crate) type CallbackUpvalue = Upvalue<Option<Callback>>;
51
52#[cfg(all(feature = "async", feature = "send"))]
53pub(crate) type AsyncCallback =
54    Box<dyn for<'a> Fn(&'a RawLua, c_int) -> BoxFuture<'a, Result<c_int>> + Send + 'static>;
55
56#[cfg(all(feature = "async", not(feature = "send")))]
57pub(crate) type AsyncCallback =
58    Box<dyn for<'a> Fn(&'a RawLua, c_int) -> BoxFuture<'a, Result<c_int>> + 'static>;
59
60#[cfg(feature = "async")]
61pub(crate) type AsyncCallbackUpvalue = Upvalue<AsyncCallback>;
62
63#[cfg(feature = "async")]
64pub(crate) type AsyncPollUpvalue = Upvalue<Option<BoxFuture<'static, Result<c_int>>>>;
65
66/// Type to set next Lua VM action after executing interrupt or hook function.
67pub enum VmState {
68    Continue,
69    /// Yield the current thread.
70    ///
71    /// Supported by Lua 5.3+ and Luau.
72    Yield,
73}
74
75#[cfg(not(feature = "luau"))]
76pub(crate) enum HookKind {
77    Global,
78    Thread(HookTriggers, HookCallback),
79}
80
81#[cfg(all(feature = "send", not(feature = "luau")))]
82pub(crate) type HookCallback = XRc<dyn Fn(&Lua, &Debug) -> Result<VmState> + Send>;
83
84#[cfg(all(not(feature = "send"), not(feature = "luau")))]
85pub(crate) type HookCallback = XRc<dyn Fn(&Lua, &Debug) -> Result<VmState>>;
86
87#[cfg(all(feature = "send", feature = "luau"))]
88pub(crate) type InterruptCallback = XRc<dyn Fn(&Lua) -> Result<VmState> + Send>;
89
90#[cfg(all(not(feature = "send"), feature = "luau"))]
91pub(crate) type InterruptCallback = XRc<dyn Fn(&Lua) -> Result<VmState>>;
92
93#[cfg(all(feature = "send", feature = "luau"))]
94pub(crate) type ThreadCreationCallback = XRc<dyn Fn(&Lua, crate::Thread) -> Result<()> + Send>;
95
96#[cfg(all(not(feature = "send"), feature = "luau"))]
97pub(crate) type ThreadCreationCallback = XRc<dyn Fn(&Lua, crate::Thread) -> Result<()>>;
98
99#[cfg(all(feature = "send", feature = "luau"))]
100pub(crate) type ThreadCollectionCallback = XRc<dyn Fn(crate::LightUserData) + Send>;
101
102#[cfg(all(not(feature = "send"), feature = "luau"))]
103pub(crate) type ThreadCollectionCallback = XRc<dyn Fn(crate::LightUserData)>;
104
105#[cfg(all(feature = "send", feature = "lua54"))]
106pub(crate) type WarnCallback = XRc<dyn Fn(&Lua, &str, bool) -> Result<()> + Send>;
107
108#[cfg(all(not(feature = "send"), feature = "lua54"))]
109pub(crate) type WarnCallback = XRc<dyn Fn(&Lua, &str, bool) -> Result<()>>;
110
111/// A trait that adds `Send` requirement if `send` feature is enabled.
112#[cfg(feature = "send")]
113pub trait MaybeSend: Send {}
114#[cfg(feature = "send")]
115impl<T: Send> MaybeSend for T {}
116
117#[cfg(not(feature = "send"))]
118pub trait MaybeSend {}
119#[cfg(not(feature = "send"))]
120impl<T> MaybeSend for T {}
121
122pub(crate) struct DestructedUserdata;
123
124pub(crate) trait LuaType {
125    const TYPE_ID: c_int;
126}
127
128impl LuaType for bool {
129    const TYPE_ID: c_int = ffi::LUA_TBOOLEAN;
130}
131
132impl LuaType for Number {
133    const TYPE_ID: c_int = ffi::LUA_TNUMBER;
134}
135
136impl LuaType for LightUserData {
137    const TYPE_ID: c_int = ffi::LUA_TLIGHTUSERDATA;
138}
139
140mod app_data;
141mod registry_key;
142mod sync;
143mod value_ref;
144
145#[cfg(test)]
146mod assertions {
147    use super::*;
148
149    #[cfg(not(feature = "send"))]
150    static_assertions::assert_not_impl_any!(ValueRef: Send);
151    #[cfg(feature = "send")]
152    static_assertions::assert_impl_all!(ValueRef: Send, Sync);
153}