auto formatting

This commit is contained in:
kyren 2017-07-24 10:40:00 -04:00
parent 1eaa201441
commit 69fa01df45
5 changed files with 48 additions and 60 deletions

View File

@ -54,9 +54,7 @@ impl fmt::Display for Error {
Error::UserDataTypeMismatch => write!(fmt, "Userdata not expected type"), Error::UserDataTypeMismatch => write!(fmt, "Userdata not expected type"),
Error::UserDataBorrowError => write!(fmt, "Userdata already mutably borrowed"), Error::UserDataBorrowError => write!(fmt, "Userdata already mutably borrowed"),
Error::UserDataBorrowMutError => write!(fmt, "Userdata already borrowed"), Error::UserDataBorrowMutError => write!(fmt, "Userdata already borrowed"),
Error::CallbackError(ref msg, _) => { Error::CallbackError(ref msg, _) => write!(fmt, "Error during lua callback: {}", msg),
write!(fmt, "Error during lua callback: {}", msg)
}
Error::ExternalError(ref err) => err.fmt(fmt), Error::ExternalError(ref err) => err.fmt(fmt),
} }
} }

View File

@ -48,7 +48,7 @@ pub enum Value<'lua> {
/// it is implicitly cloned. /// it is implicitly cloned.
Error(Error), Error(Error),
} }
pub use self::Value::Nil as Nil; pub use self::Value::Nil;
/// Trait for types convertible to `Value`. /// Trait for types convertible to `Value`.
pub trait ToLua<'a> { pub trait ToLua<'a> {
@ -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. /// Add a regular method as a function which accepts a &mut T as the first parameter.
pub fn add_method_mut<M>(&mut self, name: &str, method: M) pub fn add_method_mut<M>(&mut self, name: &str, method: M)
where M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>) where
-> Result<MultiValue<'lua>> M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>) -> Result<MultiValue<'lua>>,
{ {
self.methods.insert( self.methods.insert(
name.to_owned(), 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 /// cause an error with certain binary metamethods that can trigger if ony the right side has a
/// metatable. /// metatable.
pub fn add_meta_method_mut<M>(&mut self, meta: MetaMethod, method: M) pub fn add_meta_method_mut<M>(&mut self, meta: MetaMethod, method: M)
where M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>) where
-> Result<MultiValue<'lua>> M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>) -> Result<MultiValue<'lua>>,
{ {
self.meta_methods.insert(meta, Self::box_method_mut(method)); 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<M>(mut method: M) -> Callback<'lua> fn box_method_mut<M>(mut method: M) -> Callback<'lua>
where M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>) where
-> Result<MultiValue<'lua>> M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>) -> Result<MultiValue<'lua>>,
{ {
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)?;
@ -880,9 +880,7 @@ impl<'lua> AnyUserData<'lua> {
/// Borrow this userdata out of the internal RefCell that is held in lua. /// Borrow this userdata out of the internal RefCell that is held in lua.
pub fn borrow<T: UserData>(&self) -> Result<Ref<T>> { pub fn borrow<T: UserData>(&self) -> Result<Ref<T>> {
self.inspect(|cell| { self.inspect(|cell| {
Ok( Ok(cell.try_borrow().map_err(|_| Error::UserDataBorrowError)?)
cell.try_borrow().map_err(|_| Error::UserDataBorrowError)?,
)
}).ok_or(Error::UserDataTypeMismatch)? }).ok_or(Error::UserDataTypeMismatch)?
} }
@ -967,12 +965,18 @@ impl Lua {
&LUA_USERDATA_REGISTRY_KEY as *const u8 as *mut c_void, &LUA_USERDATA_REGISTRY_KEY as *const u8 as *mut c_void,
); );
push_userdata::<RefCell<HashMap<TypeId, c_int>>>(state, RefCell::new(HashMap::new())); push_userdata::<RefCell<HashMap<TypeId, c_int>>>(
state,
RefCell::new(HashMap::new()),
);
ffi::lua_newtable(state); ffi::lua_newtable(state);
push_string(state, "__gc"); push_string(state, "__gc");
ffi::lua_pushcfunction(state, userdata_destructor::<RefCell<HashMap<TypeId, c_int>>>); ffi::lua_pushcfunction(
state,
userdata_destructor::<RefCell<HashMap<TypeId, c_int>>>,
);
ffi::lua_rawset(state, -3); ffi::lua_rawset(state, -3);
ffi::lua_setmetatable(state, -2); ffi::lua_setmetatable(state, -2);
@ -1293,10 +1297,7 @@ impl Lua {
/// Unpacks a `MultiValue` instance into a value that implements `FromLuaMulti`. /// 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. /// This can be used to convert the arguments of a Rust function called by Lua.
pub fn unpack<'lua, T: FromLuaMulti<'lua>>( pub fn unpack<'lua, T: FromLuaMulti<'lua>>(&'lua self, value: MultiValue<'lua>) -> Result<T> {
&'lua self,
value: MultiValue<'lua>,
) -> Result<T> {
T::from_lua_multi(value, self) T::from_lua_multi(value, self)
} }
@ -1505,7 +1506,8 @@ impl Lua {
&LUA_USERDATA_REGISTRY_KEY as *const u8 as *mut c_void, &LUA_USERDATA_REGISTRY_KEY as *const u8 as *mut c_void,
); );
ffi::lua_gettable(self.state, ffi::LUA_REGISTRYINDEX); ffi::lua_gettable(self.state, ffi::LUA_REGISTRYINDEX);
let registered_userdata = &mut *get_userdata::<RefCell<HashMap<TypeId, c_int>>>(self.state, -1); let registered_userdata =
&mut *get_userdata::<RefCell<HashMap<TypeId, c_int>>>(self.state, -1);
let mut map = (*registered_userdata).borrow_mut(); let mut map = (*registered_userdata).borrow_mut();
ffi::lua_pop(self.state, 1); ffi::lua_pop(self.state, 1);

View File

@ -1,4 +1,4 @@
use std::result::{Result as StdResult}; use std::result::Result as StdResult;
use hlist_macro::{HNil, HCons}; use hlist_macro::{HNil, HCons};
@ -108,7 +108,8 @@ impl<'lua, T: FromLuaMulti<'lua>> FromLuaMulti<'lua> for HCons<T, HNil> {
} }
impl<'lua, H: ToLua<'lua>, A, B> ToLuaMulti<'lua> for HCons<H, HCons<A, B>> impl<'lua, H: ToLua<'lua>, A, B> ToLuaMulti<'lua> for HCons<H, HCons<A, B>>
where HCons<A, B>: ToLuaMulti<'lua> where
HCons<A, B>: ToLuaMulti<'lua>,
{ {
fn to_lua_multi(self, lua: &'lua Lua) -> Result<MultiValue<'lua>> { fn to_lua_multi(self, lua: &'lua Lua) -> Result<MultiValue<'lua>> {
let mut results = self.1.to_lua_multi(lua)?; let mut results = self.1.to_lua_multi(lua)?;
@ -118,7 +119,8 @@ impl<'lua, H: ToLua<'lua>, A, B> ToLuaMulti<'lua> for HCons<H, HCons<A, B>>
} }
impl<'lua, H: FromLua<'lua>, A, B> FromLuaMulti<'lua> for HCons<H, HCons<A, B>> impl<'lua, H: FromLua<'lua>, A, B> FromLuaMulti<'lua> for HCons<H, HCons<A, B>>
where HCons<A, B>: FromLuaMulti<'lua> where
HCons<A, B>: FromLuaMulti<'lua>,
{ {
fn from_lua_multi(mut values: MultiValue<'lua>, lua: &'lua Lua) -> Result<Self> { fn from_lua_multi(mut values: MultiValue<'lua>, lua: &'lua Lua) -> Result<Self> {
let val = H::from_lua(values.pop_front().unwrap_or(Nil), lua)?; let val = H::from_lua(values.pop_front().unwrap_or(Nil), lua)?;

View File

@ -4,10 +4,8 @@ use std::panic::catch_unwind;
use std::os::raw::c_void; use std::os::raw::c_void;
use String as LuaString; use String as LuaString;
use { use {Lua, Result, ExternalError, LightUserData, UserDataMethods, UserData, Table, Thread,
Lua, Result, ExternalError, LightUserData, UserDataMethods, UserData, Table, Thread, ThreadStatus, Error, Function, Value, Variadic, MetaMethod};
ThreadStatus, Error, Function, Value, Variadic, MetaMethod
};
#[test] #[test]
fn test_set_get() { fn test_set_get() {
@ -156,10 +154,7 @@ fn test_table() {
.unwrap(); .unwrap();
let table4 = globals.get::<_, Table>("table4").unwrap(); let table4 = globals.get::<_, Table>("table4").unwrap();
assert_eq!( assert_eq!(
table4 table4.pairs().collect::<Result<Vec<(i64, i64)>>>().unwrap(),
.pairs()
.collect::<Result<Vec<(i64, i64)>>>()
.unwrap(),
vec![(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)] 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 lua_error = globals.get::<_, Function>("lua_error").unwrap();
let rust_error = globals.get::<_, Function>("rust_error").unwrap(); let rust_error = globals.get::<_, Function>("rust_error").unwrap();
let return_error = globals.get::<_, Function>("return_error").unwrap(); let return_error = globals.get::<_, Function>("return_error").unwrap();
let return_string_error = globals let return_string_error = globals.get::<_, Function>("return_string_error").unwrap();
.get::<_, Function>("return_string_error")
.unwrap();
let test_pcall = globals.get::<_, Function>("test_pcall").unwrap(); let test_pcall = globals.get::<_, Function>("test_pcall").unwrap();
let understand_recursion = globals let understand_recursion = globals.get::<_, Function>("understand_recursion").unwrap();
.get::<_, Function>("understand_recursion")
.unwrap();
assert!(no_error.call::<_, ()>(()).is_ok()); assert!(no_error.call::<_, ()>(()).is_ok());
match lua_error.call::<_, ()>(()) { match lua_error.call::<_, ()>(()) {
@ -828,7 +819,8 @@ fn test_expired_userdata() {
globals.set("userdata", MyUserdata { id: 123 }).unwrap(); globals.set("userdata", MyUserdata { id: 123 }).unwrap();
} }
lua.eval::<()>(r#" lua.eval::<()>(
r#"
local tbl = setmetatable({ local tbl = setmetatable({
userdata = userdata userdata = userdata
}, { __gc = function(self) }, { __gc = function(self)
@ -840,7 +832,9 @@ fn test_expired_userdata() {
userdata = nil -- make table and userdata collectable userdata = nil -- make table and userdata collectable
collectgarbage("collect") collectgarbage("collect")
hatch:access() hatch:access()
"#, None).unwrap(); "#,
None,
).unwrap();
} }
#[test] #[test]

View File

@ -110,11 +110,7 @@ where
// operation results in an error and the stack is smaller than the value before // 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 // the call, then this is unrecoverable and this will panic. If this function
// panics, it will clear the stack before panicking. // panics, it will clear the stack before panicking.
pub unsafe fn stack_err_guard<F, R>( pub unsafe fn stack_err_guard<F, R>(state: *mut ffi::lua_State, change: c_int, op: F) -> Result<R>
state: *mut ffi::lua_State,
change: c_int,
op: F,
) -> Result<R>
where where
F: FnOnce() -> Result<R>, F: FnOnce() -> Result<R>,
{ {
@ -208,7 +204,11 @@ pub unsafe fn plen(state: *mut ffi::lua_State, index: c_int) -> Result<ffi::lua_
} }
// Protected version of lua_geti, uses 3 stack spaces, does not call checkstack. // Protected version of lua_geti, uses 3 stack spaces, does not call checkstack.
pub unsafe fn pgeti(state: *mut ffi::lua_State, index: c_int, i: ffi::lua_Integer) -> Result<c_int> { pub unsafe fn pgeti(
state: *mut ffi::lua_State,
index: c_int,
i: ffi::lua_Integer,
) -> Result<c_int> {
unsafe extern "C" fn geti(state: *mut ffi::lua_State) -> c_int { unsafe extern "C" fn geti(state: *mut ffi::lua_State) -> c_int {
let i = ffi::lua_tointeger(state, -1); let i = ffi::lua_tointeger(state, -1);
ffi::lua_geti(state, -2, i); 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. // 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<c_int> { pub unsafe fn pnext(state: *mut ffi::lua_State, index: c_int) -> Result<c_int> {
unsafe extern "C" fn next(state: *mut ffi::lua_State) -> c_int { unsafe extern "C" fn next(state: *mut ffi::lua_State) -> c_int {
if ffi::lua_next(state, -2) == 0 { if ffi::lua_next(state, -2) == 0 { 0 } else { 2 }
0
} else {
2
}
} }
let table_index = ffi::lua_absindex(state, index); 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<c_int> {
let stack_start = ffi::lua_gettop(state) - 3; let stack_start = ffi::lua_gettop(state) - 3;
handle_error(state, pcall_with_traceback(state, 2, ffi::LUA_MULTRET))?; handle_error(state, pcall_with_traceback(state, 2, ffi::LUA_MULTRET))?;
let nresults = ffi::lua_gettop(state) - stack_start; let nresults = ffi::lua_gettop(state) - stack_start;
if nresults == 0 { if nresults == 0 { Ok(0) } else { Ok(1) }
Ok(0)
} else {
Ok(1)
}
} }
// If the return code indicates an error, pops the error off of the stack and // If the return code indicates an error, pops the error off of the stack and