Skip to content

Commit 47e6a37

Browse files
committed
Add shortcuts to check thread status (Thread::is_resumable(), Thread::is_finished() etc)
1 parent bf0c969 commit 47e6a37

5 files changed

Lines changed: 52 additions & 29 deletions

File tree

src/thread.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,31 @@ impl Thread {
259259
}
260260
}
261261

262+
/// Returns `true` if this thread is resumable (meaning it can be resumed by calling
263+
/// [`Thread::resume`]).
264+
#[inline(always)]
265+
pub fn is_resumable(&self) -> bool {
266+
self.status() == ThreadStatus::Resumable
267+
}
268+
269+
/// Returns `true` if this thread is currently running.
270+
#[inline(always)]
271+
pub fn is_running(&self) -> bool {
272+
self.status() == ThreadStatus::Running
273+
}
274+
275+
/// Returns `true` if this thread has finished executing.
276+
#[inline(always)]
277+
pub fn is_finished(&self) -> bool {
278+
self.status() == ThreadStatus::Finished
279+
}
280+
281+
/// Returns `true` if this thread has raised a Lua error during execution.
282+
#[inline(always)]
283+
pub fn is_error(&self) -> bool {
284+
self.status() == ThreadStatus::Error
285+
}
286+
262287
/// Sets a hook function that will periodically be called as Lua code executes.
263288
///
264289
/// This function is similar or [`Lua::set_hook`] except that it sets for the thread.

tests/async.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use futures_util::stream::TryStreamExt;
77
use tokio::sync::Mutex;
88

99
use mlua::{
10-
Error, Function, Lua, LuaOptions, MultiValue, ObjectLike, Result, StdLib, Table, ThreadStatus, UserData,
10+
Error, Function, Lua, LuaOptions, MultiValue, ObjectLike, Result, StdLib, Table, UserData,
1111
UserDataMethods, UserDataRef, Value,
1212
};
1313

@@ -714,7 +714,7 @@ fn test_async_yield_with() -> Result<()> {
714714
assert_eq!(thread.resume::<(i32, i32)>((10, 11))?, (21, 110));
715715
assert_eq!(thread.resume::<(i32, i32)>((11, 12))?, (23, 132));
716716
assert_eq!(thread.resume::<(i32, i32)>((12, 13))?, (0, 0));
717-
assert_eq!(thread.status(), ThreadStatus::Finished);
717+
assert!(thread.is_finished());
718718

719719
Ok(())
720720
}

tests/hooks.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::sync::atomic::{AtomicI64, Ordering};
44
use std::sync::{Arc, Mutex};
55

66
use mlua::debug::DebugEvent;
7-
use mlua::{Error, HookTriggers, Lua, Result, ThreadStatus, Value, VmState};
7+
use mlua::{Error, HookTriggers, Lua, Result, Value, VmState};
88

