diff options
author | kyren <kerriganw@gmail.com> | 2018-02-11 08:51:17 -0500 |
---|---|---|
committer | kyren <kerriganw@gmail.com> | 2018-02-11 08:51:17 -0500 |
commit | ce7e8e61fd9849695527b3c0b04db8f7c7c94db1 (patch) | |
tree | efba810062822e92bc65d84f0a8ffd467101f588 /src | |
parent | da1e1625b3f8359454a20a44db9ed18a763e11f7 (diff) | |
download | mlua-ce7e8e61fd9849695527b3c0b04db8f7c7c94db1.zip |
shave this yak some more, make `Callback` type alias have two lifetimes
Diffstat (limited to 'src')
-rw-r--r-- | src/lua.rs | 15 | ||||
-rw-r--r-- | src/types.rs | 3 | ||||
-rw-r--r-- | src/userdata.rs | 10 |
3 files changed, 12 insertions, 16 deletions
@@ -973,7 +973,7 @@ impl Lua { fn create_callback_function<'lua, 'callback>( &'lua self, - func: Callback<'callback>, + func: Callback<'callback, 'static>, ) -> Result<Function<'lua>> { 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<R>, { - let f: Box< - Fn(&'callback Lua, MultiValue<'callback>) -> Result<MultiValue<'callback>> + '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<MultiValue<'callback>>, - > = 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, 'scope>, 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<Fn(&'lua Lua, MultiValue<'lua>) -> Result<MultiValue<'lua>>>; +pub(crate) type Callback<'lua, 'a> = + Box<Fn(&'lua Lua, MultiValue<'lua>) -> Result<MultiValue<'lua>> + '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<StdString, Callback<'lua>>, - pub(crate) meta_methods: HashMap<MetaMethod, Callback<'lua>>, + pub(crate) methods: HashMap<StdString, Callback<'lua, 'static>>, + pub(crate) meta_methods: HashMap<MetaMethod, Callback<'lua, 'static>>, pub(crate) _type: PhantomData<T>, } @@ -175,7 +175,7 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> { self.meta_methods.insert(meta, Self::box_function(function)); } - fn box_function<A, R, F>(function: F) -> Callback<'lua> + fn box_function<A, R, F>(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<A, R, M>(method: M) -> Callback<'lua> + fn box_method<A, R, M>(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<A, R, M>(method: M) -> Callback<'lua> + fn box_method_mut<A, R, M>(method: M) -> Callback<'lua, 'static> where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, |