Skip to main content

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(feature = "send")]
100pub(crate) type ThreadEventCallback = XRc<dyn Fn(&Lua, crate::thread::ThreadEvent) -> Result<()> + Send>;
101
102#[cfg(not(feature = "send"))]
103pub(crate) type ThreadEventCallback = XRc<dyn Fn(&Lua, crate::thread::ThreadEvent) -> Result<()>>;
104
105#[cfg(feature = "send")]
106#[cfg(any(feature = "lua55", feature = "lua54"))]
107pub(crate) type WarnCallback = XRc<dyn Fn(&Lua, &str, bool) -> Result<()> + Send>;
108
109#[cfg(not(feature = "send"))]
110#[cfg(any(feature = "lua55", feature = "lua54"))]
111pub(crate) type WarnCallback = XRc<dyn Fn(&Lua, &str, bool) -> Result<()>>;
112
113/// A trait that adds `Send` requirement if `send` feature is enabled.
114#[cfg(feature = "send")]
115pub trait MaybeSend: Send {}
116#[cfg(feature = "send")]
117impl<T: Send> MaybeSend for T {}
118
119/// A trait that adds `Send` requirement if `send` feature is enabled.
120#[cfg(not(feature = "send"))]
121pub trait MaybeSend {}
122#[cfg(not(feature = "send"))]
123impl<T> MaybeSend for T {}
124
125/// A trait that adds `Sync` requirement if `send` feature is enabled.
126#[cfg(feature = "send")]
127pub trait MaybeSync: Sync {}
128#[cfg(feature = "send")]
129impl<T: Sync> MaybeSync for T {}
130
131/// A trait that adds `Sync` requirement if `send` feature is enabled.
132#[cfg(not(feature = "send"))]
133pub trait MaybeSync {}
134#[cfg(not(feature = "send"))]
135impl<T> MaybeSync for T {}
136
137pub(crate) struct DestructedUserdata;
138
139pub(crate) trait LuaType {
140    const TYPE_ID: c_int;
141}
142
143impl LuaType for bool {
144    const TYPE_ID: c_int = ffi::LUA_TBOOLEAN;
145}
146
147impl LuaType for Number {
148    const TYPE_ID: c_int = ffi::LUA_TNUMBER;
149}
150
151impl LuaType for LightUserData {
152    const TYPE_ID: c_int = ffi::LUA_TLIGHTUSERDATA;
153}
154
155mod app_data;
156mod registry_key;
157mod sync;
158mod value_ref;
159
160#[cfg(test)]
161mod assertions {
162    use super::*;
163
164    #[cfg(not(feature = "send"))]
165    static_assertions::assert_not_impl_any!(ValueRef: Send);
166    #[cfg(feature = "send")]
167    static_assertions::assert_impl_all!(ValueRef: Send, Sync);
168}