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