auto formatting
This commit is contained in:
parent
1eaa201441
commit
69fa01df45
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
|
44
src/lua.rs
44
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<M>(&mut self, name: &str, method: M)
|
||||
where M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>)
|
||||
-> Result<MultiValue<'lua>>
|
||||
where
|
||||
M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>) -> Result<MultiValue<'lua>>,
|
||||
{
|
||||
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<M>(&mut self, meta: MetaMethod, method: M)
|
||||
where M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>)
|
||||
-> Result<MultiValue<'lua>>
|
||||
where
|
||||
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));
|
||||
}
|
||||
|
@ -837,8 +837,8 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> {
|
|||
}
|
||||
|
||||
fn box_method_mut<M>(mut method: M) -> Callback<'lua>
|
||||
where M: 'lua + for<'a> FnMut(&'lua Lua, &'a mut T, MultiValue<'lua>)
|
||||
-> Result<MultiValue<'lua>>
|
||||
where
|
||||
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() {
|
||||
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<T: UserData>(&self) -> Result<Ref<T>> {
|
||||
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::<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);
|
||||
|
||||
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_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<T> {
|
||||
pub fn unpack<'lua, T: FromLuaMulti<'lua>>(&'lua self, value: MultiValue<'lua>) -> Result<T> {
|
||||
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::<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();
|
||||
ffi::lua_pop(self.state, 1);
|
||||
|
||||
|
|
|
@ -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<T, HNil> {
|
|||
}
|
||||
|
||||
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>> {
|
||||
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>>
|
||||
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> {
|
||||
let val = H::from_lua(values.pop_front().unwrap_or(Nil), lua)?;
|
||||
|
|
28
src/tests.rs
28
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::<Result<Vec<(i64, i64)>>>()
|
||||
.unwrap(),
|
||||
table4.pairs().collect::<Result<Vec<(i64, i64)>>>().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);
|
||||
}
|
||||
|
|
24
src/util.rs
24
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<F, R>(
|
||||
state: *mut ffi::lua_State,
|
||||
change: c_int,
|
||||
op: F,
|
||||
) -> Result<R>
|
||||
pub unsafe fn stack_err_guard<F, R>(state: *mut ffi::lua_State, change: c_int, op: F) -> Result<R>
|
||||
where
|
||||
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.
|
||||
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 {
|
||||
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<c_int> {
|
||||
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<c_int> {
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue