use std::future::Future; use mlua::{FromLuaMulti, Lua, Result, Table, ToLuaMulti}; 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_table(self, key: &'static str, value: Table) -> Result { self.tab.raw_set(key, value)?; Ok(self) } 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 value = self.lua.create_function(func)?; self.tab.raw_set(key, value)?; Ok(self) } 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 value = self.lua.create_async_function(func)?; self.tab.raw_set(key, value)?; Ok(self) } pub fn build(self) -> Result> { self.tab.set_readonly(true); Ok(self.tab) } }