Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/async_http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ impl UserData for BodyReader {
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
// Every call returns a next chunk
methods.add_async_method_mut("read", |lua, mut reader, ()| async move {
if let Some(bytes) = reader.0.frame().await {
if let Some(bytes) = bytes.into_lua_err()?.data_ref() {
return Some(lua.create_string(&bytes)).transpose();
}
if let Some(bytes) = reader.0.frame().await
&& let Some(bytes) = bytes.into_lua_err()?.data_ref()
{
return Some(lua.create_string(bytes)).transpose();
}
Ok(None)
});
Expand Down
14 changes: 6 additions & 8 deletions examples/guided_tour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn main() -> Result<()> {
assert_eq!(globals.get::<String>("global")?, "foobar");

assert_eq!(lua.load("1 + 1").eval::<i32>()?, 2);
assert_eq!(lua.load("false == false").eval::<bool>()?, true);
assert!(lua.load("false == false").eval::<bool>()?);
assert_eq!(lua.load("return 1 + 2").eval::<i32>()?, 3);

// Use can use special `chunk!` macro to use Rust tokenizer and automatically capture variables
Expand Down Expand Up @@ -119,15 +119,13 @@ fn main() -> Result<()> {
})?;
globals.set("join", join)?;

assert_eq!(
assert!(
lua.load(r#"check_equal({"a", "b", "c"}, {"a", "b", "c"})"#)
.eval::<bool>()?,
true
.eval::<bool>()?
);
assert_eq!(
lua.load(r#"check_equal({"a", "b", "c"}, {"d", "e", "f"})"#)
.eval::<bool>()?,
false
assert!(
!lua.load(r#"check_equal({"a", "b", "c"}, {"d", "e", "f"})"#)
.eval::<bool>()?
);
assert_eq!(lua.load(r#"join("a", "b", "c")"#).eval::<String>()?, "abc");

Expand Down
4 changes: 2 additions & 2 deletions examples/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() {
match lua.load(&line).eval::<MultiValue>() {
Ok(values) => {
editor.add_history_entry(line).unwrap();
if values.len() > 0 {
if !values.is_empty() {
println!(
"{}",
values
Expand All @@ -37,7 +37,7 @@ fn main() {
..
}) => {
// continue reading input and append it to `line`
line.push_str("\n"); // separate input lines
line.push('\n'); // separate input lines
prompt = ">> ";
}
Err(e) => {
Expand Down
6 changes: 2 additions & 4 deletions src/state/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,8 @@ impl RawLua {
},
);
#[cfg(feature = "luau-jit")]
if status == ffi::LUA_OK {
if (*self.extra.get()).enable_jit && ffi::luau_codegen_supported() != 0 {
ffi::luau_codegen_compile(state, -1);
}
if status == ffi::LUA_OK && (*self.extra.get()).enable_jit && ffi::luau_codegen_supported() != 0 {
ffi::luau_codegen_compile(state, -1);
}
status
}
Expand Down
4 changes: 2 additions & 2 deletions tests/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ async fn test_async_call() -> Result<()> {
assert_eq!(hello.call_async::<String>("alex").await?, "hello, alex!");

// Executing non-async functions using async call is allowed
let sum = lua.create_function(|_lua, (a, b): (i64, i64)| return Ok(a + b))?;
let sum = lua.create_function(|_lua, (a, b): (i64, i64)| Ok(a + b))?;
assert_eq!(sum.call_async::<i64>((5, 1)).await?, 6);

Ok(())
Expand Down Expand Up @@ -230,7 +230,7 @@ async fn test_async_return_async_closure() -> Result<()> {

let g = lua.create_async_function(move |_, b: i64| async move {
sleep_ms(10).await;
return Ok(a + b);
Ok(a + b)
})?;

Ok(g)
Expand Down
2 changes: 1 addition & 1 deletion tests/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn test_buffer() -> Result<()> {

// Check that we can pass buffer type to Lua
let buf1 = buf1.as_buffer().unwrap();
let func = lua.create_function(|_, buf: Value| return buf.to_string())?;
let func = lua.create_function(|_, buf: Value| buf.to_string())?;
assert!(func.call::<String>(buf1)?.starts_with("buffer:"));

// Check buffer methods
Expand Down
8 changes: 4 additions & 4 deletions tests/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn test_chunk_path() -> Result<()> {

// &Path
assert_eq!(
(lua.load(&*temp_dir.path().join("module.lua").as_path())).eval::<i32>()?,
(lua.load(temp_dir.path().join("module.lua").as_path())).eval::<i32>()?,
321
);

Expand All @@ -63,14 +63,14 @@ fn test_chunk_impls() -> Result<()> {

// StdString
assert_eq!(lua.load(String::from("1")).eval::<i32>()?, 1);
assert_eq!(lua.load(&String::from("2")).eval::<i32>()?, 2);
assert_eq!(lua.load(String::from("2")).eval::<i32>()?, 2);

// &[u8]
assert_eq!(lua.load(&b"3"[..]).eval::<i32>()?, 3);

// Vec<u8>
assert_eq!(lua.load(b"4".to_vec()).eval::<i32>()?, 4);
assert_eq!(lua.load(&b"5".to_vec()).eval::<i32>()?, 5);
assert_eq!(lua.load(b"5".to_vec()).eval::<i32>()?, 5);

Ok(())
}
Expand Down Expand Up @@ -172,7 +172,7 @@ fn test_compiler_library_constants() {
let lua = Lua::new();
lua.set_compiler(compiler);
let const_bool = lua.load("return mylib.const_bool").eval::<bool>().unwrap();
assert_eq!(const_bool, true);
assert!(const_bool);
let const_num = lua.load("return mylib.const_num").eval::<f64>().unwrap();
assert_eq!(const_num, 123.0);
let const_vec = lua.load("return mylib.const_vec").eval::<Vector>().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion tests/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ fn test_bool_into_lua() -> Result<()> {
// Push into stack
let table = lua.create_table()?;
table.set("b", true)?;
assert_eq!(true, table.get::<bool>("b")?);
assert!(table.get::<bool>("b")?);

Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions tests/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn test_error_context() -> Result<()> {

// Rewrite context message and test `downcast_ref`
let func3 = lua.create_function(|_, ()| {
Err::<(), _>(Error::external(io::Error::new(io::ErrorKind::Other, "other")))
Err::<(), _>(Error::external(io::Error::other("other")))
.context("some context")
.context("some new context")
})?;
Expand All @@ -52,11 +52,11 @@ fn test_error_chain() -> Result<()> {
let lua = Lua::new();

// Check that `Error::ExternalError` creates a chain with a single element
let io_err = io::Error::new(io::ErrorKind::Other, "other");
let io_err = io::Error::other("other");
assert_eq!(Error::external(io_err).chain().count(), 1);

let func = lua.create_function(|_, ()| {
let err = Error::external(io::Error::new(io::ErrorKind::Other, "other")).context("io error");
let err = Error::external(io::Error::other("other")).context("io error");
Err::<(), _>(err)
})?;
let err = func.call::<()>(()).unwrap_err();
Expand Down
6 changes: 3 additions & 3 deletions tests/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ fn test_function_info() -> Result<()> {
let func_with_upvalues_info = func_with_upvalues.info();
assert_eq!(func_with_upvalues_info.num_upvalues, 2);
assert_eq!(func_with_upvalues_info.num_params, 1);
assert_eq!(func_with_upvalues_info.is_vararg, true);
assert!(func_with_upvalues_info.is_vararg);
}

Ok(())
Expand All @@ -227,7 +227,7 @@ fn test_function_dump() -> Result<()> {
let concat_lua = lua
.load(r#"function(arg1, arg2) return arg1 .. arg2 end"#)
.eval::<Function>()?;
let concat = lua.load(&concat_lua.dump(false)).into_function()?;
let concat = lua.load(concat_lua.dump(false)).into_function()?;

assert_eq!(concat.call::<String>(("foo", "bar"))?, "foobar");

Expand Down Expand Up @@ -417,7 +417,7 @@ fn test_function_wrap() -> Result<()> {
// Check recursive mut callback error
let fmut = Function::wrap_mut(|f: Function| match f.call::<()>(&f) {
Err(Error::CallbackError { cause, .. }) => match cause.as_ref() {
Error::RecursiveMutCallback { .. } => Ok::<_, Error>(()),
Error::RecursiveMutCallback => Ok::<_, Error>(()),
other => panic!("incorrect result: {other:?}"),
},
other => panic!("incorrect result: {other:?}"),
Expand Down
2 changes: 1 addition & 1 deletion tests/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn test_multivalue_by_ref() -> Result<()> {
let f = lua.create_function(|_, (i, s, b): (i32, LuaString, bool)| {
assert_eq!(i, 3);
assert_eq!(s.to_str()?, "hello");
assert_eq!(b, true);
assert!(b);
Ok(())
})?;
f.call::<()>(&multi)?;
Expand Down
21 changes: 9 additions & 12 deletions tests/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,18 @@ fn test_serialize_failure() -> Result<(), Box<dyn StdError>> {
let lua = Lua::new();

let ud = Value::UserData(lua.create_userdata(MyUserData(123))?);
match serde_json::to_value(&ud) {
Ok(v) => panic!("expected serialization error, got {}", v),
Err(serde_json::Error { .. }) => {}
if let Ok(v) = serde_json::to_value(&ud) {
panic!("expected serialization error, got {}", v)
}

let func = lua.create_function(|_, _: ()| Ok(()))?;
match serde_json::to_value(&Value::Function(func.clone())) {
Ok(v) => panic!("expected serialization error, got {}", v),
Err(serde_json::Error { .. }) => {}
if let Ok(v) = serde_json::to_value(Value::Function(func.clone())) {
panic!("expected serialization error, got {}", v)
}

let thr = lua.create_thread(func)?;
match serde_json::to_value(&Value::Thread(thr)) {
Ok(v) => panic!("expected serialization error, got {}", v),
Err(serde_json::Error { .. }) => {}
if let Ok(v) = serde_json::to_value(Value::Thread(thr)) {
panic!("expected serialization error, got {}", v)
}

Ok(())
Expand Down Expand Up @@ -822,12 +819,12 @@ fn test_arbitrary_precision() {
fn test_buffer_serialize() -> LuaResult<()> {
let lua = Lua::new();

let buf = lua.create_buffer(&[1, 2, 3, 4])?;
let buf = lua.create_buffer([1, 2, 3, 4])?;
let val = serde_value::to_value(&buf).unwrap();
assert_eq!(val, serde_value::Value::Bytes(vec![1, 2, 3, 4]));

// Try empty buffer
let buf = lua.create_buffer(&[])?;
let buf = lua.create_buffer([])?;
let val = serde_value::to_value(&buf).unwrap();
assert_eq!(val, serde_value::Value::Bytes(vec![]));

Expand All @@ -839,7 +836,7 @@ fn test_buffer_serialize() -> LuaResult<()> {
fn test_buffer_from_value() -> LuaResult<()> {
let lua = Lua::new();

let buf = lua.create_buffer(&[1, 2, 3, 4])?;
let buf = lua.create_buffer([1, 2, 3, 4])?;
let val = lua.from_value::<serde_value::Value>(Value::Buffer(buf)).unwrap();
assert_eq!(val, serde_value::Value::Bytes(vec![1, 2, 3, 4]));

Expand Down
2 changes: 1 addition & 1 deletion tests/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn test_string_views() -> Result<()> {
fn test_string_from_bytes() -> Result<()> {
let lua = Lua::new();

let rs = lua.create_string(&[0, 1, 2, 3, 0, 1, 2, 3])?;
let rs = lua.create_string([0, 1, 2, 3, 0, 1, 2, 3])?;
assert_eq!(rs.as_bytes(), &[0, 1, 2, 3, 0, 1, 2, 3]);

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion tests/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ fn test_table_for_each() -> Result<()> {
table.set(k, Value::Nil)?;
lua.gc_collect()?;
}
Ok(i += 1)
let _: () = i += 1;
Ok(())
})?;
assert_eq!(i, 5);

Expand Down
32 changes: 14 additions & 18 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn test_eval() -> Result<()> {
let lua = Lua::new();

assert_eq!(lua.load("\t1 + 1").eval::<i32>()?, 2);
assert_eq!(lua.load("false == false").eval::<bool>()?, true);
assert!(lua.load("false == false").eval::<bool>()?);
assert_eq!(lua.load("\nreturn 1 + 2").eval::<i32>()?, 3);
match lua.load("if true then").eval::<()>() {
Err(Error::SyntaxError {
Expand Down Expand Up @@ -437,7 +437,7 @@ fn test_panic() -> Result<()> {
// Test returning Rust panic (must be resumed)
{
let lua = make_lua(LuaOptions::default())?;
match catch_unwind(AssertUnwindSafe(|| -> Result<()> {
if let Ok(_) = catch_unwind(AssertUnwindSafe(|| -> Result<()> {
let _caught_panic = lua
.load(
r#"
Expand All @@ -449,8 +449,7 @@ fn test_panic() -> Result<()> {
.eval::<Value>()?;
Ok(())
})) {
Ok(_) => panic!("no panic was detected"),
Err(_) => {}
panic!("no panic was detected")
};

assert!(lua.globals().get::<Value>("err")? == Value::Nil);
Expand Down Expand Up @@ -671,10 +670,10 @@ fn test_pcall_xpcall() -> Result<()> {
)
.exec()?;

assert_eq!(globals.get::<bool>("pcall_status")?, false);
assert!(!globals.get::<bool>("pcall_status")?);
assert_eq!(globals.get::<String>("pcall_error")?, "testerror");

assert_eq!(globals.get::<bool>("xpcall_statusr")?, false);
assert!(!globals.get::<bool>("xpcall_statusr")?);
#[cfg(any(
feature = "lua55",
feature = "lua54",
Expand Down Expand Up @@ -728,7 +727,7 @@ fn test_recursive_mut_callback_error() -> Result<()> {
match lua.globals().get::<Function>("f")?.call::<()>(false) {
Err(Error::CallbackError { ref cause, .. }) => match *cause.as_ref() {
Error::CallbackError { ref cause, .. } => match *cause.as_ref() {
Error::RecursiveMutCallback { .. } => {}
Error::RecursiveMutCallback => {}
ref other => panic!("incorrect result: {:?}", other),
},
ref other => panic!("incorrect result: {:?}", other),
Expand Down Expand Up @@ -920,13 +919,11 @@ fn test_application_data() -> Result<()> {

// Insert of new data or removal should fail now
assert!(lua.try_set_app_data::<i32>(123).is_err());
match catch_unwind(AssertUnwindSafe(|| lua.set_app_data::<i32>(123))) {
Ok(_) => panic!("expected panic"),
Err(_) => {}
if catch_unwind(AssertUnwindSafe(|| lua.set_app_data::<i32>(123))).is_ok() {
panic!("expected panic")
}
match catch_unwind(AssertUnwindSafe(|| lua.remove_app_data::<i32>())) {
Ok(_) => panic!("expected panic"),
Err(_) => {}
if catch_unwind(AssertUnwindSafe(|| lua.remove_app_data::<i32>())).is_ok() {
panic!("expected panic")
}

// Check display and debug impls
Expand All @@ -935,9 +932,8 @@ fn test_application_data() -> Result<()> {

// Borrowing immutably and mutably of the same type is not allowed
assert!(lua.try_app_data_mut::<&str>().is_err());
match catch_unwind(AssertUnwindSafe(|| lua.app_data_mut::<&str>().unwrap())) {
Ok(_) => panic!("expected panic"),
Err(_) => {}
if let Ok(_) = catch_unwind(AssertUnwindSafe(|| lua.app_data_mut::<&str>().unwrap())) {
panic!("expected panic")
}
assert!(lua.try_app_data_ref::<Vec<&str>>().is_err());
drop((s, v));
Expand All @@ -959,7 +955,7 @@ fn test_application_data() -> Result<()> {
assert_eq!(*lua.app_data_ref::<Vec<&str>>().unwrap(), vec!["test2", "test3"]);

lua.remove_app_data::<Vec<&str>>();
assert!(matches!(lua.app_data_ref::<Vec<&str>>(), None));
assert!(lua.app_data_ref::<Vec<&str>>().is_none());

Ok(())
}
Expand Down Expand Up @@ -1004,7 +1000,7 @@ fn test_c_function() -> Result<()> {

let func = unsafe { lua.create_c_function(c_function)? };
func.call::<()>(())?;
assert_eq!(lua.globals().get::<bool>("c_function")?, true);
assert!(lua.globals().get::<bool>("c_function")?);

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions tests/userdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ fn test_metamethods() -> Result<()> {
if i <= data.0 {
return Ok(mlua::Variadic::from_iter(vec![i, i]));
}
return Ok(mlua::Variadic::new());
Ok(mlua::Variadic::new())
})?;
Ok((stateless_iter, data.clone(), 0))
Ok((stateless_iter, *data, 0))
});
}
}
Expand Down
Loading