Struct nvim_utils::builder::ModuleBuilder
source · 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>where
F: 'static + Send + Fn(&'a Lua, A) -> LuaResult<R>,
A: FromLuaMulti<'a>,
R: ToLuaMulti<'a>,
pub fn add_fn<A, R, F>(&mut self, name: &str, func: F) -> LuaResult<&mut Self>where F: 'static + Send + Fn(&'a Lua, A) -> LuaResult<R>, A: FromLuaMulti<'a>, R: ToLuaMulti<'a>,
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_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: LuaValue<'a>
) -> LuaResult<&mut Self>
pub fn add_value( &mut self, name: &str, value: LuaValue<'a> ) -> LuaResult<&mut Self>
Adds a lua value (any) to the module
sourcepub fn with_fn<A, R, F>(self, name: &str, func: F) -> LuaResult<Self>where
F: 'static + Send + Fn(&'a Lua, A) -> LuaResult<R>,
A: FromLuaMulti<'a>,
R: ToLuaMulti<'a>,
pub fn with_fn<A, R, F>(self, name: &str, func: F) -> LuaResult<Self>where F: 'static + Send + Fn(&'a Lua, A) -> LuaResult<R>, A: FromLuaMulti<'a>, R: ToLuaMulti<'a>,
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_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: LuaValue<'a>) -> LuaResult<Self>
pub fn with_value(self, name: &str, value: LuaValue<'a>) -> LuaResult<Self>
Adds a lua value (any) to the module, consuming and returning the builder