From 69fa01df4508a56140776f30fb18eecd77312050 Mon Sep 17 00:00:00 2001 From: kyren Date: Mon, 24 Jul 2017 10:40:00 -0400 Subject: [PATCH] auto formatting --- src/error.rs | 4 +--- src/lua.rs | 44 +++++++++++++++++++++++--------------------- src/multi.rs | 8 +++++--- src/tests.rs | 28 +++++++++++----------------- src/util.rs | 24 ++++++++---------------- 5 files changed, 48 insertions(+), 60 deletions(-) diff --git a/src/error.rs b/src/error.rs index 9c9f6f7..6904dcc 100644 --- a/src/error.rs +++ b/src/error.rs @@ -54,9 +54,7 @@ impl fmt::Display for Error { Error::UserDataTypeMismatch => write!(fmt, "Userdata not expected type"), Error::UserDataBorrowError => write!(fmt, "Userdata already mutably borrowed"), Error::UserDataBorrowMutError => write!(fmt, "Userdata already borrowed"), - Error::CallbackError(ref msg, _) => { - write!(fmt, "Error during lua callback: {}", msg) - } + Error::CallbackError(ref msg, _) => write!(fmt, "Error during lua callback: {}", msg), Error::ExternalError(ref err) => err.fmt(fmt), } } diff --git a/src/lua.rs b/src/lua.rs index 6b8579c..8fee6b5 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -48,7 +48,7 @@ pub enum Value<'lua> { /// it is implicitly cloned. Error(Error), } -pub use self::Value::Nil as Nil; +pub use self::Value::Nil; /// Trait for types convertible to `Value`. pub trait ToLua<'a> { @@ -398,10 +398,10 @@ where ffi::lua_pop(lua.state, 1); Some((|| { - let key = K::from_lua(key, lua)?; - let value = V::from_lua(value, lua)?; - Ok((key, value)) - })()) + let key = K::from_lua(key, lua)?; + let value = V::from_lua(value, lua)?; + Ok((key, value)) + })()) } Err(e) => Some(Err(e)), } @@ -770,8 +770,8 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> { /// Add a regular method as a function which accepts a &mut T as the first parameter. pub fn add_method_mut(&mut self, name: &str, method: M) - where M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>) - -> Result> + where + M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>) -> Result>, { self.methods.insert( name.to_owned(), @@ -802,8 +802,8 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> { /// cause an error with certain binary metamethods that can trigger if ony the right side has a /// metatable. pub fn add_meta_method_mut(&mut self, meta: MetaMethod, method: M) - where M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>) - -> Result> + where + M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>) -> Result>, { self.meta_methods.insert(meta, Self::box_method_mut(method)); } @@ -837,8 +837,8 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> { } fn box_method_mut(mut method: M) -> Callback<'lua> - where M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>) - -> Result> + where + M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>) -> Result>, { Box::new(move |lua, mut args| if let Some(front) = args.pop_front() { let userdata = AnyUserData::from_lua(front, lua)?; @@ -880,9 +880,7 @@ impl<'lua> AnyUserData<'lua> { /// Borrow this userdata out of the internal RefCell that is held in lua. pub fn borrow(&self) -> Result> { self.inspect(|cell| { - Ok( - cell.try_borrow().map_err(|_| Error::UserDataBorrowError)?, - ) + Ok(cell.try_borrow().map_err(|_| Error::UserDataBorrowError)?) }).ok_or(Error::UserDataTypeMismatch)? } @@ -967,12 +965,18 @@ impl Lua { &LUA_USERDATA_REGISTRY_KEY as *const u8 as *mut c_void, ); - push_userdata::>>(state, RefCell::new(HashMap::new())); + push_userdata::>>( + state, + RefCell::new(HashMap::new()), + ); ffi::lua_newtable(state); push_string(state, "__gc"); - ffi::lua_pushcfunction(state, userdata_destructor::>>); + ffi::lua_pushcfunction( + state, + userdata_destructor::>>, + ); ffi::lua_rawset(state, -3); ffi::lua_setmetatable(state, -2); @@ -1293,10 +1297,7 @@ impl Lua { /// Unpacks a `MultiValue` instance into a value that implements `FromLuaMulti`. /// /// This can be used to convert the arguments of a Rust function called by Lua. - pub fn unpack<'lua, T: FromLuaMulti<'lua>>( - &'lua self, - value: MultiValue<'lua>, - ) -> Result { + pub fn unpack<'lua, T: FromLuaMulti<'lua>>(&'lua self, value: MultiValue<'lua>) -> Result { T::from_lua_multi(value, self) } @@ -1505,7 +1506,8 @@ impl Lua { &LUA_USERDATA_REGISTRY_KEY as *const u8 as *mut c_void, ); ffi::lua_gettable(self.state, ffi::LUA_REGISTRYINDEX); - let registered_userdata = &mut *get_userdata::>>(self.state, -1); + let registered_userdata = + &mut *get_userdata::>>(self.state, -1); let mut map = (*registered_userdata).borrow_mut(); ffi::lua_pop(self.state, 1); diff --git a/src/multi.rs b/src/multi.rs index 11c13fc..65051ce 100644 --- a/src/multi.rs +++ b/src/multi.rs @@ -1,4 +1,4 @@ -use std::result::{Result as StdResult}; +use std::result::Result as StdResult; use hlist_macro::{HNil, HCons}; @@ -108,7 +108,8 @@ impl<'lua, T: FromLuaMulti<'lua>> FromLuaMulti<'lua> for HCons { } impl<'lua, H: ToLua<'lua>, A, B> ToLuaMulti<'lua> for HCons> - where HCons: ToLuaMulti<'lua> +where + HCons: ToLuaMulti<'lua>, { fn to_lua_multi(self, lua: &'lua Lua) -> Result> { let mut results = self.1.to_lua_multi(lua)?; @@ -118,7 +119,8 @@ impl<'lua, H: ToLua<'lua>, A, B> ToLuaMulti<'lua> for HCons> } impl<'lua, H: FromLua<'lua>, A, B> FromLuaMulti<'lua> for HCons> - where HCons: FromLuaMulti<'lua> +where + HCons: FromLuaMulti<'lua>, { fn from_lua_multi(mut values: MultiValue<'lua>, lua: &'lua Lua) -> Result { let val = H::from_lua(values.pop_front().unwrap_or(Nil), lua)?; diff --git a/src/tests.rs b/src/tests.rs index 3c5b970..d7cbbfb 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -4,10 +4,8 @@ use std::panic::catch_unwind; use std::os::raw::c_void; use String as LuaString; -use { - Lua, Result, ExternalError, LightUserData, UserDataMethods, UserData, Table, Thread, - ThreadStatus, Error, Function, Value, Variadic, MetaMethod -}; +use {Lua, Result, ExternalError, LightUserData, UserDataMethods, UserData, Table, Thread, + ThreadStatus, Error, Function, Value, Variadic, MetaMethod}; #[test] fn test_set_get() { @@ -156,10 +154,7 @@ fn test_table() { .unwrap(); let table4 = globals.get::<_, Table>("table4").unwrap(); assert_eq!( - table4 - .pairs() - .collect::>>() - .unwrap(), + table4.pairs().collect::>>().unwrap(), vec![(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)] ); } @@ -521,13 +516,9 @@ fn test_error() { let lua_error = globals.get::<_, Function>("lua_error").unwrap(); let rust_error = globals.get::<_, Function>("rust_error").unwrap(); let return_error = globals.get::<_, Function>("return_error").unwrap(); - let return_string_error = globals - .get::<_, Function>("return_string_error") - .unwrap(); + let return_string_error = globals.get::<_, Function>("return_string_error").unwrap(); let test_pcall = globals.get::<_, Function>("test_pcall").unwrap(); - let understand_recursion = globals - .get::<_, Function>("understand_recursion") - .unwrap(); + let understand_recursion = globals.get::<_, Function>("understand_recursion").unwrap(); assert!(no_error.call::<_, ()>(()).is_ok()); match lua_error.call::<_, ()>(()) { @@ -828,7 +819,8 @@ fn test_expired_userdata() { globals.set("userdata", MyUserdata { id: 123 }).unwrap(); } - lua.eval::<()>(r#" + lua.eval::<()>( + r#" local tbl = setmetatable({ userdata = userdata }, { __gc = function(self) @@ -840,7 +832,9 @@ fn test_expired_userdata() { userdata = nil -- make table and userdata collectable collectgarbage("collect") hatch:access() - "#, None).unwrap(); + "#, + None, + ).unwrap(); } #[test] @@ -866,6 +860,6 @@ fn detroys_userdata() { } assert_eq!(DROPPED.load(Ordering::SeqCst), false); - drop(lua); // should destroy all objects + drop(lua); // should destroy all objects assert_eq!(DROPPED.load(Ordering::SeqCst), true); } diff --git a/src/util.rs b/src/util.rs index 5a049ee..a29a8c9 100644 --- a/src/util.rs +++ b/src/util.rs @@ -110,11 +110,7 @@ where // operation results in an error and the stack is smaller than the value before // the call, then this is unrecoverable and this will panic. If this function // panics, it will clear the stack before panicking. -pub unsafe fn stack_err_guard( - state: *mut ffi::lua_State, - change: c_int, - op: F, -) -> Result +pub unsafe fn stack_err_guard(state: *mut ffi::lua_State, change: c_int, op: F) -> Result where F: FnOnce() -> Result, { @@ -208,7 +204,11 @@ pub unsafe fn plen(state: *mut ffi::lua_State, index: c_int) -> Result Result { +pub unsafe fn pgeti( + state: *mut ffi::lua_State, + index: c_int, + i: ffi::lua_Integer, +) -> Result { unsafe extern "C" fn geti(state: *mut ffi::lua_State) -> c_int { let i = ffi::lua_tointeger(state, -1); ffi::lua_geti(state, -2, i); @@ -228,11 +228,7 @@ pub unsafe fn pgeti(state: *mut ffi::lua_State, index: c_int, i: ffi::lua_Intege // Protected version of lua_next, uses 3 stack spaces, does not call checkstack. pub unsafe fn pnext(state: *mut ffi::lua_State, index: c_int) -> Result { unsafe extern "C" fn next(state: *mut ffi::lua_State) -> c_int { - if ffi::lua_next(state, -2) == 0 { - 0 - } else { - 2 - } + if ffi::lua_next(state, -2) == 0 { 0 } else { 2 } } let table_index = ffi::lua_absindex(state, index); @@ -245,11 +241,7 @@ pub unsafe fn pnext(state: *mut ffi::lua_State, index: c_int) -> Result { let stack_start = ffi::lua_gettop(state) - 3; handle_error(state, pcall_with_traceback(state, 2, ffi::LUA_MULTRET))?; let nresults = ffi::lua_gettop(state) - stack_start; - if nresults == 0 { - Ok(0) - } else { - Ok(1) - } + if nresults == 0 { Ok(0) } else { Ok(1) } } // If the return code indicates an error, pops the error off of the stack and