summaryrefslogtreecommitdiff
path: root/src/lua/runnable.rs
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-08-08 21:46:54 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2024-08-08 21:46:54 +0200
commit4763d8ce3b833df1e7321a407b08666f69657fdb (patch)
tree472e38eedef269c244726083bab94afa2942d945 /src/lua/runnable.rs
parent41d707480f2138a8a9b00b4efac4c87ed0eb79fc (diff)
refactore runnable
Diffstat (limited to 'src/lua/runnable.rs')
-rw-r--r--src/lua/runnable.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/lua/runnable.rs b/src/lua/runnable.rs
new file mode 100644
index 0000000..adda512
--- /dev/null
+++ b/src/lua/runnable.rs
@@ -0,0 +1,42 @@
+use mlua::{FromLuaMulti, Function, IntoLuaMulti, Lua, Result};
+
+use super::ownedfunction::OwnedFunction;
+
+pub trait Runnable<A, R>
+where
+ Self: Send,
+{
+ fn run<'lua>(&self, arg: A, lua: &'lua Lua) -> Result<R>
+ where
+ A: IntoLuaMulti<'lua>,
+ R: FromLuaMulti<'lua>;
+}
+
+impl<A, R, T> Runnable<A, R> for T
+where
+ T: Fn(A) -> R,
+ Self: Send,
+{
+ fn run<'lua>(&self, arg: A, _: &'lua Lua) -> Result<R>
+ where
+ A: IntoLuaMulti<'lua>,
+ R: FromLuaMulti<'lua>,
+ {
+ Ok(self(arg))
+ }
+}
+
+impl<A, R> Runnable<A, R> for OwnedFunction
+where
+ Self: Send,
+{
+ fn run<'lua>(&self, arg: A, lua: &'lua Lua) -> Result<R>
+ where
+ A: IntoLuaMulti<'lua>,
+ R: FromLuaMulti<'lua>,
+ {
+ let func: Function = self.get(lua);
+ func.call::<A, R>(arg)
+ }
+}
+