Clean up interface a bit

This commit is contained in:
Isaac Clayton 2022-06-01 12:43:46 +02:00
parent 8293b6971d
commit 265be4a2fb
3 changed files with 27 additions and 32 deletions

View file

@ -1,12 +1,12 @@
use mlua::{Error, FromLua, Lua, ToLua, UserData};
use std::collections::{HashMap, HashSet};
use mlua::{Error, FromLua, Function, Lua, ToLua, UserData, Value};
pub mod runtime;
pub use runtime::*;
impl Runtime for Lua {
type Module = String;
// type Error = Error;
type Interface = LuaInterface;
fn init(module: Self::Module) -> Option<Self> {
let lua = Lua::new();
@ -14,24 +14,14 @@ impl Runtime for Lua {
return Some(lua);
}
fn interface(&self) -> Self::Interface {
todo!()
fn interface(&self) -> Interface {
let mut globals = HashSet::new();
for pair in self.globals().pairs::<String, Value>() {
if let Ok((k, _)) = pair {
globals.insert(k);
}
}
fn val<'lua, K: ToLua<'lua>, V: FromLua<'lua>>(&'lua self, key: K) -> Option<V> {
self.globals().get(key).ok()
}
}
pub struct LuaInterface {
funs: Vec<String>,
vals: Vec<String>,
}
impl Interface for LuaInterface {
type Handle = String;
fn handles(&self) -> &[Self::Handle] {
todo!()
globals
}
}

View file

@ -4,4 +4,14 @@ use runner::*;
pub fn main() {
let lua: Lua = Runtime::init("x = 7".to_string()).unwrap();
println!("{:?}", lua.interface());
}
struct InterfaceX;
impl InterfaceX {
pub fn get_x<T: Runtime>(runtime: T) -> usize {
// runtime.get("x")
todo!()
}
}

View file

@ -1,22 +1,17 @@
use mlua::{FromLua, ToLua};
use std::collections::HashSet;
pub trait FromRuntime: Sized {
fn from_runtime() -> Option<Self>;
}
use mlua::{FromLua, Lua, ToLua, Value};
pub trait Interface {
type Handle;
fn handles(&self) -> &[Self::Handle];
}
pub type Interface = HashSet<String>;
pub trait Runtime
where
Self: Sized,
{
type Module;
type Interface: Interface;
fn init(plugin: Self::Module) -> Option<Self>;
fn interface(&self) -> Self::Interface;
fn val<'lua, K: ToLua<'lua>, V: FromLua<'lua>>(&'lua self, key: K) -> Option<V>;
fn interface(&self) -> Interface;
// fn val<'a, T>(&'a self, name: String) -> Option<T>;
// fn call<'a, A: MyFromLua<'a>, R>(&'a mut self, name: String, arg: A) -> Option<R>;
}