mirror of
https://github.com/lune-org/mlua-luau-scheduler.git
synced 2025-04-04 10:30:56 +01:00
Rename args
This commit is contained in:
parent
b2e5dc9754
commit
c344fb927a
5 changed files with 297 additions and 273 deletions
263
src/args.rs
263
src/args.rs
|
@ -1,263 +0,0 @@
|
|||
#![allow(dead_code)]
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use mlua::prelude::*;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub enum Arg {
|
||||
#[default]
|
||||
Nil,
|
||||
Bool(bool),
|
||||
Number(f64),
|
||||
String(String),
|
||||
}
|
||||
|
||||
impl IntoLua<'_> for Arg {
|
||||
#[inline]
|
||||
fn into_lua(self, lua: &Lua) -> LuaResult<LuaValue> {
|
||||
match self {
|
||||
Arg::Nil => Ok(LuaValue::Nil),
|
||||
Arg::Bool(b) => Ok(LuaValue::Boolean(b)),
|
||||
Arg::Number(n) => Ok(LuaValue::Number(n)),
|
||||
Arg::String(s) => Ok(LuaValue::String(lua.create_string(&s)?)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Primitives
|
||||
|
||||
impl From<()> for Arg {
|
||||
#[inline]
|
||||
fn from(_: ()) -> Self {
|
||||
Arg::Nil
|
||||
}
|
||||
}
|
||||
|
||||
impl From<bool> for Arg {
|
||||
#[inline]
|
||||
fn from(b: bool) -> Self {
|
||||
Arg::Bool(b)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u8> for Arg {
|
||||
#[inline]
|
||||
fn from(u: u8) -> Self {
|
||||
Arg::Number(u as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u16> for Arg {
|
||||
#[inline]
|
||||
fn from(u: u16) -> Self {
|
||||
Arg::Number(u as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u32> for Arg {
|
||||
#[inline]
|
||||
fn from(u: u32) -> Self {
|
||||
Arg::Number(u as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u64> for Arg {
|
||||
#[inline]
|
||||
fn from(u: u64) -> Self {
|
||||
Arg::Number(u as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i8> for Arg {
|
||||
#[inline]
|
||||
fn from(i: i8) -> Self {
|
||||
Arg::Number(i as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i16> for Arg {
|
||||
#[inline]
|
||||
fn from(i: i16) -> Self {
|
||||
Arg::Number(i as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i32> for Arg {
|
||||
#[inline]
|
||||
fn from(i: i32) -> Self {
|
||||
Arg::Number(i as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i64> for Arg {
|
||||
#[inline]
|
||||
fn from(i: i64) -> Self {
|
||||
Arg::Number(i as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f32> for Arg {
|
||||
#[inline]
|
||||
fn from(n: f32) -> Self {
|
||||
Arg::Number(n as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f64> for Arg {
|
||||
#[inline]
|
||||
fn from(n: f64) -> Self {
|
||||
Arg::Number(n)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Arg {
|
||||
#[inline]
|
||||
fn from(s: String) -> Self {
|
||||
Arg::String(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&String> for Arg {
|
||||
#[inline]
|
||||
fn from(s: &String) -> Self {
|
||||
Arg::String(s.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Arg {
|
||||
#[inline]
|
||||
fn from(s: &str) -> Self {
|
||||
Arg::String(s.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
// Other types
|
||||
|
||||
impl From<Duration> for Arg {
|
||||
#[inline]
|
||||
fn from(d: Duration) -> Self {
|
||||
Arg::Number(d.as_secs_f64())
|
||||
}
|
||||
}
|
||||
|
||||
// Multi args
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Args {
|
||||
inner: Vec<Arg>,
|
||||
}
|
||||
|
||||
impl Args {
|
||||
#[inline]
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoLuaMulti<'_> for Args {
|
||||
#[inline]
|
||||
fn into_lua_multi(self, lua: &Lua) -> LuaResult<LuaMultiValue> {
|
||||
let mut values = Vec::new();
|
||||
for arg in self.inner {
|
||||
values.push(arg.into_lua(lua)?);
|
||||
}
|
||||
Ok(LuaMultiValue::from_vec(values))
|
||||
}
|
||||
}
|
||||
|
||||
// Boilerplate
|
||||
|
||||
impl<T> From<T> for Args
|
||||
where
|
||||
T: Into<Arg>,
|
||||
{
|
||||
#[inline]
|
||||
fn from(t: T) -> Self {
|
||||
Args {
|
||||
inner: vec![t.into()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T0, T1> From<(T0, T1)> for Args
|
||||
where
|
||||
T0: Into<Arg>,
|
||||
T1: Into<Arg>,
|
||||
{
|
||||
#[inline]
|
||||
fn from((t0, t1): (T0, T1)) -> Self {
|
||||
Args {
|
||||
inner: vec![t0.into(), t1.into()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T0, T1, T2> From<(T0, T1, T2)> for Args
|
||||
where
|
||||
T0: Into<Arg>,
|
||||
T1: Into<Arg>,
|
||||
T2: Into<Arg>,
|
||||
{
|
||||
#[inline]
|
||||
fn from((t0, t1, t2): (T0, T1, T2)) -> Self {
|
||||
Args {
|
||||
inner: vec![t0.into(), t1.into(), t2.into()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3> From<(T0, T1, T2, T3)> for Args
|
||||
where
|
||||
T0: Into<Arg>,
|
||||
T1: Into<Arg>,
|
||||
T2: Into<Arg>,
|
||||
T3: Into<Arg>,
|
||||
{
|
||||
#[inline]
|
||||
fn from((t0, t1, t2, t3): (T0, T1, T2, T3)) -> Self {
|
||||
Args {
|
||||
inner: vec![t0.into(), t1.into(), t2.into(), t3.into()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3, T4> From<(T0, T1, T2, T3, T4)> for Args
|
||||
where
|
||||
T0: Into<Arg>,
|
||||
T1: Into<Arg>,
|
||||
T2: Into<Arg>,
|
||||
T3: Into<Arg>,
|
||||
T4: Into<Arg>,
|
||||
{
|
||||
#[inline]
|
||||
fn from((t0, t1, t2, t3, t4): (T0, T1, T2, T3, T4)) -> Self {
|
||||
Args {
|
||||
inner: vec![t0.into(), t1.into(), t2.into(), t3.into(), t4.into()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3, T4, T5> From<(T0, T1, T2, T3, T4, T5)> for Args
|
||||
where
|
||||
T0: Into<Arg>,
|
||||
T1: Into<Arg>,
|
||||
T2: Into<Arg>,
|
||||
T3: Into<Arg>,
|
||||
T4: Into<Arg>,
|
||||
T5: Into<Arg>,
|
||||
{
|
||||
#[inline]
|
||||
fn from((t0, t1, t2, t3, t4, t5): (T0, T1, T2, T3, T4, T5)) -> Self {
|
||||
Args {
|
||||
inner: vec![
|
||||
t0.into(),
|
||||
t1.into(),
|
||||
t2.into(),
|
||||
t3.into(),
|
||||
t4.into(),
|
||||
t5.into(),
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,13 +3,13 @@ use std::future::Future;
|
|||
use mlua::prelude::*;
|
||||
use tokio::spawn;
|
||||
|
||||
use crate::{Args, Message, MessageSender, ThreadId};
|
||||
use crate::{AsyncValues, Message, MessageSender, ThreadId};
|
||||
|
||||
pub trait LuaSchedulerExt<'lua> {
|
||||
fn create_async_function<A, R, F, FR>(&'lua self, func: F) -> LuaResult<LuaFunction<'lua>>
|
||||
where
|
||||
A: FromLuaMulti<'lua> + 'static,
|
||||
R: Into<Args> + Send + 'static,
|
||||
R: Into<AsyncValues> + Send + 'static,
|
||||
F: Fn(&'lua Lua, A) -> FR + 'static,
|
||||
FR: Future<Output = LuaResult<R>> + Send + 'static;
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ impl<'lua> LuaSchedulerExt<'lua> for Lua {
|
|||
fn create_async_function<A, R, F, FR>(&'lua self, func: F) -> LuaResult<LuaFunction<'lua>>
|
||||
where
|
||||
A: FromLuaMulti<'lua> + 'static,
|
||||
R: Into<Args> + Send + 'static,
|
||||
R: Into<AsyncValues> + Send + 'static,
|
||||
F: Fn(&'lua Lua, A) -> FR + 'static,
|
||||
FR: Future<Output = LuaResult<R>> + Send + 'static,
|
||||
{
|
||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -9,20 +9,20 @@ use tokio::{
|
|||
time::{sleep, Instant},
|
||||
};
|
||||
|
||||
mod args;
|
||||
mod error_storage;
|
||||
mod lua;
|
||||
mod lua_ext;
|
||||
mod message;
|
||||
mod stats;
|
||||
mod thread_id;
|
||||
mod value;
|
||||
|
||||
use args::*;
|
||||
use error_storage::*;
|
||||
use lua::*;
|
||||
use message::*;
|
||||
use stats::*;
|
||||
use thread_id::*;
|
||||
use value::*;
|
||||
|
||||
const NUM_TEST_BATCHES: usize = 20;
|
||||
const NUM_TEST_THREADS: usize = 50_000;
|
||||
|
@ -82,7 +82,7 @@ fn main_lua_task(mut rx: MessageReceiver, tx: MessageSender, stats: Stats) -> Lu
|
|||
let main_fn = lua.load(MAIN_CHUNK).into_function()?;
|
||||
for _ in 0..NUM_TEST_THREADS {
|
||||
let thread = lua.create_thread(main_fn.clone())?;
|
||||
runnable_threads.insert(ThreadId::from(&thread), (thread, Ok(Args::new())));
|
||||
runnable_threads.insert(ThreadId::from(&thread), (thread, Ok(AsyncValues::new())));
|
||||
}
|
||||
|
||||
loop {
|
||||
|
@ -99,7 +99,7 @@ fn main_lua_task(mut rx: MessageReceiver, tx: MessageSender, stats: Stats) -> Lu
|
|||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
error_storage.replace(e);
|
||||
Args::from(())
|
||||
AsyncValues::from(())
|
||||
}
|
||||
};
|
||||
if let Err(e) = thread.resume::<_, ()>(args) {
|
||||
|
@ -177,7 +177,7 @@ async fn main_async_task(
|
|||
spawn(async move {
|
||||
sleep(duration).await;
|
||||
let elapsed = Instant::now() - yielded_at;
|
||||
tx.send(Message::Resume(thread_id, Ok(Args::from(elapsed))))
|
||||
tx.send(Message::Resume(thread_id, Ok(AsyncValues::from(elapsed))))
|
||||
});
|
||||
}
|
||||
Message::Error(_, e) => {
|
||||
|
|
|
@ -6,13 +6,13 @@ use tokio::{
|
|||
time::Instant,
|
||||
};
|
||||
|
||||
use crate::{Args, ThreadId};
|
||||
use crate::{AsyncValues, ThreadId};
|
||||
|
||||
pub type MessageSender = UnboundedSender<Message>;
|
||||
pub type MessageReceiver = UnboundedReceiver<Message>;
|
||||
|
||||
pub enum Message {
|
||||
Resume(ThreadId, LuaResult<Args>),
|
||||
Resume(ThreadId, LuaResult<AsyncValues>),
|
||||
Cancel(ThreadId),
|
||||
Sleep(ThreadId, Instant, Duration),
|
||||
Error(ThreadId, Box<LuaError>),
|
||||
|
|
287
src/value.rs
Normal file
287
src/value.rs
Normal file
|
@ -0,0 +1,287 @@
|
|||
#![allow(dead_code)]
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use mlua::prelude::*;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub enum AsyncValue {
|
||||
#[default]
|
||||
Nil,
|
||||
Bool(bool),
|
||||
Number(f64),
|
||||
String(String),
|
||||
Bytes(Vec<u8>),
|
||||
}
|
||||
|
||||
impl IntoLua<'_> for AsyncValue {
|
||||
#[inline]
|
||||
fn into_lua(self, lua: &Lua) -> LuaResult<LuaValue> {
|
||||
match self {
|
||||
AsyncValue::Nil => Ok(LuaValue::Nil),
|
||||
AsyncValue::Bool(b) => Ok(LuaValue::Boolean(b)),
|
||||
AsyncValue::Number(n) => Ok(LuaValue::Number(n)),
|
||||
AsyncValue::String(s) => Ok(LuaValue::String(lua.create_string(&s)?)),
|
||||
AsyncValue::Bytes(b) => Ok(LuaValue::String(lua.create_string(&b)?)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Primitives
|
||||
|
||||
impl From<()> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(_: ()) -> Self {
|
||||
AsyncValue::Nil
|
||||
}
|
||||
}
|
||||
|
||||
impl From<bool> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(b: bool) -> Self {
|
||||
AsyncValue::Bool(b)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u8> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(u: u8) -> Self {
|
||||
AsyncValue::Number(u as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u16> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(u: u16) -> Self {
|
||||
AsyncValue::Number(u as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u32> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(u: u32) -> Self {
|
||||
AsyncValue::Number(u as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u64> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(u: u64) -> Self {
|
||||
AsyncValue::Number(u as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i8> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(i: i8) -> Self {
|
||||
AsyncValue::Number(i as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i16> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(i: i16) -> Self {
|
||||
AsyncValue::Number(i as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i32> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(i: i32) -> Self {
|
||||
AsyncValue::Number(i as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i64> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(i: i64) -> Self {
|
||||
AsyncValue::Number(i as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f32> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(n: f32) -> Self {
|
||||
AsyncValue::Number(n as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f64> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(n: f64) -> Self {
|
||||
AsyncValue::Number(n)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(s: String) -> Self {
|
||||
AsyncValue::String(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&String> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(s: &String) -> Self {
|
||||
AsyncValue::String(s.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(s: &str) -> Self {
|
||||
AsyncValue::String(s.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<u8>> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(b: Vec<u8>) -> Self {
|
||||
AsyncValue::Bytes(b)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Vec<u8>> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(b: &Vec<u8>) -> Self {
|
||||
AsyncValue::Bytes(b.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&[u8]> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(b: &[u8]) -> Self {
|
||||
AsyncValue::Bytes(b.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
// Other types
|
||||
|
||||
impl From<Duration> for AsyncValue {
|
||||
#[inline]
|
||||
fn from(d: Duration) -> Self {
|
||||
AsyncValue::Number(d.as_secs_f64())
|
||||
}
|
||||
}
|
||||
|
||||
// Multi args
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct AsyncValues {
|
||||
inner: Vec<AsyncValue>,
|
||||
}
|
||||
|
||||
impl AsyncValues {
|
||||
#[inline]
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoLuaMulti<'_> for AsyncValues {
|
||||
#[inline]
|
||||
fn into_lua_multi(self, lua: &Lua) -> LuaResult<LuaMultiValue> {
|
||||
Ok(LuaMultiValue::from_vec(
|
||||
self.inner
|
||||
.into_iter()
|
||||
.map(|arg| arg.into_lua(lua))
|
||||
.collect::<LuaResult<Vec<_>>>()?,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
// Boilerplate
|
||||
|
||||
impl<T> From<T> for AsyncValues
|
||||
where
|
||||
T: Into<AsyncValue>,
|
||||
{
|
||||
#[inline]
|
||||
fn from(t: T) -> Self {
|
||||
AsyncValues {
|
||||
inner: vec![t.into()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T0, T1> From<(T0, T1)> for AsyncValues
|
||||
where
|
||||
T0: Into<AsyncValue>,
|
||||
T1: Into<AsyncValue>,
|
||||
{
|
||||
#[inline]
|
||||
fn from((t0, t1): (T0, T1)) -> Self {
|
||||
AsyncValues {
|
||||
inner: vec![t0.into(), t1.into()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T0, T1, T2> From<(T0, T1, T2)> for AsyncValues
|
||||
where
|
||||
T0: Into<AsyncValue>,
|
||||
T1: Into<AsyncValue>,
|
||||
T2: Into<AsyncValue>,
|
||||
{
|
||||
#[inline]
|
||||
fn from((t0, t1, t2): (T0, T1, T2)) -> Self {
|
||||
AsyncValues {
|
||||
inner: vec![t0.into(), t1.into(), t2.into()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3> From<(T0, T1, T2, T3)> for AsyncValues
|
||||
where
|
||||
T0: Into<AsyncValue>,
|
||||
T1: Into<AsyncValue>,
|
||||
T2: Into<AsyncValue>,
|
||||
T3: Into<AsyncValue>,
|
||||
{
|
||||
#[inline]
|
||||
fn from((t0, t1, t2, t3): (T0, T1, T2, T3)) -> Self {
|
||||
AsyncValues {
|
||||
inner: vec![t0.into(), t1.into(), t2.into(), t3.into()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3, T4> From<(T0, T1, T2, T3, T4)> for AsyncValues
|
||||
where
|
||||
T0: Into<AsyncValue>,
|
||||
T1: Into<AsyncValue>,
|
||||
T2: Into<AsyncValue>,
|
||||
T3: Into<AsyncValue>,
|
||||
T4: Into<AsyncValue>,
|
||||
{
|
||||
#[inline]
|
||||
fn from((t0, t1, t2, t3, t4): (T0, T1, T2, T3, T4)) -> Self {
|
||||
AsyncValues {
|
||||
inner: vec![t0.into(), t1.into(), t2.into(), t3.into(), t4.into()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3, T4, T5> From<(T0, T1, T2, T3, T4, T5)> for AsyncValues
|
||||
where
|
||||
T0: Into<AsyncValue>,
|
||||
T1: Into<AsyncValue>,
|
||||
T2: Into<AsyncValue>,
|
||||
T3: Into<AsyncValue>,
|
||||
T4: Into<AsyncValue>,
|
||||
T5: Into<AsyncValue>,
|
||||
{
|
||||
#[inline]
|
||||
fn from((t0, t1, t2, t3, t4, t5): (T0, T1, T2, T3, T4, T5)) -> Self {
|
||||
AsyncValues {
|
||||
inner: vec![
|
||||
t0.into(),
|
||||
t1.into(),
|
||||
t2.into(),
|
||||
t3.into(),
|
||||
t4.into(),
|
||||
t5.into(),
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue