#![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), } impl IntoLua<'_> for AsyncValue { #[inline] fn into_lua(self, lua: &Lua) -> LuaResult { 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 for AsyncValue { #[inline] fn from(b: bool) -> Self { AsyncValue::Bool(b) } } impl From for AsyncValue { #[inline] fn from(u: u8) -> Self { AsyncValue::Number(u as f64) } } impl From for AsyncValue { #[inline] fn from(u: u16) -> Self { AsyncValue::Number(u as f64) } } impl From for AsyncValue { #[inline] fn from(u: u32) -> Self { AsyncValue::Number(u as f64) } } impl From for AsyncValue { #[inline] fn from(u: u64) -> Self { AsyncValue::Number(u as f64) } } impl From for AsyncValue { #[inline] fn from(i: i8) -> Self { AsyncValue::Number(i as f64) } } impl From for AsyncValue { #[inline] fn from(i: i16) -> Self { AsyncValue::Number(i as f64) } } impl From for AsyncValue { #[inline] fn from(i: i32) -> Self { AsyncValue::Number(i as f64) } } impl From for AsyncValue { #[inline] fn from(i: i64) -> Self { AsyncValue::Number(i as f64) } } impl From for AsyncValue { #[inline] fn from(n: f32) -> Self { AsyncValue::Number(n as f64) } } impl From for AsyncValue { #[inline] fn from(n: f64) -> Self { AsyncValue::Number(n) } } impl From 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> for AsyncValue { #[inline] fn from(b: Vec) -> Self { AsyncValue::Bytes(b) } } impl From<&Vec> for AsyncValue { #[inline] fn from(b: &Vec) -> 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 for AsyncValue { #[inline] fn from(d: Duration) -> Self { AsyncValue::Number(d.as_secs_f64()) } } // Multi args #[derive(Debug, Default)] pub struct AsyncValues { inner: Vec, } impl AsyncValues { #[inline] pub fn new() -> Self { Self::default() } } impl IntoLuaMulti<'_> for AsyncValues { #[inline] fn into_lua_multi(self, lua: &Lua) -> LuaResult { Ok(LuaMultiValue::from_vec( self.inner .into_iter() .map(|arg| arg.into_lua(lua)) .collect::>>()?, )) } } // Boilerplate impl From for AsyncValues where T: Into, { #[inline] fn from(t: T) -> Self { AsyncValues { inner: vec![t.into()], } } } impl From<(T0, T1)> for AsyncValues where T0: Into, T1: Into, { #[inline] fn from((t0, t1): (T0, T1)) -> Self { AsyncValues { inner: vec![t0.into(), t1.into()], } } } impl From<(T0, T1, T2)> for AsyncValues where T0: Into, T1: Into, T2: Into, { #[inline] fn from((t0, t1, t2): (T0, T1, T2)) -> Self { AsyncValues { inner: vec![t0.into(), t1.into(), t2.into()], } } } impl From<(T0, T1, T2, T3)> for AsyncValues where T0: Into, T1: Into, T2: Into, T3: Into, { #[inline] fn from((t0, t1, t2, t3): (T0, T1, T2, T3)) -> Self { AsyncValues { inner: vec![t0.into(), t1.into(), t2.into(), t3.into()], } } } impl From<(T0, T1, T2, T3, T4)> for AsyncValues where T0: Into, T1: Into, T2: Into, T3: Into, T4: Into, { #[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 From<(T0, T1, T2, T3, T4, T5)> for AsyncValues where T0: Into, T1: Into, T2: Into, T3: Into, T4: Into, T5: Into, { #[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(), ], } } }