summaryrefslogtreecommitdiff
path: root/src/lua/runnable.rs
diff options
context:
space:
mode:
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)
+ }
+}
+