Documented code, got basic example working

This commit is contained in:
Isaac Clayton 2022-06-01 16:39:59 +02:00
parent 4ff9a6b1b5
commit 4003037ca8
7 changed files with 200 additions and 57 deletions

View file

@ -1,40 +1,10 @@
use std::collections::{HashMap, HashSet};
use mlua::{Function, Lua, LuaSerdeExt, Value};
use serde::{de::DeserializeOwned, Serialize};
use mlua::{Error, FromLua, Function, Lua, LuaSerdeExt, ToLua, UserData, Value};
pub use map_macro::{map, set};
pub mod runtime;
pub use runtime::*;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
impl Runtime for Lua {
type Module = String;
fn init(module: Self::Module) -> Option<Self> {
let lua = Lua::new();
lua.load(&module).exec().ok()?;
return Some(lua);
}
fn handles(&self) -> Handles {
let mut globals = HashSet::new();
for pair in self.globals().pairs::<String, Value>() {
if let Ok((k, _)) = pair {
globals.insert(k);
}
}
globals
}
fn val<T: DeserializeOwned>(&self, name: String) -> Option<T> {
let val: Value = self.globals().get(name).ok()?;
Some(self.from_value(val).ok()?)
}
fn call<T: Serialize + DeserializeOwned>(&self, name: String, arg: T) -> Option<T> {
let fun: Function = self.globals().get(name).ok()?;
let arg: Value = self.to_value(&arg).ok()?;
let result = fun.call(arg).ok()?;
Some(self.from_value(result).ok()?)
}
}
pub mod lua;
pub use lua::*;