99
#[test]
1010
fn test_hook_triggers() {
@@ -281,14 +281,14 @@ fn test_hook_yield() -> Result<()> {
281281
assert!(co.resume::<()>(()).is_ok());
282282
assert!(co.resume::<()>(()).is_ok());
283283
assert!(co.resume::<()>(()).is_ok());
284-
assert!(co.status() == ThreadStatus::Finished);
284+
assert!(co.is_finished());
285285
}
286286
#[cfg(any(feature = "lua51", feature = "lua52", feature = "luajit"))]
287287
{
288288
assert!(
289289
matches!(co.resume::<()>(()), Err(Error::RuntimeError(err)) if err.contains("attempt to yield from a hook"))
290290
);
291-
assert!(co.status() == ThreadStatus::Error);
291+
assert!(co.is_error());
292292
}
293293

294294
Ok(())
@@ -321,7 +321,7 @@ fn test_global_hook() -> Result<()> {
321321
thread.resume::<()>(()).unwrap();
322322
lua.remove_global_hook();
323323
thread.resume::<()>(()).unwrap();
324-
assert_eq!(thread.status(), ThreadStatus::Finished);
324+
assert!(thread.is_finished());
325325
assert_eq!(counter.load(Ordering::Relaxed), 3);
326326

327327
Ok(())

tests/luau.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use std::os::raw::c_void;
66
use std::sync::Arc;
77
use std::sync::atomic::{AtomicBool, AtomicPtr, AtomicU64, Ordering};
88

9-
use mlua::{
10-
Compiler, Error, Function, Lua, LuaOptions, Result, StdLib, Table, ThreadStatus, Value, Vector, VmState,
11-
};
9+
use mlua::{Compiler, Error, Function, Lua, LuaOptions, Result, StdLib, Table, Value, Vector, VmState};
1210

1311
#[test]
1412
fn test_version() -> Result<()> {
@@ -324,11 +322,11 @@ fn test_interrupts() -> Result<()> {
324322
.into_function()?,
325323
)?;
326324
co.resume::<()>(())?;
327-
assert_eq!(co.status(), ThreadStatus::Resumable);
325+
assert!(co.is_resumable());
328326
let result: i32 = co.resume(())?;
329327
assert_eq!(result, 6);
330328
assert_eq!(yield_count.load(Ordering::Relaxed), 7);
331-
assert_eq!(co.status(), ThreadStatus::Finished);
329+
assert!(co.is_finished());
332330

333331
// Test no yielding at non-yieldable points
334332
yield_count.store(0, Ordering::Relaxed);

tests/thread.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::panic::catch_unwind;
22

3-
use mlua::{Error, Function, IntoLua, Lua, Result, Thread, ThreadStatus, Value};
3+
use mlua::{Error, Function, IntoLua, Lua, Result, Thread, Value};
44

55
#[test]
66
fn test_thread() -> Result<()> {
@@ -21,17 +21,17 @@ fn test_thread() -> Result<()> {
2121
.eval()?,
2222
)?;
2323

24-
assert_eq!(thread.status(), ThreadStatus::Resumable);
24+
assert!(thread.is_resumable());
2525
assert_eq!(thread.resume::<i64>(0)?, 0);
26-
assert_eq!(thread.status(), ThreadStatus::Resumable);
26+
assert!(thread.is_resumable());
2727
assert_eq!(thread.resume::<i64>(1)?, 1);
28-
assert_eq!(thread.status(), ThreadStatus::Resumable);
28+
assert!(thread.is_resumable());
2929
assert_eq!(thread.resume::<i64>(2)?, 3);
30-
assert_eq!(thread.status(), ThreadStatus::Resumable);
30+
assert!(thread.is_resumable());
3131
assert_eq!(thread.resume::<i64>(3)?, 6);
32-
assert_eq!(thread.status(), ThreadStatus::Resumable);
32+
assert!(thread.is_resumable());
3333
assert_eq!(thread.resume::<i64>(4)?, 10);
34-
assert_eq!(thread.status(), ThreadStatus::Finished);
34+
assert!(thread.is_finished());
3535

3636
let accumulate = lua.create_thread(
3737
lua.load(
@@ -50,9 +50,9 @@ fn test_thread() -> Result<()> {
5050
accumulate.resume::<()>(i)?;
5151
}
5252
assert_eq!(accumulate.resume::<i64>(4)?, 10);
53-
assert_eq!(accumulate.status(), ThreadStatus::Resumable);
53+
assert!(accumulate.is_resumable());
5454
assert!(accumulate.resume::<()>("error").is_err());
55-
assert_eq!(accumulate.status(), ThreadStatus::Error);
55+
assert!(accumulate.is_error());
5656

5757
let thread = lua
5858
.load(
@@ -65,7 +65,7 @@ fn test_thread() -> Result<()> {
6565
"#,
6666
)
6767
.eval::<Thread>()?;
68-
assert_eq!(thread.status(), ThreadStatus::Resumable);
68+
assert!(thread.is_resumable());
6969
assert_eq!(thread.resume::<i64>(())?, 42);
7070

7171
let thread: Thread = lua
@@ -92,7 +92,7 @@ fn test_thread() -> Result<()> {
9292

9393
// Already running thread must be unresumable
9494
let thread = lua.create_thread(lua.create_function(|lua, ()| {
95-
assert_eq!(lua.current_thread().status(), ThreadStatus::Running);
95+
assert!(lua.current_thread().is_running());
9696
let result = lua.current_thread().resume::<()>(());
9797
assert!(
9898
matches!(result, Err(Error::CoroutineUnresumable)),
@@ -123,12 +123,12 @@ fn test_thread_reset() -> Result<()> {
123123
assert!(thread.reset(func.clone()).is_ok());
124124

125125
for _ in 0..2 {
126-
assert_eq!(thread.status(), ThreadStatus::Resumable);
126+
assert!(thread.is_resumable());
127127
let _ = thread.resume::<AnyUserData>(MyUserData(arc.clone()))?;
128-
assert_eq!(thread.status(), ThreadStatus::Resumable);
128+
assert!(thread.is_resumable());
129129
assert_eq!(Arc::strong_count(&arc), 2);
130130
thread.resume::<()>(())?;
131-
assert_eq!(thread.status(), ThreadStatus::Finished);
131+
assert!(thread.is_finished());
132132
thread.reset(func.clone())?;
133133
lua.gc_collect()?;
134134
assert_eq!(Arc::strong_count(&arc), 1);
@@ -138,21 +138,21 @@ fn test_thread_reset() -> Result<()> {
138138
let func: Function = lua.load(r#"function(ud) error("test error") end"#).eval()?;
139139
let thread = lua.create_thread(func.clone())?;
140140
let _ = thread.resume::<AnyUserData>(MyUserData(arc.clone()));
141-
assert_eq!(thread.status(), ThreadStatus::Error);
141+
assert!(thread.is_error());
142142
assert_eq!(Arc::strong_count(&arc), 2);
143143
#[cfg(any(feature = "lua55", feature = "lua54"))]
144144
{
145145
assert!(thread.reset(func.clone()).is_err());
146146
// Reset behavior has changed in Lua v5.4.4
147147
// It's became possible to force reset thread by popping error object
148-
assert!(matches!(thread.status(), ThreadStatus::Finished));
148+
assert!(thread.is_finished());
149149
assert!(thread.reset(func.clone()).is_ok());
150-
assert_eq!(thread.status(), ThreadStatus::Resumable);
150+
assert!(thread.is_resumable());
151151
}
152152
#[cfg(any(feature = "lua55", feature = "lua54", feature = "luau"))]
153153
{
154154
assert!(thread.reset(func.clone()).is_ok());
155-
assert_eq!(thread.status(), ThreadStatus::Resumable);
155+
assert!(thread.is_resumable());
156156
}
157157

158158
// Try reset running thread

0 commit comments

Comments
 (0)