#[repr(C, u8)]pub enum Value {
Nil,
Bool(bool),
Int(i64),
Float(f64),
Str(Gc<LuaStr>),
Table(Gc<Table>),
Closure(Gc<LuaClosure>),
Native(Gc<NativeClosure>),
Coro(Gc<Coro>),
Userdata(Gc<Userdata>),
LightUserdata(*const ()),
}Expand description
P17-D v2 Direction E (E1) — #[repr(C, u8)] makes the discriminant a
1-byte tag at offset 0, with the variant payload starting at offset 8
(after 7 bytes of alignment padding). The total size stays 16 bytes
(same as the prior plain Rust enum representation), preserving P02’s
arithmetic-fast-path 24% win over NaN-boxing.
The PUC-equivalent layout this gives us means LJ_FR2-style frame
metadata reads (stack[base-2] for closure, stack[base-1] for the
packed frame marker) can use a single 1-byte tag load + payload
branch — see Value::tag_byte and friends. The previous enum
repr left discriminant position unspecified, so byte-level reads of
Value layout would have been unportable.
Variant order MUST stay stable: rustc assigns discriminants
0..11 in declaration order (Nil=0, Bool=1, …, LightUserdata=10),
and Phase 3+ hot paths read those discriminants via tag_byte().
New variants must be appended; reordering changes the wire layout.
Variants§
Nil
Lua nil.
Bool(bool)
Lua boolean.
Int(i64)
Lua integer (5.3+; in 5.1 all numbers arrive as Float).
Float(f64)
Lua float.
Str(Gc<LuaStr>)
Lua string — GC-managed byte string.
Table(Gc<Table>)
Lua table.
Closure(Gc<LuaClosure>)
Lua function backed by a LuaClosure.
Tuple Fields
0: Gc<LuaClosure>Closure handle.
Native(Gc<NativeClosure>)
Lua function backed by a host NativeClosure.
Tuple Fields
0: Gc<NativeClosure>Native closure handle.
Coro(Gc<Coro>)
Lua thread (coroutine).
Userdata(Gc<Userdata>)
Full userdata — GC-managed host-allocated payload with a metatable.
LightUserdata(*const ())
PUC LUA_TLIGHTUSERDATA: an opaque host pointer that participates only
as an identity token (raw equality on pointer bits, no metatable, not
GC-managed). Currently produced exclusively by debug.upvalueid — it
points at the upvalue cell’s Value slot and stays distinct per cell.
Implementations§
Source§impl Value
impl Value
Sourcepub fn type_name(self) -> &'static str
pub fn type_name(self) -> &'static str
Lua-visible type name ("nil", "boolean", "number",
"string", "table", "function", "thread", "userdata")
matching type().
Sourcepub fn is_nil(self) -> bool
pub fn is_nil(self) -> bool
True when this is Value::Nil.
Sourcepub fn tag_byte(&self) -> u8
pub fn tag_byte(&self) -> u8
P17-D v2 Direction E (E1) — read the variant’s discriminant byte
directly. The #[repr(C, u8)] on the enum makes this a single
1-byte load from &self, regardless of variant.
Discriminant values follow declaration order:
Nil=0, Bool=1, Int=2, Float=3, Str=4, Table=5, Closure=6, Native=7, Coro=8, Userdata=9, LightUserdata=10.
Use tag constants instead of literal numbers at call
sites — see the module-level tag constants below.
Sourcepub fn is_callable(self) -> bool
pub fn is_callable(self) -> bool
Fast tag-only check for Lua function-call sites. Returns true
iff the value’s discriminant is Closure or Native (the
callable types). Avoids matching the entire enum.
Sourcepub fn try_as_str(&self) -> Option<&str>
pub fn try_as_str(&self) -> Option<&str>
Borrow the Lua string’s bytes as a UTF-8 &str (B7 — Phase 2).
Returns None if this value is not a Value::Str, or if the
string’s bytes are not valid UTF-8.
Embedders dealing with text data use this. For binary data
(Redis protocol buffers, etc.) use Value::as_bytes.