summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/examples.rs19
-rw-r--r--examples/repl.rs7
-rw-r--r--src/conversion.rs6
-rw-r--r--src/ffi.rs22
-rw-r--r--src/lib.rs10
-rw-r--r--src/lua.rs24
-rw-r--r--src/prelude.rs16
-rw-r--r--src/table.rs16
-rw-r--r--src/tests.rs27
-rw-r--r--src/types.rs5
-rw-r--r--src/userdata.rs68
-rw-r--r--src/util.rs17
12 files changed, 114 insertions, 123 deletions
diff --git a/examples/examples.rs b/examples/examples.rs
index 51c21a6..3726428 100644
--- a/examples/examples.rs
+++ b/examples/examples.rs
@@ -2,7 +2,7 @@ extern crate rlua;
use std::f32;
-use rlua::{Lua, Result, Function, Variadic, UserData, UserDataMethods, MetaMethod};
+use rlua::{Function, Lua, MetaMethod, Result, UserData, UserDataMethods, Variadic};
fn examples() -> Result<()> {
// Create a Lua context with Lua::new(). Eventually, this will allow
@@ -103,17 +103,11 @@ fn examples() -> Result<()> {
globals.set("join", join)?;
assert_eq!(
- lua.eval::<bool>(
- r#"check_equal({"a", "b", "c"}, {"a", "b", "c"})"#,
- None,
- )?,
+ lua.eval::<bool>(r#"check_equal({"a", "b", "c"}, {"a", "b", "c"})"#, None)?,
true
);
assert_eq!(
- lua.eval::<bool>(
- r#"check_equal({"a", "b", "c"}, {"d", "e", "f"})"#,
- None,
- )?,
+ lua.eval::<bool>(r#"check_equal({"a", "b", "c"}, {"d", "e", "f"})"#, None)?,
false
);
assert_eq!(lua.eval::<String>(r#"join("a", "b", "c")"#, None)?, "abc");
@@ -141,12 +135,7 @@ fn examples() -> Result<()> {
let vec2_constructor = lua.create_function(|_, (x, y): (f32, f32)| Ok(Vec2(x, y)));
globals.set("vec2", vec2_constructor)?;
- assert!(
- lua.eval::<f32>(
- "(vec2(1, 2) + vec2(2, 2)):magnitude()",
- None,
- )? - 5.0 < f32::EPSILON
- );
+ assert!(lua.eval::<f32>("(vec2(1, 2) + vec2(2, 2)):magnitude()", None)? - 5.0 < f32::EPSILON);
Ok(())
}
diff --git a/examples/repl.rs b/examples/repl.rs
index 710fd6d..ff634c7 100644
--- a/examples/repl.rs
+++ b/examples/repl.rs
@@ -3,7 +3,7 @@
extern crate rlua;
extern crate rustyline;
-use rlua::{Lua, MultiValue, Error};
+use rlua::{Error, Lua, MultiValue};
use rustyline::Editor;
fn main() {
@@ -33,7 +33,10 @@ fn main() {
);
break;
}
- Err(Error::SyntaxError { incomplete_input: true, .. }) => {
+ Err(Error::SyntaxError {
+ incomplete_input: true,
+ ..
+ }) => {
// continue reading input and append it to `line`
line.push_str("\n"); // separate input lines
prompt = ">> ";
diff --git a/src/conversion.rs b/src/conversion.rs
index 556c736..306249b 100644
--- a/src/conversion.rs
+++ b/src/conversion.rs
@@ -1,13 +1,13 @@
-use std::collections::{HashMap, BTreeMap};
+use std::collections::{BTreeMap, HashMap};
use std::hash::Hash;
use std::string::String as StdString;
use error::*;
-use types::{Integer, Number, LightUserData};
+use types::{Integer, LightUserData, Number};
use lua::*;
use string::String;
use table::Table;
-use userdata::{UserData, AnyUserData};
+use userdata::{AnyUserData, UserData};
impl<'lua> ToLua<'lua> for Value<'lua> {
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
diff --git a/src/ffi.rs b/src/ffi.rs
index a944afe..2d12311 100644
--- a/src/ffi.rs
+++ b/src/ffi.rs
@@ -4,22 +4,24 @@
#![allow(non_snake_case)]
use std::ptr;
-use std::os::raw::{c_void, c_char, c_int, c_longlong, c_double};
+use std::os::raw::{c_char, c_double, c_int, c_longlong, c_void};
pub type lua_Integer = c_longlong;
pub type lua_Number = c_double;
pub enum lua_State {}
-pub type lua_Alloc = unsafe extern "C" fn(ud: *mut c_void,
- ptr: *mut c_void,
- osize: usize,
- nsize: usize)
- -> *mut c_void;
+pub type lua_Alloc = unsafe extern "C" fn(
+ ud: *mut c_void,
+ ptr: *mut c_void,
+ osize: usize,
+ nsize: usize,
+) -> *mut c_void;
pub type lua_KContext = *mut c_void;
-pub type lua_KFunction = unsafe extern "C" fn(state: *mut lua_State,
- status: c_int,
- ctx: lua_KContext)
- -> c_int;
+pub type lua_KFunction = unsafe extern "C" fn(
+ state: *mut lua_State,
+ status: c_int,
+ ctx: lua_KContext,
+) -> c_int;
pub type lua_CFunction = unsafe extern "C" fn(state: *mut lua_State) -> c_int;
pub const LUA_OK: c_int = 0;
diff --git a/src/lib.rs b/src/lib.rs
index 052bcf3..12542ad 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -57,13 +57,13 @@ mod userdata;
#[cfg(test)]
mod tests;
-pub use error::{Error, Result, ExternalError, ExternalResult};
-pub use types::{Integer, Number, LightUserData};
+pub use error::{Error, ExternalError, ExternalResult, Result};
+pub use types::{Integer, LightUserData, Number};
pub use multi::Variadic;
pub use string::String;
pub use table::{Table, TablePairs, TableSequence};
-pub use userdata::{MetaMethod, UserDataMethods, UserData, AnyUserData};
-pub use lua::{Value, Nil, ToLua, FromLua, MultiValue, ToLuaMulti, FromLuaMulti, Function,
- ThreadStatus, Thread, Lua};
+pub use userdata::{AnyUserData, MetaMethod, UserData, UserDataMethods};
+pub use lua::{FromLua, FromLuaMulti, Function, Lua, MultiValue, Nil, Thread, ThreadStatus, ToLua,
+ ToLuaMulti, Value};
pub mod prelude;
diff --git a/src/lua.rs b/src/lua.rs
index 47eff11..12a623b 100644
--- a/src/lua.rs
+++ b/src/lua.rs
@@ -14,10 +14,10 @@ use libc;
use ffi;
use error::*;
use util::*;
-use types::{Integer, Number, LightUserData, Callback, LuaRef};
+use types::{Callback, Integer, LightUserData, LuaRef, Number};
use string::String;
use table::Table;
-use userdata::{UserDataMethods, MetaMethod, UserData, AnyUserData};
+use userdata::{AnyUserData, MetaMethod, UserData, UserDataMethods};
/// A dynamically typed Lua value.
#[derive(Debug, Clone)]
@@ -1018,17 +1018,15 @@ impl Lua {
ud
}
- ffi::LUA_TNUMBER => {
- if ffi::lua_isinteger(state, -1) != 0 {
- let i = Value::Integer(ffi::lua_tointeger(state, -1));
- ffi::lua_pop(state, 1);
- i
- } else {
- let n = Value::Number(ffi::lua_tonumber(state, -1));
- ffi::lua_pop(state, 1);
- n
- }
- }
+ ffi::LUA_TNUMBER => if ffi::lua_isinteger(state, -1) != 0 {
+ let i = Value::Integer(ffi::lua_tointeger(state, -1));
+ ffi::lua_pop(state, 1);
+ i
+ } else {
+ let n = Value::Number(ffi::lua_tonumber(state, -1));
+ ffi::lua_pop(state, 1);
+ n
+ },
ffi::LUA_TSTRING => Value::String(String(self.pop_ref(state))),
diff --git a/src/prelude.rs b/src/prelude.rs
index 71517a3..64f6394 100644
--- a/src/prelude.rs
+++ b/src/prelude.rs
@@ -1,10 +1,10 @@
//! Re-exports most types with an extra `Lua*` prefix to prevent name clashes.
-pub use {Error as LuaError, Result as LuaResult, ExternalError as LuaExternalError,
- ExternalResult as LuaExternalResult, Value as LuaValue, Nil as LuaNil, ToLua, FromLua,
- MultiValue as LuaMultiValue, ToLuaMulti, FromLuaMulti, Integer as LuaInteger,
- Number as LuaNumber, LightUserData as LuaLightUserData, String as LuaString,
- Table as LuaTable, TablePairs as LuaTablePairs, TableSequence as LuaTableSequence,
- Function as LuaFunction, ThreadStatus as LuaThreadStatus, Thread as LuaThread,
- MetaMethod as LuaMetaMethod, UserDataMethods as LuaUserDataMethods,
- UserData as LuaUserData, AnyUserData as LuaAnyUserData, Lua};
+pub use {AnyUserData as LuaAnyUserData, Error as LuaError, ExternalError as LuaExternalError,
+ ExternalResult as LuaExternalResult, FromLua, FromLuaMulti, Function as LuaFunction,
+ Integer as LuaInteger, LightUserData as LuaLightUserData, Lua,
+ MetaMethod as LuaMetaMethod, MultiValue as LuaMultiValue, Nil as LuaNil,
+ Number as LuaNumber, Result as LuaResult, String as LuaString, Table as LuaTable,
+ TablePairs as LuaTablePairs, TableSequence as LuaTableSequence, Thread as LuaThread,
+ ThreadStatus as LuaThreadStatus, ToLua, ToLuaMulti, UserData as LuaUserData,
+ UserDataMethods as LuaUserDataMethods, Value as LuaValue};
diff --git a/src/table.rs b/src/table.rs
index d74f04d..6642171 100644
--- a/src/table.rs
+++ b/src/table.rs
@@ -4,7 +4,7 @@ use ffi;
use error::Result;
use util::*;
use types::{Integer, LuaRef};
-use lua::{ToLua, FromLua};
+use lua::{FromLua, ToLua};
/// Handle to an internal Lua table.
#[derive(Clone, Debug)]
@@ -367,10 +367,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)),
}
@@ -433,7 +433,7 @@ where
mod tests {
use super::Table;
use error::Result;
- use lua::{Value, Nil, Lua};
+ use lua::{Lua, Nil, Value};
#[test]
fn test_set_get() {
@@ -588,7 +588,9 @@ mod tests {
let table = lua.create_table();
let metatable = lua.create_table();
- metatable.set("__index", lua.create_function(|_, ()| Ok("index_value"))).unwrap();
+ metatable
+ .set("__index", lua.create_function(|_, ()| Ok("index_value")))
+ .unwrap();
table.set_metatable(Some(metatable));
assert_eq!(table.get::<_, String>("any_key").unwrap(), "index_value");
match table.raw_get::<_, Value>("any_key").unwrap() {
diff --git a/src/tests.rs b/src/tests.rs
index 6e7ce53..954d809 100644
--- a/src/tests.rs
+++ b/src/tests.rs
@@ -2,7 +2,7 @@ use std::fmt;
use std::error;
use std::panic::catch_unwind;
-use {Lua, Result, ExternalError, Table, Thread, ThreadStatus, Error, Function, Value, Variadic};
+use {Error, ExternalError, Function, Lua, Result, Table, Thread, ThreadStatus, Value, Variadic};
#[test]
fn test_load() {
@@ -56,13 +56,14 @@ fn test_eval() {
assert_eq!(lua.eval::<bool>("false == false", None).unwrap(), true);
assert_eq!(lua.eval::<i32>("return 1 + 2", None).unwrap(), 3);
match lua.eval::<()>("if true then", None) {
- Err(Error::SyntaxError { incomplete_input: true, .. }) => {}
- r => {
- panic!(
- "expected SyntaxError with incomplete_input=true, got {:?}",
- r
- )
- }
+ Err(Error::SyntaxError {
+ incomplete_input: true,
+ ..
+ }) => {}
+ r => panic!(
+ "expected SyntaxError with incomplete_input=true, got {:?}",
+ r
+ ),
}
}
@@ -289,12 +290,18 @@ fn test_error() {
assert!(return_string_error.call::<_, Error>(()).is_ok());
match lua.eval::<()>("if youre happy and you know it syntax error", None) {
- Err(Error::SyntaxError { incomplete_input: false, .. }) => {}
+ Err(Error::SyntaxError {
+ incomplete_input: false,
+ ..
+ }) => {}
Err(_) => panic!("error is not LuaSyntaxError::Syntax kind"),
_ => panic!("error not returned"),
}
match lua.eval::<()>("function i_will_finish_what_i()", None) {
- Err(Error::SyntaxError { incomplete_input: true, .. }) => {}
+ Err(Error::SyntaxError {
+ incomplete_input: true,
+ ..
+ }) => {}
Err(_) => panic!("error is not LuaSyntaxError::IncompleteStatement kind"),
_ => panic!("error not returned"),
}
diff --git a/src/types.rs b/src/types.rs
index 6385ec8..a9cf2be 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -3,7 +3,7 @@ use std::os::raw::{c_int, c_void};
use ffi;
use error::Result;
-use lua::{MultiValue, Lua};
+use lua::{Lua, MultiValue};
/// Type of Lua integer numbers.
pub type Integer = ffi::lua_Integer;
@@ -15,8 +15,7 @@ pub type Number = ffi::lua_Number;
pub struct LightUserData(pub *mut c_void);
pub(crate) type Callback<'lua> = Box<
- FnMut(&'lua Lua, MultiValue<'lua>) -> Result<MultiValue<'lua>>
- + 'lua,
+ FnMut(&'lua Lua, MultiValue<'lua>) -> Result<MultiValue<'lua>> + 'lua,
>;
pub(crate) struct LuaRef<'lua> {
diff --git a/src/userdata.rs b/src/userdata.rs
index ebef232..8d52425 100644
--- a/src/userdata.rs
+++ b/src/userdata.rs
@@ -1,4 +1,4 @@
-use std::cell::{RefCell, Ref, RefMut};
+use std::cell::{Ref, RefCell, RefMut};
use std::marker::PhantomData;
use std::collections::HashMap;
use std::string::String as StdString;
@@ -7,7 +7,7 @@ use ffi;
use error::*;
use util::*;
use types::{Callback, LuaRef};
-use lua::{FromLua, FromLuaMulti, ToLuaMulti, Lua};
+use lua::{FromLua, FromLuaMulti, Lua, ToLuaMulti};
/// Kinds of metamethods that can be overridden.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
@@ -83,10 +83,8 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> {
R: ToLuaMulti<'lua>,
M: 'static + for<'a> FnMut(&'lua Lua, &'a T, A) -> Result<R>,
{
- self.methods.insert(
- name.to_owned(),
- Self::box_method(method),
- );
+ self.methods
+ .insert(name.to_owned(), Self::box_method(method));
}
/// Add a regular method which accepts a `&mut T` as the first parameter.
@@ -100,10 +98,8 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> {
R: ToLuaMulti<'lua>,
M: 'static + for<'a> FnMut(&'lua Lua, &'a mut T, A) -> Result<R>,
{
- self.methods.insert(
- name.to_owned(),
- Self::box_method_mut(method),
- );
+ self.methods
+ .insert(name.to_owned(), Self::box_method_mut(method));
}
/// Add a regular method as a function which accepts generic arguments, the first argument will
@@ -119,10 +115,8 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> {
R: ToLuaMulti<'lua>,
F: 'static + FnMut(&'lua Lua, A) -> Result<R>,
{
- self.methods.insert(
- name.to_owned(),
- Self::box_function(function),
- );
+ self.methods
+ .insert(name.to_owned(), Self::box_function(function));
}
/// Add a metamethod which accepts a `&T` as the first parameter.
@@ -180,9 +174,7 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> {
F: 'static + FnMut(&'lua Lua, A) -> Result<R>,
{
Box::new(move |lua, args| {
- function(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi(
- lua,
- )
+ function(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua)
})
}
@@ -195,8 +187,7 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> {
Box::new(move |lua, mut args| if let Some(front) = args.pop_front() {
let userdata = AnyUserData::from_lua(front, lua)?;
let userdata = userdata.borrow::<T>()?;
- method(lua, &userdata, A::from_lua_multi(args, lua)?)?
- .to_lua_multi(lua)
+ method(lua, &userdata, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua)
} else {
Err(Error::FromLuaConversionError {
from: "missing argument",
@@ -215,8 +206,7 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> {
Box::new(move |lua, mut args| if let Some(front) = args.pop_front() {
let userdata = AnyUserData::from_lua(front, lua)?;
let mut userdata = userdata.borrow_mut::<T>()?;
- method(lua, &mut userdata, A::from_lua_multi(args, lua)?)?
- .to_lua_multi(lua)
+ method(lua, &mut userdata, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua)
} else {
Err(Error::FromLuaConversionError {
from: "missing argument",
@@ -351,9 +341,8 @@ impl<'lua> AnyUserData<'lua> {
/// `UserDataTypeMismatch` if the userdata is not of type `T`.
pub fn borrow_mut<T: UserData>(&self) -> Result<RefMut<T>> {
self.inspect(|cell| {
- Ok(cell.try_borrow_mut().map_err(
- |_| Error::UserDataBorrowMutError,
- )?)
+ Ok(cell.try_borrow_mut()
+ .map_err(|_| Error::UserDataBorrowMutError)?)
}).ok_or(Error::UserDataTypeMismatch)?
}
@@ -396,7 +385,7 @@ impl<'lua> AnyUserData<'lua> {
#[cfg(test)]
mod tests {
- use super::{UserData, MetaMethod, UserDataMethods};
+ use super::{MetaMethod, UserData, UserDataMethods};
use error::ExternalError;
use string::String;
use lua::{Function, Lua};
@@ -470,24 +459,21 @@ mod tests {
impl UserData for MyUserData {
fn add_methods(methods: &mut UserDataMethods<Self>) {
methods.add_method("get", |_, data, ()| Ok(data.0));
- methods.add_meta_function(MetaMethod::Add, |_,
- (lhs, rhs): (MyUserData,
- MyUserData)| {
- Ok(MyUserData(lhs.0 + rhs.0))
- });
- methods.add_meta_function(MetaMethod::Sub, |_,
- (lhs, rhs): (MyUserData,
- MyUserData)| {
- Ok(MyUserData(lhs.0 - rhs.0))
- });
- methods.add_meta_method(
- MetaMethod::Index,
- |_, data, index: String| if index.to_str()? == "inner" {
+ methods
+ .add_meta_function(MetaMethod::Add, |_, (lhs, rhs): (MyUserData, MyUserData)| {
+ Ok(MyUserData(lhs.0 + rhs.0))
+ });
+ methods
+ .add_meta_function(MetaMethod::Sub, |_, (lhs, rhs): (MyUserData, MyUserData)| {
+ Ok(MyUserData(lhs.0 - rhs.0))
+ });
+ methods.add_meta_method(MetaMethod::Index, |_, data, index: String| {
+ if index.to_str()? == "inner" {
Ok(data.0)
} else {
Err("no such custom index".to_lua_err())
- },
- );
+ }
+ });
}
}
@@ -554,7 +540,7 @@ mod tests {
#[test]
fn detroys_userdata() {
- use std::sync::atomic::{Ordering, AtomicBool, ATOMIC_BOOL_INIT};
+ use std::sync::atomic::{AtomicBool, Ordering, ATOMIC_BOOL_INIT};
static DROPPED: AtomicBool = ATOMIC_BOOL_INIT;
diff --git a/src/util.rs b/src/util.rs
index 8d03036..9ee5b0d 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -8,7 +8,7 @@ use std::os::raw::{c_char, c_int, c_void};
use std::panic::{catch_unwind, resume_unwind, UnwindSafe};
use ffi;
-use error::{Result, Error};
+use error::{Error, Result};
macro_rules! cstr {
($s:expr) => (
@@ -228,7 +228,11 @@ pub unsafe fn pgeti(
// 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);
@@ -241,7 +245,11 @@ 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
@@ -255,7 +263,6 @@ pub unsafe fn handle_error(state: *mut ffi::lua_State, err: c_int) -> Result<()>
} else {
if let Some(err) = pop_wrapped_error(state) {
Err(err)
-
} else if is_wrapped_panic(state, -1) {
let panic = get_userdata::<WrappedPanic>(state, -1);
if let Some(p) = (*panic).0.take() {
@@ -264,7 +271,6 @@ pub unsafe fn handle_error(state: *mut ffi::lua_State, err: c_int) -> Result<()>
} else {
lua_panic!(state, "internal error: panic was resumed twice")
}
-
} else {
let err_string =
if let Some(s) = ffi::lua_tolstring(state, -1, ptr::null_mut()).as_ref() {
@@ -556,7 +562,6 @@ pub unsafe fn push_wrapped_error(state: *mut ffi::lua_State, err: Error) {
ffi::lua_remove(state, -2);
Ok(1)
-
} else {
panic!("internal error: userdata mismatch in Error metamethod");
})