low_luajit/lua/type/
mod.rs1use core::{ffi::CStr, fmt::{self, Display}, mem, ptr};
2use crate::{LuaState, ffi};
3
4mod nil;
5mod boolean;
6mod number;
7mod string;
8mod table;
9mod function;
10mod userdata;
11mod thread;
12
13pub use table::*;
14pub use function::*;
15
16#[repr(i32)]
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum LuaType {
22 None = ffi::LUA_TNONE,
24 Nil = ffi::LUA_TNIL,
25 Boolean = ffi::LUA_TBOOLEAN,
26 LightUserdata = ffi::LUA_TLIGHTUSERDATA,
27 Number = ffi::LUA_TNUMBER,
28 String = ffi::LUA_TSTRING,
29 Table = ffi::LUA_TTABLE,
30 Function = ffi::LUA_TFUNCTION,
31 Userdata = ffi::LUA_TUSERDATA,
32 Thread = ffi::LUA_TTHREAD,
33}
34
35impl Display for LuaType {
36 #[inline]
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 f.write_str(unsafe { str::from_utf8_unchecked(self.name().to_bytes()) })
40 }
41}
42
43impl LuaType {
44 #[inline]
48 #[must_use]
49 pub fn name(self) -> &'static CStr {
50 unsafe { CStr::from_ptr(ffi::lua_typename(ptr::null_mut(), self as i32)) }
52 }
53}
54
55impl LuaState {
59 #[inline]
63 #[must_use]
64 pub fn type_of(&self, index: i32) -> LuaType {
65 unsafe { mem::transmute(ffi::lua_type(self.as_ptr(), index)) }
67 }
68
69 #[inline]
77 pub fn check_type(&self, arg_n: i32, r#type: LuaType) {
78 unsafe { ffi::luaL_checktype(self.as_ptr(), arg_n, r#type as i32); }
80 }
81}