From 7701aeef85cb1f110ed27d75e04b9f8e768e0216 Mon Sep 17 00:00:00 2001 From: kyren Date: Thu, 8 Feb 2018 05:00:11 -0500 Subject: [PATCH] TERRIBLE HACK FIX I DO NOT UNDERSTAND Okay, so this is the fix for the previously mentioned lifetime problem. I mimicked the API for `crossbeam::scope` extremely closely for `Lua::scope`, and for some reason things that would not compile with `crossbeam::scope` WOULD compile with `Lua::scope`, and I could not figure it out. So I took the crossbeam source and made tiny edits until I determined the crossover point where invalid borrows would compile, and it was.. not what I expected it to be. Simply replacing a RefCell>> with a PhantomData<&'a ()> would suddenly cause this to compile with crossbeam: ``` struct Test { field: i32, } crossbeam::scope(|scope| { let mut t = Test { field: 0, }; scope.spawn(|| t.field = 42); drop(t); // ...anything }) ``` which is precisely the same problem as `rlua`. To say I am unsatisfied by this fix is a drastic understatement. SURELY this must be a compiler bug? --- src/lua.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lua.rs b/src/lua.rs index a7bf385..a878638 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -33,6 +33,7 @@ pub struct Lua { pub struct Scope<'scope> { lua: &'scope Lua, destructors: RefCell Box>>>, + _phantom: PhantomData>, } // Data associated with the main lua_State via lua_getextraspace. @@ -326,6 +327,7 @@ impl Lua { let mut scope = Scope { lua: self, destructors: RefCell::new(Vec::new()), + _phantom: PhantomData, }; let r = f(&mut scope); drop(scope);