mlua_codemp_patch/
traits.rs

1use std::string::String as StdString;
2
3use crate::error::Result;
4use crate::private::Sealed;
5use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti};
6
7#[cfg(feature = "async")]
8use std::future::Future;
9
10/// A trait for types that can be used as Lua objects (usually table and userdata).
11pub trait ObjectLike: Sealed {
12    /// Gets the value associated to `key` from the object, assuming it has `__index` metamethod.
13    fn get<V: FromLua>(&self, key: impl IntoLua) -> Result<V>;
14
15    /// Sets the value associated to `key` in the object, assuming it has `__newindex` metamethod.
16    fn set(&self, key: impl IntoLua, value: impl IntoLua) -> Result<()>;
17
18    /// Calls the object as a function assuming it has `__call` metamethod.
19    ///
20    /// The metamethod is called with the object as its first argument, followed by the passed
21    /// arguments.
22    fn call<R>(&self, args: impl IntoLuaMulti) -> Result<R>
23    where
24        R: FromLuaMulti;
25
26    /// Asynchronously calls the object as a function assuming it has `__call` metamethod.
27    ///
28    /// The metamethod is called with the object as its first argument, followed by the passed
29    /// arguments.
30    #[cfg(feature = "async")]
31    #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
32    fn call_async<R>(&self, args: impl IntoLuaMulti) -> impl Future<Output = Result<R>>
33    where
34        R: FromLuaMulti;
35
36    /// Gets the function associated to key `name` from the object and calls it,
37    /// passing the object itself along with `args` as function arguments.
38    fn call_method<R>(&self, name: &str, args: impl IntoLuaMulti) -> Result<R>
39    where
40        R: FromLuaMulti;
41
42    /// Gets the function associated to key `name` from the object and asynchronously calls it,
43    /// passing the object itself along with `args` as function arguments.
44    ///
45    /// Requires `feature = "async"`
46    ///
47    /// This might invoke the `__index` metamethod.
48    #[cfg(feature = "async")]
49    #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
50    fn call_async_method<R>(&self, name: &str, args: impl IntoLuaMulti) -> impl Future<Output = Result<R>>
51    where
52        R: FromLuaMulti;
53
54    /// Gets the function associated to key `name` from the object and calls it,
55    /// passing `args` as function arguments.
56    ///
57    /// This might invoke the `__index` metamethod.
58    fn call_function<R>(&self, name: &str, args: impl IntoLuaMulti) -> Result<R>
59    where
60        R: FromLuaMulti;
61
62    /// Gets the function associated to key `name` from the object and asynchronously calls it,
63    /// passing `args` as function arguments.
64    ///
65    /// Requires `feature = "async"`
66    ///
67    /// This might invoke the `__index` metamethod.
68    #[cfg(feature = "async")]
69    #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
70    fn call_async_function<R>(&self, name: &str, args: impl IntoLuaMulti) -> impl Future<Output = Result<R>>
71    where
72        R: FromLuaMulti;
73
74    /// Converts the object to a string in a human-readable format.
75    ///
76    /// This might invoke the `__tostring` metamethod.
77    fn to_string(&self) -> Result<StdString>;
78}