pub enum UdValue {
Nil,
Boolean(bool),
Integer(i64),
Number(f64),
Str(String),
Function(CFunction),
UserdataRef(*const dyn Any),
UserdataOwned(Box<dyn UserDataTrait>),
}Expand description
Intermediate value type for userdata field/method returns.
Since LuaValue requires GC-allocated strings, trait methods return UdValue
which the VM converts to proper LuaValue (interning strings as needed).
Variants§
Nil
Boolean(bool)
Integer(i64)
Number(f64)
Str(String)
A Rust string — will be interned by the VM when converting to LuaValue
Function(CFunction)
A light C function — used for returning methods from get_field
UserdataRef(*const dyn Any)
Borrowed reference to another userdata’s inner value (as operand in
arithmetic/comparison). Valid only during the trait method call.
Use UdValue::as_userdata_ref to safely downcast.
UserdataOwned(Box<dyn UserDataTrait>)
Owned userdata value (as return from arithmetic trait methods). The VM allocates this as a new GC-managed userdata.
Implementations§
Source§impl UdValue
impl UdValue
pub fn is_nil(&self) -> bool
Sourcepub fn as_userdata_ref<T: 'static>(&self) -> Option<&T>
pub fn as_userdata_ref<T: 'static>(&self) -> Option<&T>
Try to downcast a UserdataRef operand to a concrete type.
Returns Some(&T) if the operand is a userdata of type T.
This is the primary way to access the other operand in arithmetic
trait methods when both operands are userdata.
§Safety
The returned reference borrows from the GC-managed userdata on the Lua stack. It is valid for the duration of the trait method call.
§Example (generated by derive macro)
fn lua_add(&self, other: &UdValue) -> Option<UdValue> {
if let Some(o) = other.as_userdata_ref::<Vec2>() {
Some(UdValue::from_userdata(Vec2 { x: self.x + o.x, y: self.y + o.y }))
} else {
None
}
}Sourcepub fn from_userdata<T: UserDataTrait>(value: T) -> Self
pub fn from_userdata<T: UserDataTrait>(value: T) -> Self
Wrap a value that implements UserDataTrait into an owned UdValue.
Use this as the return value from arithmetic trait methods when the
result is a new userdata (e.g., Vec2 + Vec2 → Vec2).