From b35ff5fa1206b11b0c486ff01e7151f61abb72b2 Mon Sep 17 00:00:00 2001 From: kyren Date: Sun, 5 Aug 2018 19:02:19 -0400 Subject: [PATCH] Remove out of date documentation, simpler scope lifetimes The documentation describing it being a logic bug to access "outer" callback handles when inside an "inner" callback is inaccurate, that was only true when using an older design for handle values. Also, there is no reason to have a separate 'callback lifetime, because 'scope is already invariant and just using 'scope seems equivalent. --- src/scope.rs | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/src/scope.rs b/src/scope.rs index 58f440f..b8c2ad8 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -39,35 +39,19 @@ impl<'scope> Scope<'scope> { /// This is a version of [`Lua::create_function`] that creates a callback which expires on scope /// drop. See [`Lua::scope`] for more details. /// - /// Since the provided function does not have to be 'static, it is easy to capture outer - /// variables in the provided callback. However, you must *not* use Lua handle values (`Table`, - /// `Function` etc) or a `Lua` instance that you have captured from an outer level inside such a - /// callback. It is *always* a logic error to access a `Lua` instance or handle value from an - /// "outer" callback level inside an "inner" callback level, Lua does stack protection during - /// callbacks that makes the outer instances unusable until the callback returns. This is true - /// regardless of the use of `Lua::scope`, but it is very difficult (though not impossible!) to - /// run into unless you can create callbacks that are non-'static. - /// - /// If you do access outer `Lua` instances or handles inside an inner callback, this will result - /// in a panic. You can instead use either [`RegistryKey`] values or [`Function::bind`] to pass - /// values to callbacks without error. - /// /// [`Lua::create_function`]: struct.Lua.html#method.create_function /// [`Lua::scope`]: struct.Lua.html#method.scope - /// [`RegistryKey`]: struct.RegistryKey.html - /// [`Function::bind`]: struct.Function.html#method.bind - pub fn create_function<'callback, 'lua, A, R, F>(&'lua self, func: F) -> Result> + pub fn create_function<'lua, A, R, F>(&'lua self, func: F) -> Result> where - A: FromLuaMulti<'callback>, - R: ToLuaMulti<'callback>, - F: 'scope + Fn(&'callback Lua, A) -> Result, - 'scope: 'callback, + A: FromLuaMulti<'scope>, + R: ToLuaMulti<'scope>, + F: 'scope + Fn(&'scope Lua, A) -> Result, { unsafe { 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 f = mem::transmute::, Callback<'scope, 'static>>(f); let f = self.lua.create_callback(f)?; let mut destructors = self.destructors.borrow_mut(); @@ -99,15 +83,11 @@ impl<'scope> Scope<'scope> { /// [`Lua::create_function_mut`]: struct.Lua.html#method.create_function_mut /// [`Lua::scope`]: struct.Lua.html#method.scope /// [`Scope::create_function`]: #method.create_function - pub fn create_function_mut<'callback, 'lua, A, R, F>( - &'lua self, - func: F, - ) -> Result> + pub fn create_function_mut<'lua, A, R, F>(&'lua self, func: F) -> Result> where - A: FromLuaMulti<'callback>, - R: ToLuaMulti<'callback>, - F: 'scope + FnMut(&'callback Lua, A) -> Result, - 'scope: 'callback, + A: FromLuaMulti<'scope>, + R: ToLuaMulti<'scope>, + F: 'scope + FnMut(&'scope Lua, A) -> Result, { let func = RefCell::new(func); self.create_function(move |lua, args| {