From ce7e8e61fd9849695527b3c0b04db8f7c7c94db1 Mon Sep 17 00:00:00 2001 From: kyren Date: Sun, 11 Feb 2018 08:51:17 -0500 Subject: shave this yak some more, make `Callback` type alias have two lifetimes --- src/lua.rs | 15 +++++---------- src/types.rs | 3 ++- src/userdata.rs | 10 +++++----- 3 files changed, 12 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/lua.rs b/src/lua.rs index 9dcd438..9526301 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -973,7 +973,7 @@ impl Lua { fn create_callback_function<'lua, 'callback>( &'lua self, - func: Callback<'callback>, + func: Callback<'callback, 'static>, ) -> Result> { unsafe extern "C" fn callback_call_impl(state: *mut ffi::lua_State) -> c_int { callback_error(state, || { @@ -1066,16 +1066,11 @@ impl<'lua, 'scope> Scope<'lua, 'scope> { R: ToLuaMulti<'callback>, F: 'scope + Fn(&'callback Lua, A) -> Result, { - let f: Box< - Fn(&'callback Lua, MultiValue<'callback>) -> Result> + 'scope, - > = Box::new(move |lua, args| func(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua)); - unsafe { - // SCARY, we are transmuting the 'scope lifetime to 'static. - let f: Box< - Fn(&'callback Lua, MultiValue<'callback>) -> Result>, - > = mem::transmute(f); - + let f = Box::new(move |lua, args| { + func(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua) + }); + let f = mem::transmute::, Callback<'callback, 'static>>(f); let mut f = self.lua.create_callback_function(f)?; f.0.drop_unref = false; diff --git a/src/types.rs b/src/types.rs index 9398871..8ab85c3 100644 --- a/src/types.rs +++ b/src/types.rs @@ -44,7 +44,8 @@ impl Drop for RegistryKey { } } -pub(crate) type Callback<'lua> = Box) -> Result>>; +pub(crate) type Callback<'lua, 'a> = + Box) -> Result> + 'a>; pub(crate) struct LuaRef<'lua> { pub lua: &'lua Lua, diff --git a/src/userdata.rs b/src/userdata.rs index c48a1b4..c119911 100644 --- a/src/userdata.rs +++ b/src/userdata.rs @@ -72,8 +72,8 @@ pub enum MetaMethod { /// /// [`UserData`]: trait.UserData.html pub struct UserDataMethods<'lua, T> { - pub(crate) methods: HashMap>, - pub(crate) meta_methods: HashMap>, + pub(crate) methods: HashMap>, + pub(crate) meta_methods: HashMap>, pub(crate) _type: PhantomData, } @@ -175,7 +175,7 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> { self.meta_methods.insert(meta, Self::box_function(function)); } - fn box_function(function: F) -> Callback<'lua> + fn box_function(function: F) -> Callback<'lua, 'static> where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, @@ -184,7 +184,7 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> { Box::new(move |lua, args| function(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua)) } - fn box_method(method: M) -> Callback<'lua> + fn box_method(method: M) -> Callback<'lua, 'static> where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, @@ -205,7 +205,7 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> { }) } - fn box_method_mut(method: M) -> Callback<'lua> + fn box_method_mut(method: M) -> Callback<'lua, 'static> where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, -- cgit v1.2.3