pub struct ModuleBuilder<'a> { /* private fields */ }
builder
only.Expand description
Wraps some of the boilerplate for building a lua module using mlua
in a nice builder pattern.
Includes functions for operating on a reference to the builder, as well as for consuming the builder.
§Examples
use nvim_utils::prelude::*; // imports mlua::prelude::* and the `vim` module
use nvim_utils::builder::ModuleBuilder;
fn my_plugin(lua: &Lua) -> LuaResult<LuaTable> {
// A method that adds two numbers and pushes the result to a table
let add = |lua: &Lua, (this, a, b): (LuaTable, i32, i32)| -> LuaResult<()> {
let results = this.get::<_, LuaTable>("results")?;
results.push(a + b)?;
Ok(())
};
// Consuming the builder
let module = ModuleBuilder::new(lua)
.with_table_empty("results")?
.with_fn("add", add)?
.build()?;
// Using a mutable reference to the builder
let mut builder = ModuleBuilder::new(lua);
builder.add_table_empty("results")?;
builder.add_fn("add", add)?;
let module = builder.build()?;
// If you need to return a LuaValue instead of a LuaTable, you can use mlua's `to_lua` method instead of `build`
// let value = builder.to_lua(lua)?;
Ok(module)
}
Implementations§
Source§impl<'a> ModuleBuilder<'a>
impl<'a> ModuleBuilder<'a>
Sourcepub fn fields(&'a self) -> impl Iterator<Item = (&String, &LuaValue<'_>)>
pub fn fields(&'a self) -> impl Iterator<Item = (&String, &LuaValue<'_>)>
Produces an iterator over the fields in the builder
Sourcepub fn fields_mut(
&'a mut self,
) -> impl Iterator<Item = (&String, &mut LuaValue<'_>)>
pub fn fields_mut( &'a mut self, ) -> impl Iterator<Item = (&String, &mut LuaValue<'_>)>
Produces a mutable iterator over the fields in the builder
Sourcepub fn add_fn<A, R, F>(&mut self, name: &str, func: F) -> LuaResult<&mut Self>
pub fn add_fn<A, R, F>(&mut self, name: &str, func: F) -> LuaResult<&mut Self>
Adds a function to the module
Sourcepub fn add_fn_async<A, R, F, FR>(
&mut self,
name: &str,
func: F,
) -> LuaResult<&mut Self>where
F: 'static + Send + Fn(&'a Lua, A) -> FR,
A: FromLuaMulti<'a>,
R: ToLuaMulti<'a>,
FR: 'a + Send + Future<Output = LuaResult<R>>,
Available on crate feature async
only.
pub fn add_fn_async<A, R, F, FR>(
&mut self,
name: &str,
func: F,
) -> LuaResult<&mut Self>where
F: 'static + Send + Fn(&'a Lua, A) -> FR,
A: FromLuaMulti<'a>,
R: ToLuaMulti<'a>,
FR: 'a + Send + Future<Output = LuaResult<R>>,
async
only.Adds an async function to the module
Sourcepub fn add_c_fn(
&mut self,
name: &str,
func: lua_CFunction,
) -> LuaResult<&mut Self>
pub fn add_c_fn( &mut self, name: &str, func: lua_CFunction, ) -> LuaResult<&mut Self>
Adds a C function to the module
Sourcepub fn add_table(
&mut self,
name: &str,
table: LuaTable<'a>,
) -> LuaResult<&mut Self>
pub fn add_table( &mut self, name: &str, table: LuaTable<'a>, ) -> LuaResult<&mut Self>
Adds a table to the module
Sourcepub fn add_table_from<K, V, I>(
&mut self,
name: &str,
iterator: I,
) -> LuaResult<&mut Self>
pub fn add_table_from<K, V, I>( &mut self, name: &str, iterator: I, ) -> LuaResult<&mut Self>
Adds a table to the module from an iterator
Sourcepub fn add_table_empty(&mut self, name: &str) -> LuaResult<&mut Self>
pub fn add_table_empty(&mut self, name: &str) -> LuaResult<&mut Self>
Adds an empty table to the module
Sourcepub fn add_value(
&mut self,
name: &str,
value: impl ToLua<'a>,
) -> LuaResult<&mut Self>
pub fn add_value( &mut self, name: &str, value: impl ToLua<'a>, ) -> LuaResult<&mut Self>
Adds a lua value (any) to the module
Sourcepub fn add_string(&mut self, name: &str, value: &str) -> LuaResult<&mut Self>
pub fn add_string(&mut self, name: &str, value: &str) -> LuaResult<&mut Self>
Adds a string to the module
Sourcepub fn add_int(&mut self, name: &str, value: i64) -> LuaResult<&mut Self>
pub fn add_int(&mut self, name: &str, value: i64) -> LuaResult<&mut Self>
Adds an integer to the module
Sourcepub fn add_float(&mut self, name: &str, value: f64) -> LuaResult<&mut Self>
pub fn add_float(&mut self, name: &str, value: f64) -> LuaResult<&mut Self>
Adds a number to the module
Sourcepub fn add_bool(&mut self, name: &str, value: bool) -> LuaResult<&mut Self>
pub fn add_bool(&mut self, name: &str, value: bool) -> LuaResult<&mut Self>
Adds a boolean to the module
Sourcepub fn with_fn<A, R, F>(self, name: &str, func: F) -> LuaResult<Self>
pub fn with_fn<A, R, F>(self, name: &str, func: F) -> LuaResult<Self>
Adds a function to the module, consuming and returning the builder
Sourcepub fn with_fn_async<A, R, F, FR>(self, name: &str, func: F) -> LuaResult<Self>where
F: 'static + Send + Fn(&'a Lua, A) -> FR,
A: FromLuaMulti<'a>,
R: ToLuaMulti<'a>,
FR: 'a + Send + Future<Output = LuaResult<R>>,
Available on crate feature async
only.
pub fn with_fn_async<A, R, F, FR>(self, name: &str, func: F) -> LuaResult<Self>where
F: 'static + Send + Fn(&'a Lua, A) -> FR,
A: FromLuaMulti<'a>,
R: ToLuaMulti<'a>,
FR: 'a + Send + Future<Output = LuaResult<R>>,
async
only.Adds an async function to the module, consuming and returning the builder
Sourcepub fn with_c_fn(self, name: &str, func: lua_CFunction) -> LuaResult<Self>
pub fn with_c_fn(self, name: &str, func: lua_CFunction) -> LuaResult<Self>
Adds a C function to the module, consuming and returning the builder
Sourcepub fn with_table(self, name: &str, table: LuaTable<'a>) -> LuaResult<Self>
pub fn with_table(self, name: &str, table: LuaTable<'a>) -> LuaResult<Self>
Adds a table to the module, consuming and returning the builder
Sourcepub fn with_table_from<K, V, I>(
self,
name: &str,
iterator: I,
) -> LuaResult<Self>
pub fn with_table_from<K, V, I>( self, name: &str, iterator: I, ) -> LuaResult<Self>
Adds a table to the module from an iterator, consuming and returning the builder
Sourcepub fn with_table_empty(self, name: &str) -> LuaResult<Self>
pub fn with_table_empty(self, name: &str) -> LuaResult<Self>
Adds an empty table to the module, consuming and returning the builder
Sourcepub fn with_value(self, name: &str, value: impl ToLua<'a>) -> LuaResult<Self>
pub fn with_value(self, name: &str, value: impl ToLua<'a>) -> LuaResult<Self>
Adds a lua value (any) to the module, consuming and returning the builder
Sourcepub fn with_string(self, name: &str, value: &str) -> LuaResult<Self>
pub fn with_string(self, name: &str, value: &str) -> LuaResult<Self>
Adds a string to the module, consuming and returning the builder
Sourcepub fn with_int(self, name: &str, value: i64) -> LuaResult<Self>
pub fn with_int(self, name: &str, value: i64) -> LuaResult<Self>
Adds an integer to the module, consuming and returning the builder
Sourcepub fn with_float(self, name: &str, value: f64) -> LuaResult<Self>
pub fn with_float(self, name: &str, value: f64) -> LuaResult<Self>
Adds a number to the module, consuming and returning the builder