From 22d15b36499db93c46ec381a562a3d1f440d6d02 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Fri, 12 Apr 2024 15:00:46 +0530 Subject: [PATCH] feat: return raw `i32` instead of `ExitCode` --- examples/exit_code.rs | 2 +- lib/exit.rs | 8 ++++---- lib/functions.rs | 10 +++++----- lib/scheduler.rs | 5 ++--- lib/traits.rs | 8 +++----- lib/util.rs | 3 +-- 6 files changed, 16 insertions(+), 20 deletions(-) diff --git a/examples/exit_code.rs b/examples/exit_code.rs index 6e968ef..4b19421 100644 --- a/examples/exit_code.rs +++ b/examples/exit_code.rs @@ -31,7 +31,7 @@ pub fn main() -> LuaResult<()> { // Verify that we got a correct exit code let code = sched.get_exit_code().unwrap_or_default(); - assert!(format!("{code:?}").contains("(1)")); + assert_eq!(code, 1); Ok(()) } diff --git a/lib/exit.rs b/lib/exit.rs index a2794dd..bc29d78 100644 --- a/lib/exit.rs +++ b/lib/exit.rs @@ -1,10 +1,10 @@ -use std::{cell::Cell, process::ExitCode, rc::Rc}; +use std::{cell::Cell, rc::Rc}; use event_listener::Event; #[derive(Debug, Clone)] pub(crate) struct Exit { - code: Rc>>, + code: Rc>>, event: Rc, } @@ -16,12 +16,12 @@ impl Exit { } } - pub fn set(&self, code: ExitCode) { + pub fn set(&self, code: i32) { self.code.set(Some(code)); self.event.notify(usize::MAX); } - pub fn get(&self) -> Option { + pub fn get(&self) -> Option { self.code.get() } diff --git a/lib/functions.rs b/lib/functions.rs index 05cdcb5..801d989 100644 --- a/lib/functions.rs +++ b/lib/functions.rs @@ -1,7 +1,7 @@ #![allow(unused_imports)] #![allow(clippy::too_many_lines)] -use std::process::ExitCode; +use std::process::{ExitCode, ExitStatus}; use mlua::prelude::*; @@ -118,7 +118,7 @@ impl<'lua> Functions<'lua> { let _span = tracing::trace_span!("Scheduler::fn_resume").entered(); match thread.resume::<_, LuaMultiValue>(args.clone()) { Ok(v) => { - if v.get(0).map(is_poll_pending).unwrap_or_default() { + if v.get(0).is_some_and(is_poll_pending) { // Pending, defer to scheduler and return nil resume_queue.push_item(lua, &thread, args)?; (true, LuaValue::Nil).into_lua_multi(lua) @@ -174,7 +174,7 @@ impl<'lua> Functions<'lua> { // and only if we get the pending value back we can spawn to async executor match thread.resume::<_, LuaMultiValue>(args.clone()) { Ok(v) => { - if v.get(0).map(is_poll_pending).unwrap_or_default() { + if v.get(0).is_some_and(is_poll_pending) { spawn_queue.push_item(lua, &thread, args)?; } else { // Not pending, store the value if thread is done @@ -230,9 +230,9 @@ impl<'lua> Functions<'lua> { let exit_env = lua.create_table_from(vec![ ( "exit", - lua.create_function(|lua, code: Option| { + lua.create_function(|lua, code: Option| { let _span = tracing::trace_span!("Scheduler::fn_exit").entered(); - let code = code.map(ExitCode::from).unwrap_or_default(); + let code = code.unwrap_or_default(); lua.set_exit_code(code); Ok(()) })?, diff --git a/lib/scheduler.rs b/lib/scheduler.rs index 31f699e..05300c9 100644 --- a/lib/scheduler.rs +++ b/lib/scheduler.rs @@ -2,7 +2,6 @@ use std::{ cell::Cell, - process::ExitCode, rc::{Rc, Weak as WeakRc}, sync::{Arc, Weak as WeakArc}, thread::panicking, @@ -168,7 +167,7 @@ impl<'lua> Scheduler<'lua> { Gets the exit code for this scheduler, if one has been set. */ #[must_use] - pub fn get_exit_code(&self) -> Option { + pub fn get_exit_code(&self) -> Option { self.exit.get() } @@ -177,7 +176,7 @@ impl<'lua> Scheduler<'lua> { This will cause [`Scheduler::run`] to exit immediately. */ - pub fn set_exit_code(&self, code: ExitCode) { + pub fn set_exit_code(&self, code: i32) { self.exit.set(code); } diff --git a/lib/traits.rs b/lib/traits.rs index cbe2e6e..1a240d2 100644 --- a/lib/traits.rs +++ b/lib/traits.rs @@ -1,9 +1,7 @@ #![allow(unused_imports)] #![allow(clippy::missing_errors_doc)] -use std::{ - cell::Cell, future::Future, process::ExitCode, rc::Weak as WeakRc, sync::Weak as WeakArc, -}; +use std::{cell::Cell, future::Future, rc::Weak as WeakRc, sync::Weak as WeakArc}; use async_executor::{Executor, Task}; use mlua::prelude::*; @@ -82,7 +80,7 @@ pub trait LuaSchedulerExt<'lua> { Panics if called outside of a running [`Scheduler`]. */ - fn set_exit_code(&self, code: ExitCode); + fn set_exit_code(&self, code: i32); /** Pushes (spawns) a lua thread to the **front** of the current scheduler. @@ -283,7 +281,7 @@ pub trait LuaSpawnExt<'lua> { } impl<'lua> LuaSchedulerExt<'lua> for Lua { - fn set_exit_code(&self, code: ExitCode) { + fn set_exit_code(&self, code: i32) { let exit = self .app_data_ref::() .expect("exit code can only be set from within an active scheduler"); diff --git a/lib/util.rs b/lib/util.rs index a5564d0..2fe537b 100644 --- a/lib/util.rs +++ b/lib/util.rs @@ -37,8 +37,7 @@ pub(crate) async fn run_until_yield<'lua>( pub(crate) fn is_poll_pending(value: &LuaValue) -> bool { value .as_light_userdata() - .map(|l| l == Lua::poll_pending()) - .unwrap_or_default() + .is_some_and(|l| l == Lua::poll_pending()) } /**