Skip to main content

UdValue

Enum UdValue 

Source
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

Source

pub fn is_nil(&self) -> bool

Source

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
    }
}
Source

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).

Source§

impl UdValue

Source

pub fn to_bool(&self) -> bool

Extract as bool. Follows Lua truthiness: nil and false are false, everything else is true.

Source

pub fn to_integer(&self) -> Option<i64>

Extract as i64 (with optional float→int coercion).

Source

pub fn to_number(&self) -> Option<f64>

Extract as f64 (with optional int→float coercion).

Source

pub fn to_str(&self) -> Option<&str>

Extract as string reference.

Trait Implementations§

Source§

impl Clone for UdValue

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UdValue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for UdValue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&str> for UdValue

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl<T: Into<UdValue>> From<Option<T>> for UdValue

Source§

fn from(opt: Option<T>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for UdValue

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for UdValue

Source§

fn from(b: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for UdValue

Source§

fn from(n: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for UdValue

Source§

fn from(n: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for UdValue

Source§

fn from(i: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for UdValue

Source§

fn from(i: i64) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> LuaMethodProvider for T

Source§

impl<T> LuaStaticMethodProvider for T

Source§

fn __lua_static_methods() -> &'static [(&'static str, CFunction)]

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToSmolStr for T
where T: Display + ?Sized,

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.