daily pastebin goal
70%
SHARE
TWEET

Untitled

a guest Oct 23rd, 2018 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #![allow(dead_code)]
  2.  
  3. use std::cell::RefCell;
  4. use std::marker::PhantomData;
  5.  
  6. // Lua is Send but !Sync
  7. struct Lua(RefCell<()>);
  8.  
  9. struct LuaValue<'a>(&'a Lua);
  10.  
  11. trait ToLua<'a> {
  12.     fn to_lua(self, &'a Lua) -> LuaValue<'a>;
  13. }
  14.  
  15. trait FromLua<'a> {
  16.     fn from_lua(&'a Lua, LuaValue<'a>) -> Self;
  17. }
  18.  
  19. struct LuaTable<'a>(&'a Lua);
  20.  
  21. impl<'a> ToLua<'a> for LuaTable<'a> {
  22.     fn to_lua(self, lua: &'a Lua) -> LuaValue<'a> {
  23.         LuaValue(lua)
  24.     }
  25. }
  26.  
  27. impl<'a> FromLua<'a> for LuaTable<'a> {
  28.     fn from_lua(lua: &'a Lua, _: LuaValue<'a>) -> Self {
  29.         LuaTable(lua)
  30.     }
  31. }
  32.  
  33. type LuaCallback = Box<for<'a> Fn(&'a Lua, LuaValue<'a>) -> LuaValue<'a> + Send>;
  34.  
  35. fn example_function<'a>(_lua: &'a Lua, arg: LuaTable<'a>) -> LuaTable<'a> {
  36.     arg
  37. }
  38.  
  39. trait CallbackTrait<'lua> {
  40.     fn to_callback(self) -> Box<Fn(&'lua Lua, LuaValue<'lua>) -> LuaValue<'lua> + Send>;
  41. }
  42.  
  43. struct Wrapper<A, R, F>(F, PhantomData<(A, R)>);
  44.  
  45. impl<'lua, A, R, F> CallbackTrait<'lua> for Wrapper<A, R, F>
  46. where
  47.     A: FromLua<'lua>,
  48.     R: ToLua<'lua>,
  49.     F: Fn(&'lua Lua, A) -> R + 'static + Send,
  50. {
  51.     fn to_callback(self) -> Box<Fn(&'lua Lua, LuaValue<'lua>) -> LuaValue<'lua> + Send> {
  52.         let f = self.0;
  53.         Box::new(move |lua, arg| f(lua, FromLua::from_lua(lua, arg)).to_lua(lua))
  54.     }
  55. }
  56.  
  57. fn wrap_callback<A, R, F>(f: F) -> LuaCallback
  58. where
  59.     Wrapper<A, R, F>: for<'lua> CallbackTrait<'lua>,
  60. {
  61.     Wrapper(f, PhantomData).to_callback()
  62. }
  63.  
  64. fn main() {}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top