Merge remote-tracking branch 'origin/master' into tuples

This commit is contained in:
kyren 2017-08-01 13:02:58 -04:00
commit c1abc18066
2 changed files with 74 additions and 64 deletions

View File

@ -967,9 +967,9 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> {
/// back to the user-provided metamethod if no regular method was found. /// back to the user-provided metamethod if no regular method was found.
pub fn add_method<A, R, M>(&mut self, name: &str, method: M) pub fn add_method<A, R, M>(&mut self, name: &str, method: M)
where where
A: 'lua + FromLuaMulti<'lua>, A: FromLuaMulti<'lua>,
R: 'lua + ToLuaMulti<'lua>, R: ToLuaMulti<'lua>,
M: 'lua + for<'a> FnMut(&'lua Lua, &'a T, A) -> Result<R>, M: 'static + for<'a> FnMut(&'lua Lua, &'a T, A) -> Result<R>,
{ {
self.methods.insert( self.methods.insert(
name.to_owned(), name.to_owned(),
@ -984,9 +984,9 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> {
/// [`add_method`]: #method.add_method /// [`add_method`]: #method.add_method
pub fn add_method_mut<A, R, M>(&mut self, name: &str, method: M) pub fn add_method_mut<A, R, M>(&mut self, name: &str, method: M)
where where
A: 'lua + FromLuaMulti<'lua>, A: FromLuaMulti<'lua>,
R: 'lua + ToLuaMulti<'lua>, R: ToLuaMulti<'lua>,
M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, A) -> Result<R>, M: 'static + for<'a> FnMut(&'lua Lua, &'a mut T, A) -> Result<R>,
{ {
self.methods.insert( self.methods.insert(
name.to_owned(), name.to_owned(),
@ -1003,9 +1003,9 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> {
/// [`add_method_mut`]: #method.add_method_mut /// [`add_method_mut`]: #method.add_method_mut
pub fn add_function<A, R, F>(&mut self, name: &str, function: F) pub fn add_function<A, R, F>(&mut self, name: &str, function: F)
where where
A: 'lua + FromLuaMulti<'lua>, A: FromLuaMulti<'lua>,
R: 'lua + ToLuaMulti<'lua>, R: ToLuaMulti<'lua>,
F: 'lua + FnMut(&'lua Lua, A) -> Result<R>, F: 'static + FnMut(&'lua Lua, A) -> Result<R>,
{ {
self.methods.insert( self.methods.insert(
name.to_owned(), name.to_owned(),
@ -1023,9 +1023,9 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> {
/// [`add_meta_function`]: #method.add_meta_function /// [`add_meta_function`]: #method.add_meta_function
pub fn add_meta_method<A, R, M>(&mut self, meta: MetaMethod, method: M) pub fn add_meta_method<A, R, M>(&mut self, meta: MetaMethod, method: M)
where where
A: 'lua + FromLuaMulti<'lua>, A: FromLuaMulti<'lua>,
R: 'lua + ToLuaMulti<'lua>, R: ToLuaMulti<'lua>,
M: 'lua + for<'a> FnMut(&'lua Lua, &'a T, A) -> Result<R>, M: 'static + for<'a> FnMut(&'lua Lua, &'a T, A) -> Result<R>,
{ {
self.meta_methods.insert(meta, Self::box_method(method)); 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 /// [`add_meta_function`]: #method.add_meta_function
pub fn add_meta_method_mut<A, R, M>(&mut self, meta: MetaMethod, method: M) pub fn add_meta_method_mut<A, R, M>(&mut self, meta: MetaMethod, method: M)
where where
A: 'lua + FromLuaMulti<'lua>, A: FromLuaMulti<'lua>,
R: 'lua + ToLuaMulti<'lua>, R: ToLuaMulti<'lua>,
M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, A) -> Result<R>, M: 'static + for<'a> FnMut(&'lua Lua, &'a mut T, A) -> Result<R>,
{ {
self.meta_methods.insert(meta, Self::box_method_mut(method)); 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`. /// userdata of type `T`.
pub fn add_meta_function<A, R, F>(&mut self, meta: MetaMethod, function: F) pub fn add_meta_function<A, R, F>(&mut self, meta: MetaMethod, function: F)
where where
A: 'lua + FromLuaMulti<'lua>, A: FromLuaMulti<'lua>,
R: 'lua + ToLuaMulti<'lua>, R: ToLuaMulti<'lua>,
F: 'lua + FnMut(&'lua Lua, A) -> Result<R>, F: 'static + FnMut(&'lua Lua, A) -> Result<R>,
{ {
self.meta_methods.insert(meta, Self::box_function(function)); self.meta_methods.insert(meta, Self::box_function(function));
} }
fn box_function<A, R, F>(mut function: F) -> Callback<'lua> fn box_function<A, R, F>(mut function: F) -> Callback<'lua>
where where
A: 'lua + FromLuaMulti<'lua>, A: FromLuaMulti<'lua>,
R: 'lua + ToLuaMulti<'lua>, R: ToLuaMulti<'lua>,
F: 'lua + for<'a> FnMut(&'lua Lua, A) -> Result<R>, F: 'static + for<'a> FnMut(&'lua Lua, A) -> Result<R>,
{ {
Box::new(move |lua, args| { Box::new(move |lua, args| {
function(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi( 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<A, R, M>(mut method: M) -> Callback<'lua> fn box_method<A, R, M>(mut method: M) -> Callback<'lua>
where where
A: 'lua + FromLuaMulti<'lua>, A: FromLuaMulti<'lua>,
R: 'lua + ToLuaMulti<'lua>, R: ToLuaMulti<'lua>,
M: 'lua + for<'a> FnMut(&'lua Lua, &'a T, A) -> Result<R>, M: 'static + for<'a> FnMut(&'lua Lua, &'a T, A) -> Result<R>,
{ {
Box::new(move |lua, mut args| if let Some(front) = args.pop_front() { Box::new(move |lua, mut args| if let Some(front) = args.pop_front() {
let userdata = AnyUserData::from_lua(front, lua)?; let userdata = AnyUserData::from_lua(front, lua)?;
@ -1095,9 +1095,9 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> {
fn box_method_mut<A, R, M>(mut method: M) -> Callback<'lua> fn box_method_mut<A, R, M>(mut method: M) -> Callback<'lua>
where where
A: 'lua + FromLuaMulti<'lua>, A: FromLuaMulti<'lua>,
R: 'lua + ToLuaMulti<'lua>, R: ToLuaMulti<'lua>,
M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, A) -> Result<R>, M: 'static + for<'a> FnMut(&'lua Lua, &'a mut T, A) -> Result<R>,
{ {
Box::new(move |lua, mut args| if let Some(front) = args.pop_front() { Box::new(move |lua, mut args| if let Some(front) = args.pop_front() {
let userdata = AnyUserData::from_lua(front, lua)?; 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> pub fn create_function<'lua, A, R, F>(&'lua self, mut func: F) -> Function<'lua>
where where
A: 'lua + FromLuaMulti<'lua>, A: FromLuaMulti<'lua>,
R: 'lua + ToLuaMulti<'lua>, R: ToLuaMulti<'lua>,
F: 'lua + FnMut(&'lua Lua, A) -> Result<R>, F: 'static + FnMut(&'lua Lua, A) -> Result<R>,
{ {
self.create_callback_function(Box::new(move |lua, args| { self.create_callback_function(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)

View File

@ -205,8 +205,6 @@ fn test_bind() {
#[test] #[test]
fn test_rust_function() { fn test_rust_function() {
let mut captured_var = 2;
{
let lua = Lua::new(); let lua = Lua::new();
let globals = lua.globals(); let globals = lua.globals();
lua.exec::<()>( lua.exec::<()>(
@ -223,15 +221,12 @@ fn test_rust_function() {
let lua_function = globals.get::<_, Function>("lua_function").unwrap(); let lua_function = globals.get::<_, Function>("lua_function").unwrap();
let rust_function = lua.create_function(|_, _: ()| { let rust_function = lua.create_function(|_, _: ()| {
captured_var = 42;
Ok("hello") Ok("hello")
}); });
globals.set("rust_function", rust_function).unwrap(); globals.set("rust_function", rust_function).unwrap();
assert_eq!(lua_function.call::<_, String>(()).unwrap(), "hello"); assert_eq!(lua_function.call::<_, String>(()).unwrap(), "hello");
} }
assert_eq!(captured_var, 42);
}
#[test] #[test]
fn test_user_data() { fn test_user_data() {
@ -362,17 +357,6 @@ fn test_scope() {
assert_eq!(tin.get::<_, i64>(1).unwrap(), 1); assert_eq!(tin.get::<_, i64>(1).unwrap(), 1);
assert_eq!(tin.get::<_, i64>(2).unwrap(), 2); assert_eq!(tin.get::<_, i64>(2).unwrap(), 2);
assert_eq!(tin.get::<_, i64>(3).unwrap(), 3); 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::<UserData>();
// }
} }
#[test] #[test]
@ -905,3 +889,29 @@ fn coroutine_panic() {
let thrd: Thread = lua.create_thread(thrd_main); let thrd: Thread = lua.create_thread(thrd_main);
thrd.resume::<_, ()>(()).unwrap(); 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::<MyUserData>();
}
// Should not allow self borrow of lua, it can change addresses
globals.set("boom", lua.create_function(|_, _| {
lua.pack(lua.eval::<i32>("1 + 1", None)?)
})).unwrap();
}
*/