Skip to main content

lua_types/
lib.rs

1//! Lua value types, error types, and shared newtypes.
2//!
3//! See `PORT_STRATEGY.md` §3 for the design decisions encoded here.
4
5pub mod arith;
6pub mod closure;
7pub mod error;
8pub mod filehandle;
9pub mod gc;
10pub mod opcode;
11pub mod proto;
12pub mod status;
13pub mod string;
14pub mod table;
15pub mod tagmethod;
16pub mod trace_impls;
17pub mod upval;
18pub mod userdata;
19pub mod value;
20pub mod version;
21
22// ── Top-level re-exports (most consumers use these flat names) ──────────
23pub use closure::{LuaClosure, LuaLClosure};
24pub use error::{LuaError, LuaExit, LuaThreadClose};
25pub use filehandle::LuaFileHandle;
26pub use gc::GcRef;
27pub use proto::{AbsLineInfo, LocalVar, LuaProto, UpvalDesc};
28pub use status::LuaStatus;
29pub use string::LuaString;
30pub use table::LuaTable;
31pub use upval::UpVal;
32pub use userdata::LuaUserData;
33pub use value::{F2Imod, LuaValue};
34pub use version::{Feature, LuaVersion, NumberModel, Unsupported};
35
36// ── Top-level newtypes ──────────────────────────────────────────────────
37
38/// Index into the Lua value stack. **Never a pointer or borrow.** Stack
39/// reallocates; only indices are stable across mutations.
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
41pub struct StackIdx(pub u32);
42
43impl StackIdx {
44    pub const ZERO: Self = StackIdx(0);
45    #[inline(always)]
46    pub fn get(self) -> u32 {
47        self.0
48    }
49    #[inline(always)]
50    pub fn as_usize(self) -> usize {
51        self.0 as usize
52    }
53}
54
55impl std::ops::Add<i32> for StackIdx {
56    type Output = Self;
57    #[inline(always)]
58    fn add(self, rhs: i32) -> Self {
59        StackIdx((self.0 as i32 + rhs) as u32)
60    }
61}
62impl std::ops::Sub<i32> for StackIdx {
63    type Output = Self;
64    #[inline(always)]
65    fn sub(self, rhs: i32) -> Self {
66        StackIdx((self.0 as i32 - rhs) as u32)
67    }
68}
69impl std::ops::Sub<StackIdx> for StackIdx {
70    type Output = i32;
71    #[inline(always)]
72    fn sub(self, rhs: StackIdx) -> i32 {
73        self.0 as i32 - rhs.0 as i32
74    }
75}
76
77/// Index into the call-info stack.
78#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
79pub struct CallInfoIdx(pub u32);
80
81impl CallInfoIdx {
82    pub const ZERO: Self = CallInfoIdx(0);
83    #[inline(always)]
84    pub fn get(self) -> u32 {
85        self.0
86    }
87    #[inline(always)]
88    pub fn as_usize(self) -> usize {
89        self.0 as usize
90    }
91}
92
93/// The base type tag for a Lua value. Matches C-Lua's LUA_T* constants.
94#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
95#[repr(i8)]
96pub enum LuaType {
97    None = -1,
98    Nil = 0,
99    Boolean = 1,
100    LightUserData = 2,
101    Number = 3,
102    String = 4,
103    Table = 5,
104    Function = 6,
105    UserData = 7,
106    Thread = 8,
107}
108
109impl LuaType {
110    pub const NUM_TYPES: usize = 9;
111}