diff --git a/src/lua.rs b/src/lua.rs index fcd1a87..4af24eb 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -967,9 +967,9 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> { /// back to the user-provided metamethod if no regular method was found. pub fn add_method(&mut self, name: &str, method: M) where - A: 'lua + FromLuaMulti<'lua>, - R: 'lua + ToLuaMulti<'lua>, - M: 'lua + for<'a> FnMut(&'lua Lua, &'a T, A) -> Result, + A: FromLuaMulti<'lua>, + R: ToLuaMulti<'lua>, + M: 'static + for<'a> FnMut(&'lua Lua, &'a T, A) -> Result, { self.methods.insert( name.to_owned(), @@ -984,9 +984,9 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> { /// [`add_method`]: #method.add_method pub fn add_method_mut(&mut self, name: &str, method: M) where - A: 'lua + FromLuaMulti<'lua>, - R: 'lua + ToLuaMulti<'lua>, - M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, A) -> Result, + A: FromLuaMulti<'lua>, + R: ToLuaMulti<'lua>, + M: 'static + for<'a> FnMut(&'lua Lua, &'a mut T, A) -> Result, { self.methods.insert( name.to_owned(), @@ -1003,9 +1003,9 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> { /// [`add_method_mut`]: #method.add_method_mut pub fn add_function(&mut self, name: &str, function: F) where - A: 'lua + FromLuaMulti<'lua>, - R: 'lua + ToLuaMulti<'lua>, - F: 'lua + FnMut(&'lua Lua, A) -> Result, + A: FromLuaMulti<'lua>, + R: ToLuaMulti<'lua>, + F: 'static + FnMut(&'lua Lua, A) -> Result, { self.methods.insert( name.to_owned(), @@ -1023,9 +1023,9 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> { /// [`add_meta_function`]: #method.add_meta_function pub fn add_meta_method(&mut self, meta: MetaMethod, method: M) where - A: 'lua + FromLuaMulti<'lua>, - R: 'lua + ToLuaMulti<'lua>, - M: 'lua + for<'a> FnMut(&'lua Lua, &'a T, A) -> Result, + A: FromLuaMulti<'lua>, + R: ToLuaMulti<'lua>, + M: 'static + for<'a> FnMut(&'lua Lua, &'a T, A) -> Result, { self.meta_methods.insert(meta, Self::box_method(method)); } @@ -1040,9 +1040,9 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> { /// [`add_meta_function`]: #method.add_meta_function pub fn add_meta_method_mut(&mut self, meta: MetaMethod, method: M) where - A: 'lua + FromLuaMulti<'lua>, - R: 'lua + ToLuaMulti<'lua>, - M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, A) -> Result, + A: FromLuaMulti<'lua>, + R: ToLuaMulti<'lua>, + M: 'static + for<'a> FnMut(&'lua Lua, &'a mut T, A) -> Result, { self.meta_methods.insert(meta, Self::box_method_mut(method)); } @@ -1054,18 +1054,18 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> { /// userdata of type `T`. pub fn add_meta_function(&mut self, meta: MetaMethod, function: F) where - A: 'lua + FromLuaMulti<'lua>, - R: 'lua + ToLuaMulti<'lua>, - F: 'lua + FnMut(&'lua Lua, A) -> Result, + A: FromLuaMulti<'lua>, + R: ToLuaMulti<'lua>, + F: 'static + FnMut(&'lua Lua, A) -> Result, { self.meta_methods.insert(meta, Self::box_function(function)); } fn box_function(mut function: F) -> Callback<'lua> where - A: 'lua + FromLuaMulti<'lua>, - R: 'lua + ToLuaMulti<'lua>, - F: 'lua + for<'a> FnMut(&'lua Lua, A) -> Result, + A: FromLuaMulti<'lua>, + R: ToLuaMulti<'lua>, + F: 'static + for<'a> FnMut(&'lua Lua, A) -> Result, { Box::new(move |lua, args| { function(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi( @@ -1076,9 +1076,9 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> { fn box_method(mut method: M) -> Callback<'lua> where - A: 'lua + FromLuaMulti<'lua>, - R: 'lua + ToLuaMulti<'lua>, - M: 'lua + for<'a> FnMut(&'lua Lua, &'a T, A) -> Result, + A: FromLuaMulti<'lua>, + R: ToLuaMulti<'lua>, + M: 'static + for<'a> FnMut(&'lua Lua, &'a T, A) -> Result, { Box::new(move |lua, mut args| if let Some(front) = args.pop_front() { let userdata = AnyUserData::from_lua(front, lua)?; @@ -1095,9 +1095,9 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> { fn box_method_mut(mut method: M) -> Callback<'lua> where - A: 'lua + FromLuaMulti<'lua>, - R: 'lua + ToLuaMulti<'lua>, - M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, A) -> Result, + A: FromLuaMulti<'lua>, + R: ToLuaMulti<'lua>, + M: 'static + for<'a> FnMut(&'lua Lua, &'a mut T, A) -> Result, { Box::new(move |lua, mut args| if let Some(front) = args.pop_front() { let userdata = AnyUserData::from_lua(front, lua)?; @@ -1539,9 +1539,9 @@ impl Lua { /// ``` pub fn create_function<'lua, A, R, F>(&'lua self, mut func: F) -> Function<'lua> where - A: 'lua + FromLuaMulti<'lua>, - R: 'lua + ToLuaMulti<'lua>, - F: 'lua + FnMut(&'lua Lua, A) -> Result, + A: FromLuaMulti<'lua>, + R: ToLuaMulti<'lua>, + F: 'static + FnMut(&'lua Lua, A) -> Result, { self.create_callback_function(Box::new(move |lua, args| { func(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua) diff --git a/src/tests.rs b/src/tests.rs index f5d0ad1..341d1e6 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -205,32 +205,27 @@ fn test_bind() { #[test] fn test_rust_function() { - let mut captured_var = 2; - { - let lua = Lua::new(); - let globals = lua.globals(); - lua.exec::<()>( - r#" - function lua_function() - return rust_function() - end + let lua = Lua::new(); + let globals = lua.globals(); + lua.exec::<()>( + r#" + function lua_function() + return rust_function() + end - -- Test to make sure chunk return is ignored - return 1 - "#, - None, - ).unwrap(); + -- Test to make sure chunk return is ignored + return 1 + "#, + None, + ).unwrap(); - let lua_function = globals.get::<_, Function>("lua_function").unwrap(); - let rust_function = lua.create_function(|_, _: ()| { - captured_var = 42; - Ok("hello") - }); + let lua_function = globals.get::<_, Function>("lua_function").unwrap(); + let rust_function = lua.create_function(|_, _: ()| { + Ok("hello") + }); - globals.set("rust_function", rust_function).unwrap(); - assert_eq!(lua_function.call::<_, String>(()).unwrap(), "hello"); - } - assert_eq!(captured_var, 42); + globals.set("rust_function", rust_function).unwrap(); + assert_eq!(lua_function.call::<_, String>(()).unwrap(), "hello"); } #[test] @@ -362,17 +357,6 @@ fn test_scope() { assert_eq!(tin.get::<_, i64>(1).unwrap(), 1); assert_eq!(tin.get::<_, i64>(2).unwrap(), 2); assert_eq!(tin.get::<_, i64>(3).unwrap(), 3); - - // Should not compile, don't know how to test that - // struct UserData; - // impl UserData for UserData {}; - // let userdata_ref; - // { - // let touter = globals.get::<_, Table>("touter").unwrap(); - // touter.set("userdata", lua.create_userdata(UserData).unwrap()).unwrap(); - // let userdata = touter.get::<_, AnyUserData>("userdata").unwrap(); - // userdata_ref = userdata.borrow::(); - // } } #[test] @@ -905,3 +889,29 @@ fn coroutine_panic() { let thrd: Thread = lua.create_thread(thrd_main); thrd.resume::<_, ()>(()).unwrap(); } + + +// Need to use compiletest-rs or similar to make sure these don't compile. +/* +#[test] +fn should_not_compile() { + let lua = Lua::new(); + let globals = lua.globals(); + + // Should not allow userdata borrow to outlive lifetime of AnyUserData handle + struct MyUserData; + impl UserData for MyUserData {}; + let userdata_ref; + { + let touter = globals.get::<_, Table>("touter").unwrap(); + touter.set("userdata", lua.create_userdata(MyUserData)).unwrap(); + let userdata = touter.get::<_, AnyUserData>("userdata").unwrap(); + userdata_ref = userdata.borrow::(); + } + + // Should not allow self borrow of lua, it can change addresses + globals.set("boom", lua.create_function(|_, _| { + lua.pack(lua.eval::("1 + 1", None)?) + })).unwrap(); +} +*/