use std::future::Future; use mlua::{FromLuaMulti, Lua, Result, Table, ToLuaMulti, Value}; pub struct ReadonlyTableBuilder<'lua> { lua: &'lua Lua, tab: Table<'lua>, } impl<'lua> ReadonlyTableBuilder<'lua> { pub fn new(lua: &'lua Lua) -> Result { let tab = lua.create_table()?; Ok(Self { lua, tab }) } pub fn with_value(self, key: &'static str, value: Value) -> Result { self.tab.raw_set(key, value)?; Ok(self) } pub fn with_table(self, key: &'static str, table: Table) -> Result { self.with_value(key, Value::Table(table)) } pub fn with_function(self, key: &'static str, func: F) -> Result where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + Fn(&'lua Lua, A) -> Result, { let f = self.lua.create_function(func)?; self.with_value(key, Value::Function(f)) } pub fn with_async_function(self, key: &'static str, func: F) -> Result where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + Fn(&'lua Lua, A) -> FR, FR: 'lua + Future>, { let f = self.lua.create_async_function(func)?; self.with_value(key, Value::Function(f)) } pub fn build(self) -> Result> { self.tab.set_readonly(true); Ok(self.tab) } }