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.
This commit is contained in:
kyren 2018-08-05 19:02:19 -04:00
parent e4de847395
commit b35ff5fa12
1 changed files with 9 additions and 29 deletions

View File

@ -39,35 +39,19 @@ impl<'scope> Scope<'scope> {
/// This is a version of [`Lua::create_function`] that creates a callback which expires on scope /// This is a version of [`Lua::create_function`] that creates a callback which expires on scope
/// drop. See [`Lua::scope`] for more details. /// 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::create_function`]: struct.Lua.html#method.create_function
/// [`Lua::scope`]: struct.Lua.html#method.scope /// [`Lua::scope`]: struct.Lua.html#method.scope
/// [`RegistryKey`]: struct.RegistryKey.html pub fn create_function<'lua, A, R, F>(&'lua self, func: F) -> Result<Function<'lua>>
/// [`Function::bind`]: struct.Function.html#method.bind
pub fn create_function<'callback, 'lua, A, R, F>(&'lua self, func: F) -> Result<Function<'lua>>
where where
A: FromLuaMulti<'callback>, A: FromLuaMulti<'scope>,
R: ToLuaMulti<'callback>, R: ToLuaMulti<'scope>,
F: 'scope + Fn(&'callback Lua, A) -> Result<R>, F: 'scope + Fn(&'scope Lua, A) -> Result<R>,
'scope: 'callback,
{ {
unsafe { unsafe {
let f = Box::new(move |lua, args| { let f = Box::new(move |lua, args| {
func(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua) func(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua)
}); });
let f = mem::transmute::<Callback<'callback, 'scope>, Callback<'callback, 'static>>(f); let f = mem::transmute::<Callback<'scope, 'scope>, Callback<'scope, 'static>>(f);
let f = self.lua.create_callback(f)?; let f = self.lua.create_callback(f)?;
let mut destructors = self.destructors.borrow_mut(); 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::create_function_mut`]: struct.Lua.html#method.create_function_mut
/// [`Lua::scope`]: struct.Lua.html#method.scope /// [`Lua::scope`]: struct.Lua.html#method.scope
/// [`Scope::create_function`]: #method.create_function /// [`Scope::create_function`]: #method.create_function
pub fn create_function_mut<'callback, 'lua, A, R, F>( pub fn create_function_mut<'lua, A, R, F>(&'lua self, func: F) -> Result<Function<'lua>>
&'lua self,
func: F,
) -> Result<Function<'lua>>
where where
A: FromLuaMulti<'callback>, A: FromLuaMulti<'scope>,
R: ToLuaMulti<'callback>, R: ToLuaMulti<'scope>,
F: 'scope + FnMut(&'callback Lua, A) -> Result<R>, F: 'scope + FnMut(&'scope Lua, A) -> Result<R>,
'scope: 'callback,
{ {
let func = RefCell::new(func); let func = RefCell::new(func);
self.create_function(move |lua, args| { self.create_function(move |lua, args| {