pub struct UpVal { /* private fields */ }Expand description
A closure upvalue. Open upvalues point at a slot on a thread’s stack (referred to by index, since the stack reallocates). Closed upvalues own the value.
State lives entirely in three Cell fields and is single-source-of-truth.
open_thread_id doubles as the open/closed discriminant: a non-negative
value is the owning thread id of an open upvalue; the [CLOSED_TAG]
sentinel (-1) means the upvalue is closed and its payload is in
closed_value. Valid thread ids are non-negative (the main thread is id 0),
so the sentinel is unambiguous. open_idx is the stack slot of an open
upvalue. Closing is terminal — there is no re-open path — so a CLOSED_TAG
tag never reverts.
Read the open shape with try_open_payload
(None once closed) and the closed payload with
closed_value / try_closed_value.
The all-Cell layout lets state.rs::upvalue_get / upvalue_set
short-circuit the Open path with zero borrow-guard overhead, which is the
dominant cost in fibonacci-class recursion benchmarks.
Implementations§
Source§impl UpVal
impl UpVal
pub fn open(thread_id: usize, idx: StackIdx) -> Self
pub fn closed(v: LuaValue) -> Self
pub fn is_open(&self) -> bool
pub fn is_closed(&self) -> bool
Sourcepub fn try_open_payload(&self) -> Option<(usize, StackIdx)>
pub fn try_open_payload(&self) -> Option<(usize, StackIdx)>
Zero-overhead read of the open shape used by upvalue_get /
upvalue_set and every out-of-crate consumer that inspects an open
upvalue’s (thread_id, idx). Returns Some((thread_id, idx)) when the
upvalue is still open, None once it has been closed.
Sourcepub fn closed_value(&self) -> LuaValue
pub fn closed_value(&self) -> LuaValue
Returns the closed-side value. Callers must have confirmed the
upvalue is closed (try_open_payload returned None).
pub fn close_with(&self, v: LuaValue)
pub fn set_closed_value(&self, v: LuaValue)
pub fn try_closed_value(&self) -> Option<LuaValue>
Trait Implementations§
Source§impl Trace for UpVal
UpVal — Open (refers to a thread stack slot by index) or Closed (owns a
LuaValue). The Open variant carries no direct GC reference; the slot it
points at is traced through the owning thread’s stack walk.
impl Trace for UpVal
UpVal — Open (refers to a thread stack slot by index) or Closed (owns a LuaValue). The Open variant carries no direct GC reference; the slot it points at is traced through the owning thread’s stack walk.
Source§fn type_name(&self) -> &'static str
fn type_name(&self) -> &'static str
Heap::type_name_count). Collector behavior must not branch on
this. The default covers container blanket impls, which are never
GC-boxed directly; concrete runtime types override it with
std::any::type_name::<Self>().