ObjectLike

Trait ObjectLike 

Source
pub trait ObjectLike: Sealed {
    // Required methods
    fn get<V: FromLua>(&self, key: impl IntoLua) -> Result<V>;
    fn set(&self, key: impl IntoLua, value: impl IntoLua) -> Result<()>;
    fn call<R>(&self, args: impl IntoLuaMulti) -> Result<R>
       where R: FromLuaMulti;
    fn call_async<R>(&self, args: impl IntoLuaMulti) -> AsyncCallFuture<R>
       where R: FromLuaMulti;
    fn call_method<R>(&self, name: &str, args: impl IntoLuaMulti) -> Result<R>
       where R: FromLuaMulti;
    fn call_async_method<R>(
        &self,
        name: &str,
        args: impl IntoLuaMulti,
    ) -> AsyncCallFuture<R>
       where R: FromLuaMulti;
    fn call_function<R>(&self, name: &str, args: impl IntoLuaMulti) -> Result<R>
       where R: FromLuaMulti;
    fn call_async_function<R>(
        &self,
        name: &str,
        args: impl IntoLuaMulti,
    ) -> AsyncCallFuture<R>
       where R: FromLuaMulti;
    fn to_string(&self) -> Result<StdString>;
    fn to_value(&self) -> Value;

    // Provided method
    fn get_path<V: FromLua>(&self, path: &str) -> Result<V> { ... }
}
Expand description

A trait for types that can be used as Lua objects (usually table and userdata).

Required Methods§

Source

fn get<V: FromLua>(&self, key: impl IntoLua) -> Result<V>

Gets the value associated to key from the object, assuming it has __index metamethod.

Source

fn set(&self, key: impl IntoLua, value: impl IntoLua) -> Result<()>

Sets the value associated to key in the object, assuming it has __newindex metamethod.

Source

fn call<R>(&self, args: impl IntoLuaMulti) -> Result<R>
where R: FromLuaMulti,

Calls the object as a function assuming it has __call metamethod.

The metamethod is called with the object as its first argument, followed by the passed arguments.

Source

fn call_async<R>(&self, args: impl IntoLuaMulti) -> AsyncCallFuture<R>
where R: FromLuaMulti,

Available on crate feature async only.

Asynchronously calls the object as a function assuming it has __call metamethod.

The metamethod is called with the object as its first argument, followed by the passed arguments.

Source

fn call_method<R>(&self, name: &str, args: impl IntoLuaMulti) -> Result<R>
where R: FromLuaMulti,

Gets the function associated to key name from the object and calls it, passing the object itself along with args as function arguments.

Source

fn call_async_method<R>( &self, name: &str, args: impl IntoLuaMulti, ) -> AsyncCallFuture<R>
where R: FromLuaMulti,

Available on crate feature async only.

Gets the function associated to key name from the object and asynchronously calls it, passing the object itself along with args as function arguments.

This might invoke the __index metamethod.

Source

fn call_function<R>(&self, name: &str, args: impl IntoLuaMulti) -> Result<R>
where R: FromLuaMulti,

Gets the function associated to key name from the object and calls it, passing args as function arguments.

This might invoke the __index metamethod.

Source

fn call_async_function<R>( &self, name: &str, args: impl IntoLuaMulti, ) -> AsyncCallFuture<R>
where R: FromLuaMulti,

Available on crate feature async only.

Gets the function associated to key name from the object and asynchronously calls it, passing args as function arguments.

This might invoke the __index metamethod.

Source

fn to_string(&self) -> Result<StdString>

Converts the object to a string in a human-readable format.

This might invoke the __tostring metamethod.

Source

fn to_value(&self) -> Value

Converts the object to a Lua value.

Provided Methods§

Source

fn get_path<V: FromLua>(&self, path: &str) -> Result<V>

Look up a value by a path of keys.

The syntax is similar to accessing nested tables in Lua, with additional support for ? operator to perform safe navigation.

For example, the path a[1].c is equivalent to table.a[1].c in Lua. With ? operator, a[1]?.c is equivalent to table.a[1] and table.a[1].c or nil in Lua.

Bracket notation rules:

  • [123] - integer keys
  • ["string key"] or ['string key'] - string keys (must be quoted)
  • String keys support escape sequences: \", \', \\

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§