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 function::*;
14
15#[repr(i32)]
19#[derive(Debug, Clone, Copy)]
20pub enum LuaType {
21 None = ffi::LUA_TNONE,
23 Nil = ffi::LUA_TNIL,
24 Boolean = ffi::LUA_TBOOLEAN,
25 LightUserdata = ffi::LUA_TLIGHTUSERDATA,
26 Number = ffi::LUA_TNUMBER,
27 String = ffi::LUA_TSTRING,
28 Table = ffi::LUA_TTABLE,
29 Function = ffi::LUA_TFUNCTION,
30 Userdata = ffi::LUA_TUSERDATA,
31 Thread = ffi::LUA_TTHREAD,
32}
33
34impl Display for LuaType {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 f.write_str(unsafe { str::from_utf8_unchecked(self.name().to_bytes()) })
38 }
39}
40
41impl LuaType {
42 #[inline]
44 #[must_use]
45 pub fn name(self) -> &'static CStr {
46 unsafe { CStr::from_ptr(ffi::lua_typename(ptr::null_mut(), self as i32)) }
48 }
49}
50
51impl LuaState {
55 #[inline]
59 #[must_use]
60 pub fn type_of(&self, index: i32) -> LuaType {
61 unsafe { mem::transmute(ffi::lua_type(self.0, index)) }
63 }
64
65 pub fn check_type(&self, arg_n: i32, r#type: LuaType) {
73 unsafe { ffi::luaL_checktype(self.0, arg_n, r#type as i32); }
75 }
76}