Trait mlua::UserDataFields

source ·
pub trait UserDataFields<'lua, T> {
    // Required methods
    fn add_field<V>(&mut self, name: impl AsRef<str>, value: V)
       where V: IntoLua<'lua> + Clone + 'static;
    fn add_field_method_get<M, R>(&mut self, name: impl AsRef<str>, method: M)
       where M: Fn(&'lua Lua, &T) -> Result<R> + MaybeSend + 'static,
             R: IntoLua<'lua>;
    fn add_field_method_set<M, A>(&mut self, name: impl AsRef<str>, method: M)
       where M: FnMut(&'lua Lua, &mut T, A) -> Result<()> + MaybeSend + 'static,
             A: FromLua<'lua>;
    fn add_field_function_get<F, R>(
        &mut self,
        name: impl AsRef<str>,
        function: F
    )
       where F: Fn(&'lua Lua, AnyUserData<'lua>) -> Result<R> + MaybeSend + 'static,
             R: IntoLua<'lua>;
    fn add_field_function_set<F, A>(
        &mut self,
        name: impl AsRef<str>,
        function: F
    )
       where F: FnMut(&'lua Lua, AnyUserData<'lua>, A) -> Result<()> + MaybeSend + 'static,
             A: FromLua<'lua>;
    fn add_meta_field<V>(&mut self, name: impl AsRef<str>, value: V)
       where V: IntoLua<'lua> + Clone + 'static;
    fn add_meta_field_with<F, R>(&mut self, name: impl AsRef<str>, f: F)
       where F: Fn(&'lua Lua) -> Result<R> + MaybeSend + 'static,
             R: IntoLua<'lua>;
}
Expand description

Field registry for UserData implementors.

Required Methods§

source

fn add_field<V>(&mut self, name: impl AsRef<str>, value: V)
where V: IntoLua<'lua> + Clone + 'static,

Add a static field to the UserData.

Static fields are implemented by updating the __index metamethod and returning the accessed field. This allows them to be used with the expected userdata.field syntax.

Static fields are usually shared between all instances of the UserData of the same type.

If add_meta_method is used to set the __index metamethod, it will be used as a fall-back if no regular field or method are found.

source

fn add_field_method_get<M, R>(&mut self, name: impl AsRef<str>, method: M)
where M: Fn(&'lua Lua, &T) -> Result<R> + MaybeSend + 'static, R: IntoLua<'lua>,

Add a regular field getter as a method which accepts a &T as the parameter.

Regular field getters are implemented by overriding the __index metamethod and returning the accessed field. This allows them to be used with the expected userdata.field syntax.

If add_meta_method is used to set the __index metamethod, the __index metamethod will be used as a fall-back if no regular field or method are found.

source

fn add_field_method_set<M, A>(&mut self, name: impl AsRef<str>, method: M)
where M: FnMut(&'lua Lua, &mut T, A) -> Result<()> + MaybeSend + 'static, A: FromLua<'lua>,

Add a regular field setter as a method which accepts a &mut T as the first parameter.

Regular field setters are implemented by overriding the __newindex metamethod and setting the accessed field. This allows them to be used with the expected userdata.field = value syntax.

If add_meta_method is used to set the __newindex metamethod, the __newindex metamethod will be used as a fall-back if no regular field is found.

source

fn add_field_function_get<F, R>(&mut self, name: impl AsRef<str>, function: F)
where F: Fn(&'lua Lua, AnyUserData<'lua>) -> Result<R> + MaybeSend + 'static, R: IntoLua<'lua>,

Add a regular field getter as a function which accepts a generic AnyUserData of type T argument.

Prefer to use add_field_method_get as it is easier to use.

source

fn add_field_function_set<F, A>(&mut self, name: impl AsRef<str>, function: F)
where F: FnMut(&'lua Lua, AnyUserData<'lua>, A) -> Result<()> + MaybeSend + 'static, A: FromLua<'lua>,

Add a regular field setter as a function which accepts a generic AnyUserData of type T first argument.

Prefer to use add_field_method_set as it is easier to use.

source

fn add_meta_field<V>(&mut self, name: impl AsRef<str>, value: V)
where V: IntoLua<'lua> + Clone + 'static,

Add a metatable field.

This will initialize the metatable field with value on UserData creation.

§Note

mlua will trigger an error on an attempt to define a protected metamethod, like __gc or __metatable.

source

fn add_meta_field_with<F, R>(&mut self, name: impl AsRef<str>, f: F)
where F: Fn(&'lua Lua) -> Result<R> + MaybeSend + 'static, R: IntoLua<'lua>,

Add a metatable field computed from f.

This will initialize the metatable field from f on UserData creation.

§Note

mlua will trigger an error on an attempt to define a protected metamethod, like __gc or __metatable.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<'lua, T: 'static> UserDataFields<'lua, T> for UserDataRegistry<'lua, T>