Struct ModuleBuilder

Source
pub struct ModuleBuilder<'a> { /* private fields */ }
Available on crate feature 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>

Source

pub fn new(lua: &'a Lua) -> Self

Creates a new module builder

Source

pub fn fields(&'a self) -> impl Iterator<Item = (&String, &LuaValue<'_>)>

Produces an iterator over the fields in the builder

Source

pub fn fields_mut( &'a mut self, ) -> impl Iterator<Item = (&String, &mut LuaValue<'_>)>

Produces a mutable iterator over the fields in the builder

Source

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

Source

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>>,

Available on crate feature async only.

Adds an async function to the module

Source

pub fn add_c_fn( &mut self, name: &str, func: lua_CFunction, ) -> LuaResult<&mut Self>

Adds a C function to the module

Source

pub fn add_table( &mut self, name: &str, table: LuaTable<'a>, ) -> LuaResult<&mut Self>

Adds a table to the module

Source

pub fn add_table_from<K, V, I>( &mut self, name: &str, iterator: I, ) -> LuaResult<&mut Self>
where K: ToLua<'a>, V: ToLua<'a>, I: IntoIterator<Item = (K, V)>,

Adds a table to the module from an iterator

Source

pub fn add_table_empty(&mut self, name: &str) -> LuaResult<&mut Self>

Adds an empty table to the module

Source

pub fn add_value( &mut self, name: &str, value: impl ToLua<'a>, ) -> LuaResult<&mut Self>

Adds a lua value (any) to the module

Source

pub fn add_string(&mut self, name: &str, value: &str) -> LuaResult<&mut Self>

Adds a string to the module

Source

pub fn add_int(&mut self, name: &str, value: i64) -> LuaResult<&mut Self>

Adds an integer to the module

Source

pub fn add_float(&mut self, name: &str, value: f64) -> LuaResult<&mut Self>

Adds a number to the module

Source

pub fn add_bool(&mut self, name: &str, value: bool) -> LuaResult<&mut Self>

Adds a boolean to the module

Source

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

Source

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>>,

Available on crate feature async only.

Adds an async function to the module, consuming and returning the builder

Source

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

Source

pub fn with_table(self, name: &str, table: LuaTable<'a>) -> LuaResult<Self>

Adds a table to the module, consuming and returning the builder

Source

pub fn with_table_from<K, V, I>( self, name: &str, iterator: I, ) -> LuaResult<Self>
where K: ToLua<'a>, V: ToLua<'a>, I: IntoIterator<Item = (K, V)>,

Adds a table to the module from an iterator, consuming and returning the builder

Source

pub fn with_table_empty(self, name: &str) -> LuaResult<Self>

Adds an empty table to the module, consuming and returning the builder

Source

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

Source

pub fn with_string(self, name: &str, value: &str) -> LuaResult<Self>

Adds a string to the module, consuming and returning the builder

Source

pub fn with_int(self, name: &str, value: i64) -> LuaResult<Self>

Adds an integer to the module, consuming and returning the builder

Source

pub fn with_float(self, name: &str, value: f64) -> LuaResult<Self>

Adds a number to the module, consuming and returning the builder

Source

pub fn with_bool(&mut self, name: &str, value: bool) -> LuaResult<&mut Self>

Adds a boolean to the module, consuming and returning the builder

Source

pub fn build(self) -> LuaResult<LuaTable<'a>>

Consumes the builder and returns the module as a table

Trait Implementations§

Source§

impl<'a> Debug for ModuleBuilder<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> ToLua<'a> for ModuleBuilder<'a>

Source§

fn to_lua(self, lua: &'a Lua) -> LuaResult<LuaValue<'a>>

Performs the conversion.

Auto Trait Implementations§

§

impl<'a> Freeze for ModuleBuilder<'a>

§

impl<'a> !RefUnwindSafe for ModuleBuilder<'a>

§

impl<'a> !Send for ModuleBuilder<'a>

§

impl<'a> !Sync for ModuleBuilder<'a>

§

impl<'a> Unpin for ModuleBuilder<'a>

§

impl<'a> !UnwindSafe for ModuleBuilder<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<'lua, T> ToLuaMulti<'lua> for T
where T: ToLua<'lua>,

Source§

fn to_lua_multi(self, lua: &'lua Lua) -> Result<MultiValue<'lua>, Error>

Performs the conversion.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.