diff --git a/examples/examples.rs b/examples/examples.rs index 6bd7738..ca2e5b3 100644 --- a/examples/examples.rs +++ b/examples/examples.rs @@ -2,6 +2,8 @@ extern crate hlist_macro; extern crate rlua; +use std::f32; + use rlua::*; fn examples() -> LuaResult<()> { @@ -154,10 +156,7 @@ fn examples() -> LuaResult<()> { })?; globals.set("vec2", vec2_constructor)?; - assert_eq!( - lua.eval::("(vec2(1, 2) + vec2(2, 2)):magnitude()")?, - 5.0 - ); + assert!(lua.eval::("(vec2(1, 2) + vec2(2, 2)):magnitude()")? - 5.0 < f32::EPSILON); Ok(()) } diff --git a/src/lua.rs b/src/lua.rs index 1b12b83..2460657 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -31,12 +31,12 @@ pub enum LuaValue<'lua> { } pub use self::LuaValue::Nil as LuaNil; -/// Trait for types convertible to LuaValue +/// Trait for types convertible to `LuaValue` pub trait ToLua<'a> { fn to_lua(self, lua: &'a Lua) -> LuaResult>; } -/// Trait for types convertible from LuaValue +/// Trait for types convertible from `LuaValue` pub trait FromLua<'a>: Sized { fn from_lua(lua_value: LuaValue<'a>, lua: &'a Lua) -> LuaResult; } @@ -416,8 +416,9 @@ impl<'lua> LuaFunction<'lua> { } } -/// A LuaThread is Active before the coroutine function finishes, Dead after it finishes, and in -/// Error state if error has been called inside the coroutine. +/// A `LuaThread` is Active before the coroutine function finishes, Dead after +/// it finishes, and in Error state if error has been called inside the +/// coroutine. #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum LuaThreadStatus { Dead, @@ -430,8 +431,8 @@ pub enum LuaThreadStatus { pub struct LuaThread<'lua>(LuaRef<'lua>); impl<'lua> LuaThread<'lua> { - /// If this thread has yielded a value, will return Some, otherwise the thread is finished and - /// this will return None. + /// If this thread has yielded a value, will return Some, otherwise the + /// thread is finished and this will return None. pub fn resume, R: FromLuaMulti<'lua>>( &self, args: A, @@ -463,7 +464,7 @@ impl<'lua> LuaThread<'lua> { for _ in 0..nresults { results.push_front(lua.pop_value(thread_state)?); } - R::from_lua_multi(results, lua).map(|r| Some(r)) + R::from_lua_multi(results, lua).map(Some) }) } } @@ -614,7 +615,7 @@ pub trait LuaUserDataType: 'static + Sized { } /// Handle to an internal instance of custom userdata. All userdata in this API -/// is based around RefCell, to best match the mutable semantics of the lua +/// is based around `RefCell`, to best match the mutable semantics of the lua /// language. #[derive(Clone, Debug)] pub struct LuaUserData<'lua>(LuaRef<'lua>); @@ -881,7 +882,7 @@ impl Lua { check_stack(self.state, 3)?; ffi::lua_newtable(self.state); - for (k, v) in cont.into_iter() { + for (k, v) in cont { self.push_value(self.state, k.to_lua(self)?)?; self.push_value(self.state, v.to_lua(self)?)?; ffi::lua_rawset(self.state, -3); @@ -947,7 +948,7 @@ impl Lua { } /// Returns a handle to the globals table - pub fn globals<'lua>(&'lua self) -> LuaResult> { + pub fn globals(&self) -> LuaResult { unsafe { check_stack(self.state, 1)?; ffi::lua_rawgeti(self.state, ffi::LUA_REGISTRYINDEX, ffi::LUA_RIDX_GLOBALS); diff --git a/src/multi.rs b/src/multi.rs index f5e4e10..0b27aaf 100644 --- a/src/multi.rs +++ b/src/multi.rs @@ -15,6 +15,9 @@ impl<'lua> FromLuaMulti<'lua> for () { } } +/// Result is convertible to `LuaMultiValue` following the common lua idiom of +/// returning the result on success, or in the case of an error, returning nil +/// followed by the error impl<'lua, T: ToLua<'lua>, E: ToLua<'lua>> ToLuaMulti<'lua> for Result { fn to_lua_multi(self, lua: &'lua Lua) -> LuaResult> { let mut result = LuaMultiValue::new(); @@ -57,6 +60,10 @@ impl<'lua> FromLuaMulti<'lua> for LuaMultiValue<'lua> { } } +/// Can be used to pass variadic values to or receive variadic values from lua, +/// where the type of the values is all the same and the number of values is +/// defined at runtime. This can be included in an hlist when unpacking, but +/// must be the final entry, and will consume the rest of the parameters given. pub struct LuaVariadic(pub Vec); impl<'lua, T: ToLua<'lua>> ToLuaMulti<'lua> for LuaVariadic { diff --git a/src/tests.rs b/src/tests.rs index 8c5942a..13aa7d1 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -631,10 +631,10 @@ fn test_result_conversions() { let lua = Lua::new(); let globals = lua.globals().unwrap(); - let err = lua.create_function(|lua, args| { + let err = lua.create_function(|lua, _| { lua.pack(Result::Err::("only through failure can we succeed".to_string())) }).unwrap(); - let ok = lua.create_function(|lua, args| { + let ok = lua.create_function(|lua, _| { lua.pack(Result::Ok::("!".to_string())) }).unwrap();