From da1e1625b3f8359454a20a44db9ed18a763e11f7 Mon Sep 17 00:00:00 2001 From: kyren Date: Sun, 11 Feb 2018 08:30:03 -0500 Subject: [PATCH] Specify the types exactly in the scary transmute If I happen to change the definition of the Callback type alias, instead of creating a potentially arbitrary transmute, it will now instead fail to compile. --- src/lua.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/lua.rs b/src/lua.rs index e4b44c1..9dcd438 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -1066,15 +1066,17 @@ impl<'lua, 'scope> Scope<'lua, 'scope> { R: ToLuaMulti<'callback>, F: 'scope + Fn(&'callback Lua, A) -> Result, { - unsafe { - 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) - }); + 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 mut f = self.lua.create_callback_function(mem::transmute(f))?; + let f: Box< + Fn(&'callback Lua, MultiValue<'callback>) -> Result>, + > = mem::transmute(f); + + let mut f = self.lua.create_callback_function(f)?; f.0.drop_unref = false; let mut destructors = self.destructors.borrow_mut();