extern crate hlua; #[test] fn readwrite() { #[derive(Clone)] struct Foo; impl hlua::Push for Foo where L: hlua::AsMutLua { fn push_to_lua(self, lua: L) -> hlua::PushGuard { hlua::userdata::push_userdata(self, lua, |_|{}) } } impl hlua::LuaRead for Foo where L: hlua::AsMutLua { fn lua_read_at_position(lua: L, index: i32) -> Result { let val: Result, _> = hlua::LuaRead::lua_read_at_position(lua, index); val.map(|d| d.clone()) } } let mut lua = hlua::Lua::new(); lua.set("a", Foo); let _: Foo = lua.get("a").unwrap(); } #[test] fn destructor_called() { use std::sync::{Arc, Mutex}; let called = Arc::new(Mutex::new(false)); struct Foo { called: Arc> } impl Drop for Foo { fn drop(&mut self) { let mut called = self.called.lock().unwrap(); (*called) = true; } } impl hlua::Push for Foo where L: hlua::AsMutLua { fn push_to_lua(self, lua: L) -> hlua::PushGuard { hlua::userdata::push_userdata(self, lua, |_|{}) } } { let mut lua = hlua::Lua::new(); lua.set("a", Foo{called: called.clone()}); } let locked = called.lock().unwrap(); assert!(*locked); } #[test] fn type_check() { #[derive(Clone)] struct Foo; impl hlua::Push for Foo where L: hlua::AsMutLua { fn push_to_lua(self, lua: L) -> hlua::PushGuard { hlua::userdata::push_userdata(self, lua, |_|{}) } } impl hlua::LuaRead for Foo where L: hlua::AsMutLua { fn lua_read_at_position(lua: L, index: i32) -> Result { let val: Result, _> = hlua::LuaRead::lua_read_at_position(lua, index); val.map(|d| d.clone()) } } #[derive(Clone)] struct Bar; impl hlua::Push for Bar where L: hlua::AsMutLua { fn push_to_lua(self, lua: L) -> hlua::PushGuard { hlua::userdata::push_userdata(self, lua, |_|{}) } } impl hlua::LuaRead for Bar where L: hlua::AsMutLua { fn lua_read_at_position(lua: L, index: i32) -> Result { let val: Result, _> = hlua::LuaRead::lua_read_at_position(lua, index); val.map(|d| d.clone()) } } let mut lua = hlua::Lua::new(); lua.set("a", Foo); let x: Option = lua.get("a"); assert!(x.is_none()) } #[test] fn metatables() { #[derive(Clone)] struct Foo; impl hlua::Push for Foo where L: hlua::AsMutLua { fn push_to_lua(self, lua: L) -> hlua::PushGuard { hlua::userdata::push_userdata(self, lua, |mut table| { table.set("__index".to_string(), vec![ ("test".to_string(), hlua::function0(|| 5)) ]); }) } } let mut lua = hlua::Lua::new(); lua.set("a", Foo); let x: i32 = lua.execute("return a.test()").unwrap(); assert_eq!(x, 5); }