Skip to main content

UserdataMethods

Trait UserdataMethods 

Source
pub trait UserdataMethods<T> {
    // Required methods
    fn add_method<F, A, R>(&mut self, name: &str, f: F)
       where F: Fn(&mut Vm, &T, A) -> Result<R, LuaError> + Copy + 'static,
             A: FromLuaArgs + 'static,
             R: IntoLuaReturn + 'static;
    fn add_method_mut<F, A, R>(&mut self, name: &str, f: F)
       where F: Fn(&mut Vm, &mut T, A) -> Result<R, LuaError> + Copy + 'static,
             A: FromLuaArgs + 'static,
             R: IntoLuaReturn + 'static;
    fn add_function<F, A, R>(&mut self, name: &str, f: F)
       where F: Fn(&mut Vm, A) -> Result<R, LuaError> + Copy + 'static,
             A: FromLuaArgs + 'static,
             R: IntoLuaReturn + 'static;
    fn add_meta_method<F, A, R>(&mut self, meta: MetaMethod, f: F)
       where F: Fn(&mut Vm, &T, A) -> Result<R, LuaError> + Copy + 'static,
             A: FromLuaArgs + 'static,
             R: IntoLuaReturn + 'static;
    fn add_meta_method_mut<F, A, R>(&mut self, meta: MetaMethod, f: F)
       where F: Fn(&mut Vm, &mut T, A) -> Result<R, LuaError> + Copy + 'static,
             A: FromLuaArgs + 'static,
             R: IntoLuaReturn + 'static;
    fn add_field_method_get<F, R>(&mut self, name: &str, f: F)
       where F: Fn(&mut Vm, &T) -> Result<R, LuaError> + Copy + 'static,
             R: IntoLuaReturn + 'static;
    fn add_field_method_set<F, A>(&mut self, name: &str, f: F)
       where F: Fn(&mut Vm, &mut T, A) -> Result<(), LuaError> + Copy + 'static,
             A: FromLuaArgs + 'static;
}
Expand description

Builder passed to LuaUserdata::add_methods. The concrete impl is MetatableBuilder<T> (in this module) — UserdataMethods is a trait only to keep the M: bound usable from generic code.

Required Methods§

Source

fn add_method<F, A, R>(&mut self, name: &str, f: F)
where F: Fn(&mut Vm, &T, A) -> Result<R, LuaError> + Copy + 'static, A: FromLuaArgs + 'static, R: IntoLuaReturn + 'static,

Register a regular method bound to __index[name] on the generated metatable; method lookup u:name(args) resolves through Lua’s normal __index table dispatch.

Source

fn add_method_mut<F, A, R>(&mut self, name: &str, f: F)
where F: Fn(&mut Vm, &mut T, A) -> Result<R, LuaError> + Copy + 'static, A: FromLuaArgs + 'static, R: IntoLuaReturn + 'static,

Mutable variant of add_method. The &mut T borrow is exclusive within the call window; an embedder must not concurrently userdata_borrow_mut the same payload through another path during the method body.

Source

fn add_function<F, A, R>(&mut self, name: &str, f: F)
where F: Fn(&mut Vm, A) -> Result<R, LuaError> + Copy + 'static, A: FromLuaArgs + 'static, R: IntoLuaReturn + 'static,

Register a static-style function (no implicit receiver). Bound directly on the metatable, not under __index, so it is reachable as Vec3.new(...) after vm.set_global("Vec3", mt).

Source

fn add_meta_method<F, A, R>(&mut self, meta: MetaMethod, f: F)
where F: Fn(&mut Vm, &T, A) -> Result<R, LuaError> + Copy + 'static, A: FromLuaArgs + 'static, R: IntoLuaReturn + 'static,

Register a metamethod (__add / __tostring / …). Stored directly on the metatable; the dispatcher’s existing get_mm path resolves it.

Source

fn add_meta_method_mut<F, A, R>(&mut self, meta: MetaMethod, f: F)
where F: Fn(&mut Vm, &mut T, A) -> Result<R, LuaError> + Copy + 'static, A: FromLuaArgs + 'static, R: IntoLuaReturn + 'static,

Mutable variant of add_meta_method.

Source

fn add_field_method_get<F, R>(&mut self, name: &str, f: F)
where F: Fn(&mut Vm, &T) -> Result<R, LuaError> + Copy + 'static, R: IntoLuaReturn + 'static,

Field-getter sugar: equivalent to add_method with no args and a single-value return.

v1.3 (UD1+UD2): true field-style obj.name (no parens) is supported alongside the legacy call-syntax obj:name() shape. When any add_field_method_get is registered, MetatableBuilder emits a native trampoline for __index that dispatches in the order methods → field getters → nil. Methods win on name collision (matches mlua and keeps v1.2 callers source-compatible).

Source

fn add_field_method_set<F, A>(&mut self, name: &str, f: F)
where F: Fn(&mut Vm, &mut T, A) -> Result<(), LuaError> + Copy + 'static, A: FromLuaArgs + 'static,

Field-setter sugar: registers a setter for obj.name = value (v1.3 UD1). When any add_field_method_set is registered, MetatableBuilder installs a __newindex trampoline that dispatches (self, value) to the registered setter. Unknown fields raise a runtime error rather than silently dropping the write (matches code/no-unsolicited-fallback).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§