From 1247196c351a798e886dd692d053e4a36a3a9e97 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Thu, 8 Jun 2023 14:25:44 +0200 Subject: [PATCH] Upgrade to mlua version 0.9 --- Cargo.lock | 45 +++++-- Cargo.toml | 2 +- .../lib-roblox/src/datatypes/conversion.rs | 4 +- .../src/datatypes/types/brick_color.rs | 4 +- .../lib-roblox/src/datatypes/types/cframe.rs | 118 ++++++++++++------ .../lib-roblox/src/datatypes/types/color3.rs | 23 ++-- .../src/datatypes/types/color_sequence.rs | 24 ++-- .../types/color_sequence_keypoint.rs | 7 +- .../lib-roblox/src/datatypes/types/font.rs | 10 +- .../src/datatypes/types/number_sequence.rs | 6 +- .../datatypes/types/physical_properties.rs | 2 +- .../lib-roblox/src/datatypes/types/ray.rs | 18 +-- .../lib-roblox/src/datatypes/types/rect.rs | 9 +- .../lib-roblox/src/datatypes/types/region3.rs | 14 ++- .../src/datatypes/types/region3int16.rs | 14 ++- .../lib-roblox/src/datatypes/types/udim2.rs | 48 +++---- .../lib-roblox/src/datatypes/types/vector2.rs | 17 ++- .../lib-roblox/src/datatypes/types/vector3.rs | 38 +++--- packages/lib-roblox/src/instance/mod.rs | 79 ++++++------ packages/lib-roblox/src/lib.rs | 2 +- packages/lib-roblox/src/shared/classes.rs | 6 +- packages/lib-roblox/src/shared/userdata.rs | 8 +- packages/lib/src/builtins/fs.rs | 2 +- packages/lib/src/builtins/net.rs | 10 +- packages/lib/src/builtins/process.rs | 4 +- packages/lib/src/builtins/roblox.rs | 26 ++-- packages/lib/src/builtins/serde.rs | 4 +- packages/lib/src/builtins/task.rs | 10 +- packages/lib/src/builtins/top_level.rs | 7 +- packages/lib/src/importer/require.rs | 23 ++-- packages/lib/src/lib.rs | 4 +- packages/lib/src/lua/async_ext.rs | 14 +-- packages/lib/src/lua/create.rs | 4 +- packages/lib/src/lua/net/response.rs | 6 +- packages/lib/src/lua/net/websocket.rs | 41 +++--- packages/lib/src/lua/serde/encode_decode.rs | 2 +- packages/lib/src/lua/stdio/formatting.rs | 4 +- packages/lib/src/lua/stdio/prompt.rs | 6 +- packages/lib/src/lua/table/builder.rs | 20 +-- packages/lib/src/lua/task/ext/async_ext.rs | 8 +- packages/lib/src/lua/task/proxy.rs | 16 +-- packages/lib/src/lua/task/scheduler.rs | 8 +- 42 files changed, 429 insertions(+), 288 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0f433b3..8909b9e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -290,11 +290,12 @@ dependencies = [ [[package]] name = "bstr" -version = "0.2.17" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223" +checksum = "a246e68bb43f6cd9db24bea052a53e40405417c5fb372e3d1a8a7f770a564ef5" dependencies = [ "memchr", + "serde", ] [[package]] @@ -1288,19 +1289,30 @@ dependencies = [ [[package]] name = "mlua" -version = "0.8.9" +version = "0.9.0-beta.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07366ed2cd22a3b000aed076e2b68896fb46f06f1f5786c5962da73c0af01577" +checksum = "19b5bc62c9f83dc5c6bb36714e30aceca2fb3ac5c91a21590b0cf382b22e65e0" dependencies = [ "bstr", - "cc", "erased-serde", - "luau0-src", + "mlua-sys", "num-traits", "once_cell", - "pkg-config", "rustc-hash", "serde", + "serde-value", +] + +[[package]] +name = "mlua-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b5013b291cbd5edd9259173f1ca1e62fc2f5b670c35424361acbbccddf9c679" +dependencies = [ + "cc", + "cfg-if", + "luau0-src", + "pkg-config", ] [[package]] @@ -1334,6 +1346,15 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" +[[package]] +name = "ordered-float" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7940cf2ca942593318d07fcf2596cdca60a85c9e7fab408a5e21a4f9dcd40d87" +dependencies = [ + "num-traits", +] + [[package]] name = "os_str_bytes" version = "6.5.0" @@ -1834,6 +1855,16 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-value" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3a1a3341211875ef120e117ea7fd5228530ae7e7036a779fdc9117be6b3282c" +dependencies = [ + "ordered-float", + "serde", +] + [[package]] name = "serde_derive" version = "1.0.164" diff --git a/Cargo.toml b/Cargo.toml index 981aa95..8aa40de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ directories = "5.0" futures-util = "0.3" once_cell = "1.17" -mlua = { version = "0.8", features = ["luau", "serialize"] } +mlua = { version = "0.9.0-beta.3", features = ["luau", "serialize"] } tokio = { version = "1.24", features = ["full"] } # Serde dependencies, supporting user-facing formats: json, yaml, toml diff --git a/packages/lib-roblox/src/datatypes/conversion.rs b/packages/lib-roblox/src/datatypes/conversion.rs index 9e7271d..13f41f4 100644 --- a/packages/lib-roblox/src/datatypes/conversion.rs +++ b/packages/lib-roblox/src/datatypes/conversion.rs @@ -50,7 +50,7 @@ impl<'lua> DomValueToLua<'lua> for LuaValue<'lua> { DomValue::Float64(n) => Ok(LuaValue::Number(*n)), DomValue::Float32(n) => Ok(LuaValue::Number(*n as f64)), DomValue::String(s) => Ok(LuaValue::String(lua.create_string(s)?)), - DomValue::BinaryString(s) => Ok(LuaValue::String(lua.create_string(&s)?)), + DomValue::BinaryString(s) => Ok(LuaValue::String(lua.create_string(s)?)), DomValue::Content(s) => Ok(LuaValue::String( lua.create_string(AsRef::::as_ref(s))?, )), @@ -59,7 +59,7 @@ impl<'lua> DomValueToLua<'lua> for LuaValue<'lua> { // no longer exist, so we handle that here instead of // in the userdata conversion to be able to return nils DomValue::Ref(value) => match Instance::new_opt(*value) { - Some(inst) => Ok(inst.to_lua(lua)?), + Some(inst) => Ok(inst.into_lua(lua)?), None => Ok(LuaValue::Nil), }, diff --git a/packages/lib-roblox/src/datatypes/types/brick_color.rs b/packages/lib-roblox/src/datatypes/types/brick_color.rs index 66d2def..a6aaf60 100644 --- a/packages/lib-roblox/src/datatypes/types/brick_color.rs +++ b/packages/lib-roblox/src/datatypes/types/brick_color.rs @@ -25,7 +25,7 @@ impl BrickColor { type ArgsNumber = u16; type ArgsName = String; type ArgsRgb = (u8, u8, u8); - type ArgsColor3 = Color3; + type ArgsColor3<'lua> = LuaUserDataRef<'lua, Color3>; datatype_table.set( "new", lua.create_function(|lua, args: LuaMultiValue| { @@ -36,7 +36,7 @@ impl BrickColor { } else if let Ok((r, g, b)) = ArgsRgb::from_lua_multi(args.clone(), lua) { Ok(color_from_rgb(r, g, b)) } else if let Ok(color) = ArgsColor3::from_lua_multi(args.clone(), lua) { - Ok(Self::from(color)) + Ok(Self::from(*color)) } else { // FUTURE: Better error message here using given arg types Err(LuaError::RuntimeError( diff --git a/packages/lib-roblox/src/datatypes/types/cframe.rs b/packages/lib-roblox/src/datatypes/types/cframe.rs index b46c090..a9b3c5e 100644 --- a/packages/lib-roblox/src/datatypes/types/cframe.rs +++ b/packages/lib-roblox/src/datatypes/types/cframe.rs @@ -42,13 +42,20 @@ impl CFrame { // Strict args constructors datatype_table.set( "lookAt", - lua.create_function(|_, (from, to, up): (Vector3, Vector3, Option)| { - Ok(CFrame(look_at( - from.0, - to.0, - up.unwrap_or(Vector3(Vec3::Y)).0, - ))) - })?, + lua.create_function( + |_, + (from, to, up): ( + LuaUserDataRef, + LuaUserDataRef, + Option>, + )| { + Ok(CFrame(look_at( + from.0, + to.0, + up.as_deref().unwrap_or(&Vector3(Vec3::Y)).0, + ))) + }, + )?, )?; datatype_table.set( "fromEulerAnglesXYZ", @@ -76,14 +83,20 @@ impl CFrame { )?; datatype_table.set( "fromAxisAngle", - lua.create_function(|_, (v, r): (Vector3, f32)| { + lua.create_function(|_, (v, r): (LuaUserDataRef, f32)| { Ok(CFrame(Mat4::from_axis_angle(v.0, r))) })?, )?; datatype_table.set( "fromMatrix", lua.create_function( - |_, (pos, rx, ry, rz): (Vector3, Vector3, Vector3, Option)| { + |_, + (pos, rx, ry, rz): ( + LuaUserDataRef, + LuaUserDataRef, + LuaUserDataRef, + Option>, + )| { Ok(CFrame(Mat4::from_cols( rx.0.extend(0.0), ry.0.extend(0.0), @@ -96,8 +109,12 @@ impl CFrame { )?, )?; // Dynamic args constructor - type ArgsPos = Vector3; - type ArgsLook = (Vector3, Vector3, Option); + type ArgsPos<'lua> = LuaUserDataRef<'lua, Vector3>; + type ArgsLook<'lua> = ( + LuaUserDataRef<'lua, Vector3>, + LuaUserDataRef<'lua, Vector3>, + Option>, + ); type ArgsPosXYZ = (f32, f32, f32); type ArgsPosXYZQuat = (f32, f32, f32, f32, f32, f32, f32); type ArgsMatrix = (f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32); @@ -112,7 +129,7 @@ impl CFrame { Ok(CFrame(look_at( from.0, to.0, - up.unwrap_or(Vector3(Vec3::Y)).0, + up.as_deref().unwrap_or(&Vector3(Vec3::Y)).0, ))) } else if let Ok((x, y, z)) = ArgsPosXYZ::from_lua_multi(args.clone(), lua) { Ok(CFrame(Mat4::from_translation(Vec3::new(x, y, z)))) @@ -168,20 +185,23 @@ impl LuaUserData for CFrame { fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { // Methods methods.add_method("Inverse", |_, this, ()| Ok(this.inverse())); - methods.add_method("Lerp", |_, this, (goal, alpha): (CFrame, f32)| { - let quat_this = Quat::from_mat4(&this.0); - let quat_goal = Quat::from_mat4(&goal.0); - let translation = this - .0 - .w_axis - .truncate() - .lerp(goal.0.w_axis.truncate(), alpha); - let rotation = quat_this.slerp(quat_goal, alpha); - Ok(CFrame(Mat4::from_rotation_translation( - rotation, - translation, - ))) - }); + methods.add_method( + "Lerp", + |_, this, (goal, alpha): (LuaUserDataRef, f32)| { + let quat_this = Quat::from_mat4(&this.0); + let quat_goal = Quat::from_mat4(&goal.0); + let translation = this + .0 + .w_axis + .truncate() + .lerp(goal.0.w_axis.truncate(), alpha); + let rotation = quat_this.slerp(quat_goal, alpha); + Ok(CFrame(Mat4::from_rotation_translation( + rotation, + translation, + ))) + }, + ); methods.add_method("Orthonormalize", |_, this, ()| { let rotation = Quat::from_mat4(&this.0); let translation = this.0.w_axis.truncate(); @@ -190,21 +210,31 @@ impl LuaUserData for CFrame { translation, ))) }); - methods.add_method("ToWorldSpace", |_, this, rhs: CFrame| Ok(*this * rhs)); - methods.add_method("ToObjectSpace", |_, this, rhs: CFrame| { - Ok(this.inverse() * rhs) + methods.add_method("ToWorldSpace", |_, this, rhs: LuaUserDataRef| { + Ok(*this * *rhs) }); - methods.add_method("PointToWorldSpace", |_, this, rhs: Vector3| Ok(*this * rhs)); - methods.add_method("PointToObjectSpace", |_, this, rhs: Vector3| { - Ok(this.inverse() * rhs) - }); - methods.add_method("VectorToWorldSpace", |_, this, rhs: Vector3| { - Ok((*this - Vector3(this.position())) * rhs) - }); - methods.add_method("VectorToObjectSpace", |_, this, rhs: Vector3| { - let inv = this.inverse(); - Ok((inv - Vector3(inv.position())) * rhs) + methods.add_method("ToObjectSpace", |_, this, rhs: LuaUserDataRef| { + Ok(this.inverse() * *rhs) }); + methods.add_method( + "PointToWorldSpace", + |_, this, rhs: LuaUserDataRef| Ok(*this * *rhs), + ); + methods.add_method( + "PointToObjectSpace", + |_, this, rhs: LuaUserDataRef| Ok(this.inverse() * *rhs), + ); + methods.add_method( + "VectorToWorldSpace", + |_, this, rhs: LuaUserDataRef| Ok((*this - Vector3(this.position())) * *rhs), + ); + methods.add_method( + "VectorToObjectSpace", + |_, this, rhs: LuaUserDataRef| { + let inv = this.inverse(); + Ok((inv - Vector3(inv.position())) * *rhs) + }, + ); #[rustfmt::skip] methods.add_method("GetComponents", |_, this, ()| { let pos = this.position(); @@ -251,8 +281,14 @@ impl LuaUserData for CFrame { )), }) }); - methods.add_meta_method(LuaMetaMethod::Add, |_, this, vec: Vector3| Ok(*this + vec)); - methods.add_meta_method(LuaMetaMethod::Sub, |_, this, vec: Vector3| Ok(*this - vec)); + methods.add_meta_method( + LuaMetaMethod::Add, + |_, this, vec: LuaUserDataRef| Ok(*this + *vec), + ); + methods.add_meta_method( + LuaMetaMethod::Sub, + |_, this, vec: LuaUserDataRef| Ok(*this - *vec), + ); } } diff --git a/packages/lib-roblox/src/datatypes/types/color3.rs b/packages/lib-roblox/src/datatypes/types/color3.rs index 26a84e8..c96e092 100644 --- a/packages/lib-roblox/src/datatypes/types/color3.rs +++ b/packages/lib-roblox/src/datatypes/types/color3.rs @@ -116,16 +116,19 @@ impl LuaUserData for Color3 { fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { // Methods - methods.add_method("Lerp", |_, this, (rhs, alpha): (Color3, f32)| { - let v3_this = Vec3::new(this.r, this.g, this.b); - let v3_rhs = Vec3::new(rhs.r, rhs.g, rhs.b); - let v3 = v3_this.lerp(v3_rhs, alpha); - Ok(Color3 { - r: v3.x, - g: v3.y, - b: v3.z, - }) - }); + methods.add_method( + "Lerp", + |_, this, (rhs, alpha): (LuaUserDataRef, f32)| { + let v3_this = Vec3::new(this.r, this.g, this.b); + let v3_rhs = Vec3::new(rhs.r, rhs.g, rhs.b); + let v3 = v3_this.lerp(v3_rhs, alpha); + Ok(Color3 { + r: v3.x, + g: v3.y, + b: v3.z, + }) + }, + ); methods.add_method("ToHSV", |_, this, ()| { // https://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c let (r, g, b) = (this.r, this.g, this.b); diff --git a/packages/lib-roblox/src/datatypes/types/color_sequence.rs b/packages/lib-roblox/src/datatypes/types/color_sequence.rs index 97f058a..154aebc 100644 --- a/packages/lib-roblox/src/datatypes/types/color_sequence.rs +++ b/packages/lib-roblox/src/datatypes/types/color_sequence.rs @@ -19,17 +19,23 @@ pub struct ColorSequence { impl ColorSequence { pub(crate) fn make_table(lua: &Lua, datatype_table: &LuaTable) -> LuaResult<()> { - type ArgsColor = Color3; - type ArgsColors = (Color3, Color3); - type ArgsKeypoints = Vec; + type ArgsColor<'lua> = LuaUserDataRef<'lua, Color3>; + type ArgsColors<'lua> = (LuaUserDataRef<'lua, Color3>, LuaUserDataRef<'lua, Color3>); + type ArgsKeypoints<'lua> = Vec>; datatype_table.set( "new", lua.create_function(|lua, args: LuaMultiValue| { if let Ok(color) = ArgsColor::from_lua_multi(args.clone(), lua) { Ok(ColorSequence { keypoints: vec![ - ColorSequenceKeypoint { time: 0.0, color }, - ColorSequenceKeypoint { time: 1.0, color }, + ColorSequenceKeypoint { + time: 0.0, + color: *color, + }, + ColorSequenceKeypoint { + time: 1.0, + color: *color, + }, ], }) } else if let Ok((c0, c1)) = ArgsColors::from_lua_multi(args.clone(), lua) { @@ -37,16 +43,18 @@ impl ColorSequence { keypoints: vec![ ColorSequenceKeypoint { time: 0.0, - color: c0, + color: *c0, }, ColorSequenceKeypoint { time: 1.0, - color: c1, + color: *c1, }, ], }) } else if let Ok(keypoints) = ArgsKeypoints::from_lua_multi(args, lua) { - Ok(ColorSequence { keypoints }) + Ok(ColorSequence { + keypoints: keypoints.iter().map(|k| **k).collect(), + }) } else { // FUTURE: Better error message here using given arg types Err(LuaError::RuntimeError( diff --git a/packages/lib-roblox/src/datatypes/types/color_sequence_keypoint.rs b/packages/lib-roblox/src/datatypes/types/color_sequence_keypoint.rs index c8ec640..201bde7 100644 --- a/packages/lib-roblox/src/datatypes/types/color_sequence_keypoint.rs +++ b/packages/lib-roblox/src/datatypes/types/color_sequence_keypoint.rs @@ -20,8 +20,11 @@ impl ColorSequenceKeypoint { pub(crate) fn make_table(lua: &Lua, datatype_table: &LuaTable) -> LuaResult<()> { datatype_table.set( "new", - lua.create_function(|_, (time, color): (f32, Color3)| { - Ok(ColorSequenceKeypoint { time, color }) + lua.create_function(|_, (time, color): (f32, LuaUserDataRef)| { + Ok(ColorSequenceKeypoint { + time, + color: *color, + }) })?, )?; Ok(()) diff --git a/packages/lib-roblox/src/datatypes/types/font.rs b/packages/lib-roblox/src/datatypes/types/font.rs index 2bf035a..8c5a19d 100644 --- a/packages/lib-roblox/src/datatypes/types/font.rs +++ b/packages/lib-roblox/src/datatypes/types/font.rs @@ -51,7 +51,7 @@ impl Font { )?; datatype_table.set( "fromEnum", - lua.create_function(|_, value: EnumItem| { + lua.create_function(|_, value: LuaUserDataRef| { if value.parent.desc.name == "Font" { match Font::from_enum_item(&value) { Some(props) => Ok(props), @@ -308,8 +308,8 @@ impl<'lua> FromLua<'lua> for FontWeight { } } -impl<'lua> ToLua<'lua> for FontWeight { - fn to_lua(self, lua: &'lua Lua) -> LuaResult> { +impl<'lua> IntoLua<'lua> for FontWeight { + fn into_lua(self, lua: &'lua Lua) -> LuaResult> { match EnumItem::from_enum_name_and_name("FontWeight", self.to_string()) { Some(enum_item) => Ok(LuaValue::UserData(lua.create_userdata(enum_item)?)), None => Err(LuaError::ToLuaConversionError { @@ -403,8 +403,8 @@ impl<'lua> FromLua<'lua> for FontStyle { } } -impl<'lua> ToLua<'lua> for FontStyle { - fn to_lua(self, lua: &'lua Lua) -> LuaResult> { +impl<'lua> IntoLua<'lua> for FontStyle { + fn into_lua(self, lua: &'lua Lua) -> LuaResult> { match EnumItem::from_enum_name_and_name("FontStyle", self.to_string()) { Some(enum_item) => Ok(LuaValue::UserData(lua.create_userdata(enum_item)?)), None => Err(LuaError::ToLuaConversionError { diff --git a/packages/lib-roblox/src/datatypes/types/number_sequence.rs b/packages/lib-roblox/src/datatypes/types/number_sequence.rs index 01949ca..740a2da 100644 --- a/packages/lib-roblox/src/datatypes/types/number_sequence.rs +++ b/packages/lib-roblox/src/datatypes/types/number_sequence.rs @@ -21,7 +21,7 @@ impl NumberSequence { pub(crate) fn make_table(lua: &Lua, datatype_table: &LuaTable) -> LuaResult<()> { type ArgsColor = f32; type ArgsColors = (f32, f32); - type ArgsKeypoints = Vec; + type ArgsKeypoints<'lua> = Vec>; datatype_table.set( "new", lua.create_function(|lua, args: LuaMultiValue| { @@ -56,7 +56,9 @@ impl NumberSequence { ], }) } else if let Ok(keypoints) = ArgsKeypoints::from_lua_multi(args, lua) { - Ok(NumberSequence { keypoints }) + Ok(NumberSequence { + keypoints: keypoints.iter().map(|k| **k).collect(), + }) } else { // FUTURE: Better error message here using given arg types Err(LuaError::RuntimeError( diff --git a/packages/lib-roblox/src/datatypes/types/physical_properties.rs b/packages/lib-roblox/src/datatypes/types/physical_properties.rs index 762c1d9..482a750 100644 --- a/packages/lib-roblox/src/datatypes/types/physical_properties.rs +++ b/packages/lib-roblox/src/datatypes/types/physical_properties.rs @@ -34,7 +34,7 @@ impl PhysicalProperties { } pub(crate) fn make_table(lua: &Lua, datatype_table: &LuaTable) -> LuaResult<()> { - type ArgsMaterial = EnumItem; + type ArgsMaterial<'lua> = LuaUserDataRef<'lua, EnumItem>; type ArgsNumbers = (f32, f32, f32, Option, Option); datatype_table.set( "new", diff --git a/packages/lib-roblox/src/datatypes/types/ray.rs b/packages/lib-roblox/src/datatypes/types/ray.rs index ff11ad2..c2958d1 100644 --- a/packages/lib-roblox/src/datatypes/types/ray.rs +++ b/packages/lib-roblox/src/datatypes/types/ray.rs @@ -30,12 +30,14 @@ impl Ray { pub(crate) fn make_table(lua: &Lua, datatype_table: &LuaTable) -> LuaResult<()> { datatype_table.set( "new", - lua.create_function(|_, (origin, direction): (Vector3, Vector3)| { - Ok(Ray { - origin: origin.0, - direction: direction.0, - }) - })?, + lua.create_function( + |_, (origin, direction): (LuaUserDataRef, LuaUserDataRef)| { + Ok(Ray { + origin: origin.0, + direction: direction.0, + }) + }, + )?, ) } } @@ -54,10 +56,10 @@ impl LuaUserData for Ray { fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { // Methods - methods.add_method("ClosestPoint", |_, this, to: Vector3| { + methods.add_method("ClosestPoint", |_, this, to: LuaUserDataRef| { Ok(Vector3(this.closest_point(to.0))) }); - methods.add_method("Distance", |_, this, to: Vector3| { + methods.add_method("Distance", |_, this, to: LuaUserDataRef| { let closest = this.closest_point(to.0); Ok((closest - to.0).length()) }); diff --git a/packages/lib-roblox/src/datatypes/types/rect.rs b/packages/lib-roblox/src/datatypes/types/rect.rs index df5ed3c..315def7 100644 --- a/packages/lib-roblox/src/datatypes/types/rect.rs +++ b/packages/lib-roblox/src/datatypes/types/rect.rs @@ -30,15 +30,18 @@ impl Rect { impl Rect { pub(crate) fn make_table(lua: &Lua, datatype_table: &LuaTable) -> LuaResult<()> { - type ArgsVector2s = (Option, Option); + type ArgsVector2s<'lua> = ( + Option>, + Option>, + ); type ArgsNums = (Option, Option, Option, Option); datatype_table.set( "new", lua.create_function(|lua, args: LuaMultiValue| { if let Ok((min, max)) = ArgsVector2s::from_lua_multi(args.clone(), lua) { Ok(Rect::new( - min.unwrap_or_default().0, - max.unwrap_or_default().0, + min.map(|m| *m).unwrap_or_default().0, + max.map(|m| *m).unwrap_or_default().0, )) } else if let Ok((x0, y0, x1, y1)) = ArgsNums::from_lua_multi(args, lua) { let min = Vec2::new(x0.unwrap_or_default(), y0.unwrap_or_default()); diff --git a/packages/lib-roblox/src/datatypes/types/region3.rs b/packages/lib-roblox/src/datatypes/types/region3.rs index 9f4983b..16e97ce 100644 --- a/packages/lib-roblox/src/datatypes/types/region3.rs +++ b/packages/lib-roblox/src/datatypes/types/region3.rs @@ -22,12 +22,14 @@ impl Region3 { pub(crate) fn make_table(lua: &Lua, datatype_table: &LuaTable) -> LuaResult<()> { datatype_table.set( "new", - lua.create_function(|_, (min, max): (Vector3, Vector3)| { - Ok(Region3 { - min: min.0, - max: max.0, - }) - })?, + lua.create_function( + |_, (min, max): (LuaUserDataRef, LuaUserDataRef)| { + Ok(Region3 { + min: min.0, + max: max.0, + }) + }, + )?, ) } } diff --git a/packages/lib-roblox/src/datatypes/types/region3int16.rs b/packages/lib-roblox/src/datatypes/types/region3int16.rs index f0dd3b2..887aced 100644 --- a/packages/lib-roblox/src/datatypes/types/region3int16.rs +++ b/packages/lib-roblox/src/datatypes/types/region3int16.rs @@ -22,12 +22,14 @@ impl Region3int16 { pub(crate) fn make_table(lua: &Lua, datatype_table: &LuaTable) -> LuaResult<()> { datatype_table.set( "new", - lua.create_function(|_, (min, max): (Vector3int16, Vector3int16)| { - Ok(Region3int16 { - min: min.0, - max: max.0, - }) - })?, + lua.create_function( + |_, (min, max): (LuaUserDataRef, LuaUserDataRef)| { + Ok(Region3int16 { + min: min.0, + max: max.0, + }) + }, + )?, ) } } diff --git a/packages/lib-roblox/src/datatypes/types/udim2.rs b/packages/lib-roblox/src/datatypes/types/udim2.rs index 9b34f12..597b980 100644 --- a/packages/lib-roblox/src/datatypes/types/udim2.rs +++ b/packages/lib-roblox/src/datatypes/types/udim2.rs @@ -38,15 +38,18 @@ impl UDim2 { }) })?, )?; - type ArgsUDims = (Option, Option); + type ArgsUDims<'lua> = ( + Option>, + Option>, + ); type ArgsNums = (Option, Option, Option, Option); datatype_table.set( "new", lua.create_function(|lua, args: LuaMultiValue| { if let Ok((x, y)) = ArgsUDims::from_lua_multi(args.clone(), lua) { Ok(UDim2 { - x: x.unwrap_or_default(), - y: y.unwrap_or_default(), + x: x.map(|x| *x).unwrap_or_default(), + y: y.map(|y| *y).unwrap_or_default(), }) } else if let Ok((sx, ox, sy, oy)) = ArgsNums::from_lua_multi(args, lua) { Ok(UDim2 { @@ -74,27 +77,30 @@ impl LuaUserData for UDim2 { fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { // Methods - methods.add_method("Lerp", |_, this, (goal, alpha): (UDim2, f32)| { - let this_x = Vec2::new(this.x.scale, this.x.offset as f32); - let goal_x = Vec2::new(goal.x.scale, goal.x.offset as f32); + methods.add_method( + "Lerp", + |_, this, (goal, alpha): (LuaUserDataRef, f32)| { + let this_x = Vec2::new(this.x.scale, this.x.offset as f32); + let goal_x = Vec2::new(goal.x.scale, goal.x.offset as f32); - let this_y = Vec2::new(this.y.scale, this.y.offset as f32); - let goal_y = Vec2::new(goal.y.scale, goal.y.offset as f32); + let this_y = Vec2::new(this.y.scale, this.y.offset as f32); + let goal_y = Vec2::new(goal.y.scale, goal.y.offset as f32); - let x = this_x.lerp(goal_x, alpha); - let y = this_y.lerp(goal_y, alpha); + let x = this_x.lerp(goal_x, alpha); + let y = this_y.lerp(goal_y, alpha); - Ok(UDim2 { - x: UDim { - scale: x.x, - offset: x.y.clamp(i32::MIN as f32, i32::MAX as f32).round() as i32, - }, - y: UDim { - scale: y.x, - offset: y.y.clamp(i32::MIN as f32, i32::MAX as f32).round() as i32, - }, - }) - }); + Ok(UDim2 { + x: UDim { + scale: x.x, + offset: x.y.clamp(i32::MIN as f32, i32::MAX as f32).round() as i32, + }, + y: UDim { + scale: y.x, + offset: y.y.clamp(i32::MIN as f32, i32::MAX as f32).round() as i32, + }, + }) + }, + ); // Metamethods methods.add_meta_method(LuaMetaMethod::Eq, userdata_impl_eq); methods.add_meta_method(LuaMetaMethod::ToString, userdata_impl_to_string); diff --git a/packages/lib-roblox/src/datatypes/types/vector2.rs b/packages/lib-roblox/src/datatypes/types/vector2.rs index 5f6b1f3..78a0a54 100644 --- a/packages/lib-roblox/src/datatypes/types/vector2.rs +++ b/packages/lib-roblox/src/datatypes/types/vector2.rs @@ -47,19 +47,24 @@ impl LuaUserData for Vector2 { fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { // Methods - methods.add_method("Cross", |_, this, rhs: Vector2| { + methods.add_method("Cross", |_, this, rhs: LuaUserDataRef| { let this_v3 = Vec3::new(this.0.x, this.0.y, 0f32); let rhs_v3 = Vec3::new(rhs.0.x, rhs.0.y, 0f32); Ok(this_v3.cross(rhs_v3).z) }); - methods.add_method("Dot", |_, this, rhs: Vector2| Ok(this.0.dot(rhs.0))); - methods.add_method("Lerp", |_, this, (rhs, alpha): (Vector2, f32)| { - Ok(Vector2(this.0.lerp(rhs.0, alpha))) + methods.add_method("Dot", |_, this, rhs: LuaUserDataRef| { + Ok(this.0.dot(rhs.0)) }); - methods.add_method("Max", |_, this, rhs: Vector2| { + methods.add_method( + "Lerp", + |_, this, (rhs, alpha): (LuaUserDataRef, f32)| { + Ok(Vector2(this.0.lerp(rhs.0, alpha))) + }, + ); + methods.add_method("Max", |_, this, rhs: LuaUserDataRef| { Ok(Vector2(this.0.max(rhs.0))) }); - methods.add_method("Min", |_, this, rhs: Vector2| { + methods.add_method("Min", |_, this, rhs: LuaUserDataRef| { Ok(Vector2(this.0.min(rhs.0))) }); // Metamethods diff --git a/packages/lib-roblox/src/datatypes/types/vector3.rs b/packages/lib-roblox/src/datatypes/types/vector3.rs index abd1c07..f275a41 100644 --- a/packages/lib-roblox/src/datatypes/types/vector3.rs +++ b/packages/lib-roblox/src/datatypes/types/vector3.rs @@ -31,7 +31,7 @@ impl Vector3 { // Constructors datatype_table.set( "fromAxis", - lua.create_function(|_, normal_id: EnumItem| { + lua.create_function(|_, normal_id: LuaUserDataRef| { if normal_id.parent.desc.name == "Axis" { Ok(match normal_id.name.as_str() { "X" => Vector3(Vec3::X), @@ -54,7 +54,7 @@ impl Vector3 { )?; datatype_table.set( "fromNormalId", - lua.create_function(|_, normal_id: EnumItem| { + lua.create_function(|_, normal_id: LuaUserDataRef| { if normal_id.parent.desc.name == "NormalId" { Ok(match normal_id.name.as_str() { "Left" => Vector3(Vec3::X), @@ -102,26 +102,34 @@ impl LuaUserData for Vector3 { fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { // Methods - methods.add_method("Angle", |_, this, rhs: Vector3| { + methods.add_method("Angle", |_, this, rhs: LuaUserDataRef| { Ok(this.0.angle_between(rhs.0)) }); - methods.add_method("Cross", |_, this, rhs: Vector3| { + methods.add_method("Cross", |_, this, rhs: LuaUserDataRef| { Ok(Vector3(this.0.cross(rhs.0))) }); - methods.add_method("Dot", |_, this, rhs: Vector3| Ok(this.0.dot(rhs.0))); - methods.add_method("FuzzyEq", |_, this, (rhs, epsilon): (Vector3, f32)| { - let eq_x = (rhs.0.x - this.0.x).abs() <= epsilon; - let eq_y = (rhs.0.y - this.0.y).abs() <= epsilon; - let eq_z = (rhs.0.z - this.0.z).abs() <= epsilon; - Ok(eq_x && eq_y && eq_z) + methods.add_method("Dot", |_, this, rhs: LuaUserDataRef| { + Ok(this.0.dot(rhs.0)) }); - methods.add_method("Lerp", |_, this, (rhs, alpha): (Vector3, f32)| { - Ok(Vector3(this.0.lerp(rhs.0, alpha))) - }); - methods.add_method("Max", |_, this, rhs: Vector3| { + methods.add_method( + "FuzzyEq", + |_, this, (rhs, epsilon): (LuaUserDataRef, f32)| { + let eq_x = (rhs.0.x - this.0.x).abs() <= epsilon; + let eq_y = (rhs.0.y - this.0.y).abs() <= epsilon; + let eq_z = (rhs.0.z - this.0.z).abs() <= epsilon; + Ok(eq_x && eq_y && eq_z) + }, + ); + methods.add_method( + "Lerp", + |_, this, (rhs, alpha): (LuaUserDataRef, f32)| { + Ok(Vector3(this.0.lerp(rhs.0, alpha))) + }, + ); + methods.add_method("Max", |_, this, rhs: LuaUserDataRef| { Ok(Vector3(this.0.max(rhs.0))) }); - methods.add_method("Min", |_, this, rhs: Vector3| { + methods.add_method("Min", |_, this, rhs: LuaUserDataRef| { Ok(Vector3(this.0.min(rhs.0))) }); // Metamethods diff --git a/packages/lib-roblox/src/instance/mod.rs b/packages/lib-roblox/src/instance/mod.rs index 0eb302f..ac63c1c 100644 --- a/packages/lib-roblox/src/instance/mod.rs +++ b/packages/lib-roblox/src/instance/mod.rs @@ -830,7 +830,7 @@ impl Instance { "new", lua.create_function(|lua, class_name: String| { if class_exists(&class_name) { - Instance::new_orphaned(class_name).to_lua(lua) + Instance::new_orphaned(class_name).into_lua(lua) } else { Err(LuaError::RuntimeError(format!( "Failed to create Instance - '{}' is not a valid class name", @@ -870,12 +870,12 @@ impl LuaUserData for Instance { this.ensure_not_destroyed()?; match prop_name.as_str() { - "ClassName" => return this.get_class_name().to_lua(lua), + "ClassName" => return this.get_class_name().into_lua(lua), "Name" => { - return this.get_name().to_lua(lua); + return this.get_name().into_lua(lua); } "Parent" => { - return this.get_parent().to_lua(lua); + return this.get_parent().into_lua(lua); } _ => {} } @@ -896,7 +896,7 @@ impl LuaUserData for Instance { prop_name, enum_name, enum_value.to_u32() )) })? - .to_lua(lua) + .into_lua(lua) } else { Ok(LuaValue::dom_value_to_lua(lua, &prop)?) } @@ -908,7 +908,7 @@ impl LuaUserData for Instance { prop_name, enum_name, enum_value )) })? - .to_lua(lua) + .into_lua(lua) } else if let Some(prop_default) = info.value_default { Ok(LuaValue::dom_value_to_lua(lua, prop_default)?) } else if info.value_type.is_some() { @@ -967,9 +967,9 @@ impl LuaUserData for Instance { prop_name ))); } - type Parent = Option; + type Parent<'lua> = Option>; let parent = Parent::from_lua(prop_value, lua)?; - this.set_parent(parent); + this.set_parent(parent.map(|p| p.clone())); return Ok(()); } _ => {} @@ -986,9 +986,12 @@ impl LuaUserData for Instance { }; if let Some(enum_name) = info.enum_name { - match EnumItem::from_lua(prop_value, lua) { + match LuaUserDataRef::::from_lua(prop_value, lua) { Ok(given_enum) if given_enum.parent.desc.name == enum_name => { - this.set_property(prop_name, DomValue::Enum(given_enum.into())); + this.set_property( + prop_name, + DomValue::Enum((*given_enum).clone().into()), + ); Ok(()) } Ok(given_enum) => Err(LuaError::RuntimeError(format!( @@ -1022,7 +1025,7 @@ impl LuaUserData for Instance { */ methods.add_method("Clone", |lua, this, ()| { this.ensure_not_destroyed()?; - this.clone_instance().to_lua(lua) + this.clone_instance().into_lua(lua) }); methods.add_method_mut("Destroy", |_, this, ()| { this.destroy(); @@ -1034,26 +1037,26 @@ impl LuaUserData for Instance { }); methods.add_method("GetChildren", |lua, this, ()| { this.ensure_not_destroyed()?; - this.get_children().to_lua(lua) + this.get_children().into_lua(lua) }); methods.add_method("GetDescendants", |lua, this, ()| { this.ensure_not_destroyed()?; - this.get_descendants().to_lua(lua) + this.get_descendants().into_lua(lua) }); methods.add_method("GetFullName", |lua, this, ()| { this.ensure_not_destroyed()?; - this.get_full_name().to_lua(lua) + this.get_full_name().into_lua(lua) }); methods.add_method("FindFirstAncestor", |lua, this, name: String| { this.ensure_not_destroyed()?; - this.find_ancestor(|child| child.name == name).to_lua(lua) + this.find_ancestor(|child| child.name == name).into_lua(lua) }); methods.add_method( "FindFirstAncestorOfClass", |lua, this, class_name: String| { this.ensure_not_destroyed()?; this.find_ancestor(|child| child.class == class_name) - .to_lua(lua) + .into_lua(lua) }, ); methods.add_method( @@ -1061,7 +1064,7 @@ impl LuaUserData for Instance { |lua, this, class_name: String| { this.ensure_not_destroyed()?; this.find_ancestor(|child| class_is_a(&child.class, &class_name).unwrap_or(false)) - .to_lua(lua) + .into_lua(lua) }, ); methods.add_method( @@ -1070,9 +1073,9 @@ impl LuaUserData for Instance { this.ensure_not_destroyed()?; let predicate = |child: &DomInstance| child.name == name; if matches!(recursive, Some(true)) { - this.find_descendant(predicate).to_lua(lua) + this.find_descendant(predicate).into_lua(lua) } else { - this.find_child(predicate).to_lua(lua) + this.find_child(predicate).into_lua(lua) } }, ); @@ -1082,9 +1085,9 @@ impl LuaUserData for Instance { this.ensure_not_destroyed()?; let predicate = |child: &DomInstance| child.class == class_name; if matches!(recursive, Some(true)) { - this.find_descendant(predicate).to_lua(lua) + this.find_descendant(predicate).into_lua(lua) } else { - this.find_child(predicate).to_lua(lua) + this.find_child(predicate).into_lua(lua) } }, ); @@ -1095,9 +1098,9 @@ impl LuaUserData for Instance { let predicate = |child: &DomInstance| class_is_a(&child.class, &class_name).unwrap_or(false); if matches!(recursive, Some(true)) { - this.find_descendant(predicate).to_lua(lua) + this.find_descendant(predicate).into_lua(lua) } else { - this.find_child(predicate).to_lua(lua) + this.find_child(predicate).into_lua(lua) } }, ); @@ -1105,18 +1108,24 @@ impl LuaUserData for Instance { this.ensure_not_destroyed()?; Ok(class_is_a(&this.class_name, class_name).unwrap_or(false)) }); - methods.add_method("IsAncestorOf", |_, this, instance: Instance| { - this.ensure_not_destroyed()?; - Ok(instance - .find_ancestor(|ancestor| ancestor.referent() == this.dom_ref) - .is_some()) - }); - methods.add_method("IsDescendantOf", |_, this, instance: Instance| { - this.ensure_not_destroyed()?; - Ok(this - .find_ancestor(|ancestor| ancestor.referent() == instance.dom_ref) - .is_some()) - }); + methods.add_method( + "IsAncestorOf", + |_, this, instance: LuaUserDataRef| { + this.ensure_not_destroyed()?; + Ok(instance + .find_ancestor(|ancestor| ancestor.referent() == this.dom_ref) + .is_some()) + }, + ); + methods.add_method( + "IsDescendantOf", + |_, this, instance: LuaUserDataRef| { + this.ensure_not_destroyed()?; + Ok(this + .find_ancestor(|ancestor| ancestor.referent() == instance.dom_ref) + .is_some()) + }, + ); methods.add_method("GetAttribute", |lua, this, name: String| { this.ensure_not_destroyed()?; match this.get_attribute(name) { diff --git a/packages/lib-roblox/src/lib.rs b/packages/lib-roblox/src/lib.rs index dffb2f6..adb4331 100644 --- a/packages/lib-roblox/src/lib.rs +++ b/packages/lib-roblox/src/lib.rs @@ -48,7 +48,7 @@ fn make_all_datatypes(lua: &Lua) -> LuaResult> { // Classes ("Instance", make(lua, Instance::make_table)?), // Singletons - ("Enum", Enums.to_lua(lua)?), + ("Enum", Enums.into_lua(lua)?), ]) } diff --git a/packages/lib-roblox/src/shared/classes.rs b/packages/lib-roblox/src/shared/classes.rs index 9a2a683..4d6e7de 100644 --- a/packages/lib-roblox/src/shared/classes.rs +++ b/packages/lib-roblox/src/shared/classes.rs @@ -12,7 +12,7 @@ pub(crate) fn add_class_restricted_getter<'lua, F: LuaUserDataFields<'lua, Insta field_name: &'static str, field_getter: G, ) where - R: ToLua<'lua>, + R: IntoLua<'lua>, G: 'static + Fn(&'lua Lua, &Instance) -> LuaResult, { fields.add_field_method_get(field_name, move |lua, this| { @@ -56,7 +56,7 @@ pub(crate) fn add_class_restricted_method<'lua, M: LuaUserDataMethods<'lua, Inst method: F, ) where A: FromLuaMulti<'lua>, - R: ToLuaMulti<'lua>, + R: IntoLuaMulti<'lua>, F: 'static + Fn(&'lua Lua, &Instance, A) -> LuaResult, { methods.add_method(method_name, move |lua, this, args| { @@ -85,7 +85,7 @@ pub(crate) fn add_class_restricted_method_mut< method: F, ) where A: FromLuaMulti<'lua>, - R: ToLuaMulti<'lua>, + R: IntoLuaMulti<'lua>, F: 'static + Fn(&'lua Lua, &mut Instance, A) -> LuaResult, { methods.add_method_mut(method_name, move |lua, this, args| { diff --git a/packages/lib-roblox/src/shared/userdata.rs b/packages/lib-roblox/src/shared/userdata.rs index 71e48ce..e43fb9a 100644 --- a/packages/lib-roblox/src/shared/userdata.rs +++ b/packages/lib-roblox/src/shared/userdata.rs @@ -50,18 +50,18 @@ where Ok(-*datatype) } -pub fn userdata_impl_add(_: &Lua, datatype: &D, value: D) -> LuaResult +pub fn userdata_impl_add(_: &Lua, datatype: &D, value: LuaUserDataRef) -> LuaResult where D: LuaUserData + ops::Add + Copy, { - Ok(*datatype + value) + Ok(*datatype + *value) } -pub fn userdata_impl_sub(_: &Lua, datatype: &D, value: D) -> LuaResult +pub fn userdata_impl_sub(_: &Lua, datatype: &D, value: LuaUserDataRef) -> LuaResult where D: LuaUserData + ops::Sub + Copy, { - Ok(*datatype - value) + Ok(*datatype - *value) } pub fn userdata_impl_mul_f32(_: &Lua, datatype: &D, rhs: LuaValue) -> LuaResult diff --git a/packages/lib/src/builtins/fs.rs b/packages/lib/src/builtins/fs.rs index 7c10370..e4185b7 100644 --- a/packages/lib/src/builtins/fs.rs +++ b/packages/lib/src/builtins/fs.rs @@ -21,7 +21,7 @@ pub fn create(lua: &'static Lua) -> LuaResult { async fn fs_read_file(lua: &'static Lua, path: String) -> LuaResult { let bytes = fs::read(&path).await.map_err(LuaError::external)?; - lua.create_string(&bytes) + lua.create_string(bytes) } async fn fs_read_dir(_: &'static Lua, path: String) -> LuaResult> { diff --git a/packages/lib/src/builtins/net.rs b/packages/lib/src/builtins/net.rs index 31b58c3..d177650 100644 --- a/packages/lib/src/builtins/net.rs +++ b/packages/lib/src/builtins/net.rs @@ -61,7 +61,7 @@ fn net_json_decode<'a>(lua: &'static Lua, json: LuaString<'a>) -> LuaResult(lua: &'static Lua, config: RequestConfig<'a>) -> LuaResult> { // Create and send the request - let client: NetClient = lua.named_registry_value("net.client")?; + let client: LuaUserDataRef = lua.named_registry_value("net.client")?; let mut request = client.request(config.method, &config.url); for (query, value) in config.query { request = request.query(&[(query.to_str()?, value.to_str()?)]); @@ -195,9 +195,9 @@ fn net_url_encode<'a>( (lua_string, as_binary): (LuaString<'a>, Option), ) -> LuaResult> { if matches!(as_binary, Some(true)) { - urlencoding::encode_binary(lua_string.as_bytes()).to_lua(lua) + urlencoding::encode_binary(lua_string.as_bytes()).into_lua(lua) } else { - urlencoding::encode(lua_string.to_str()?).to_lua(lua) + urlencoding::encode(lua_string.to_str()?).into_lua(lua) } } @@ -206,10 +206,10 @@ fn net_url_decode<'a>( (lua_string, as_binary): (LuaString<'a>, Option), ) -> LuaResult> { if matches!(as_binary, Some(true)) { - urlencoding::decode_binary(lua_string.as_bytes()).to_lua(lua) + urlencoding::decode_binary(lua_string.as_bytes()).into_lua(lua) } else { urlencoding::decode(lua_string.to_str()?) .map_err(|e| LuaError::RuntimeError(format!("Encountered invalid encoding - {e}")))? - .to_lua(lua) + .into_lua(lua) } } diff --git a/packages/lib/src/builtins/process.rs b/packages/lib/src/builtins/process.rs index 360d696..09aea7f 100644 --- a/packages/lib/src/builtins/process.rs +++ b/packages/lib/src/builtins/process.rs @@ -61,13 +61,13 @@ pub fn create(lua: &'static Lua, args_vec: Vec) -> LuaResult { })?; let process_exit = lua .load(PROCESS_EXIT_IMPL_LUA) - .set_name("=process.exit")? + .set_name("=process.exit") .set_environment( TableBuilder::new(lua)? .with_value("yield", process_exit_env_yield)? .with_value("exit", process_exit_env_exit)? .build_readonly()?, - )? + ) .into_function()?; // Create the full process table TableBuilder::new(lua)? diff --git a/packages/lib/src/builtins/roblox.rs b/packages/lib/src/builtins/roblox.rs index c4184fd..58f1ff7 100644 --- a/packages/lib/src/builtins/roblox.rs +++ b/packages/lib/src/builtins/roblox.rs @@ -34,7 +34,7 @@ async fn deserialize_place<'lua>( let data_model = doc.into_data_model_instance()?; Ok::<_, DocumentError>(data_model) }); - fut.await?.to_lua(lua) + fut.await?.into_lua(lua) } async fn deserialize_model<'lua>( @@ -47,13 +47,14 @@ async fn deserialize_model<'lua>( let instance_array = doc.into_instance_array()?; Ok::<_, DocumentError>(instance_array) }); - fut.await?.to_lua(lua) + fut.await?.into_lua(lua) } -async fn serialize_place( - lua: &Lua, - (data_model, as_xml): (Instance, Option), -) -> LuaResult { +async fn serialize_place<'lua>( + lua: &'lua Lua, + (data_model, as_xml): (LuaUserDataRef<'lua, Instance>, Option), +) -> LuaResult> { + let data_model = (*data_model).clone(); let fut = unblock(move || { let doc = Document::from_data_model_instance(data_model)?; let bytes = doc.to_bytes_with_format(match as_xml { @@ -63,13 +64,14 @@ async fn serialize_place( Ok::<_, DocumentError>(bytes) }); let bytes = fut.await?; - lua.create_string(&bytes) + lua.create_string(bytes) } -async fn serialize_model( - lua: &Lua, - (instances, as_xml): (Vec, Option), -) -> LuaResult { +async fn serialize_model<'lua>( + lua: &'lua Lua, + (instances, as_xml): (Vec>, Option), +) -> LuaResult> { + let instances = instances.iter().map(|i| (*i).clone()).collect(); let fut = unblock(move || { let doc = Document::from_instance_array(instances)?; let bytes = doc.to_bytes_with_format(match as_xml { @@ -79,7 +81,7 @@ async fn serialize_model( Ok::<_, DocumentError>(bytes) }); let bytes = fut.await?; - lua.create_string(&bytes) + lua.create_string(bytes) } async fn get_auth_cookie(_: &Lua, raw: Option) -> LuaResult> { diff --git a/packages/lib/src/builtins/serde.rs b/packages/lib/src/builtins/serde.rs index 7b5cf3d..ed430e3 100644 --- a/packages/lib/src/builtins/serde.rs +++ b/packages/lib/src/builtins/serde.rs @@ -37,7 +37,7 @@ async fn serde_compress<'a>( (format, str): (CompressDecompressFormat, LuaString<'a>), ) -> LuaResult> { let bytes = compress(format, str).await?; - lua.create_string(&bytes) + lua.create_string(bytes) } async fn serde_decompress<'a>( @@ -45,5 +45,5 @@ async fn serde_decompress<'a>( (format, str): (CompressDecompressFormat, LuaString<'a>), ) -> LuaResult> { let bytes = decompress(format, str).await?; - lua.create_string(&bytes) + lua.create_string(bytes) } diff --git a/packages/lib/src/builtins/task.rs b/packages/lib/src/builtins/task.rs index 38e87c9..8abf334 100644 --- a/packages/lib/src/builtins/task.rs +++ b/packages/lib/src/builtins/task.rs @@ -33,7 +33,7 @@ pub fn create(lua: &'static Lua) -> LuaResult> { let task_spawn_env_yield: LuaFunction = lua.named_registry_value("co.yield")?; let task_spawn = lua .load(SPAWN_IMPL_LUA) - .set_name("task.spawn")? + .set_name("task.spawn") .set_environment( TableBuilder::new(lua)? .with_function("thread", |lua, _: ()| Ok(lua.current_thread()))? @@ -46,7 +46,7 @@ pub fn create(lua: &'static Lua) -> LuaResult> { }, )? .build_readonly()?, - )? + ) .into_function()?; // Functions in the built-in coroutine library also need to be // replaced, these are a bit different than the ones above because @@ -71,9 +71,9 @@ pub fn create(lua: &'static Lua) -> LuaResult> { Basic task functions */ -fn task_cancel(lua: &Lua, task: TaskReference) -> LuaResult<()> { +fn task_cancel(lua: &Lua, task: LuaUserDataRef) -> LuaResult<()> { let sched = lua.app_data_ref::<&TaskScheduler>().unwrap(); - sched.remove_task(task)?; + sched.remove_task(*task)?; Ok(()) } @@ -136,7 +136,7 @@ fn coroutine_resume<'lua>( sched.force_set_current_task(Some(current)); match result { Ok(rets) => Ok((true, rets.1)), - Err(e) => Ok((false, e.to_lua_multi(lua)?)), + Err(e) => Ok((false, e.into_lua_multi(lua)?)), } } diff --git a/packages/lib/src/builtins/top_level.rs b/packages/lib/src/builtins/top_level.rs index 1b0e092..4d72d0c 100644 --- a/packages/lib/src/builtins/top_level.rs +++ b/packages/lib/src/builtins/top_level.rs @@ -43,7 +43,7 @@ pub fn error(lua: &Lua, (arg, level): (LuaValue, Option)) -> LuaResult<()> cause: LuaError::external(format!( "{}\n{}", format_label("error"), - pretty_format_multi_value(&arg.to_lua_multi(lua)?)? + pretty_format_multi_value(&arg.into_lua_multi(lua)?)? )) .into(), }, @@ -58,8 +58,7 @@ pub fn proxy_type<'lua>(lua: &'lua Lua, value: LuaValue<'lua>) -> LuaResult("type")? - .call(value) + lua.named_registry_value::("type")?.call(value) } pub fn proxy_typeof<'lua>(lua: &'lua Lua, value: LuaValue<'lua>) -> LuaResult> { @@ -74,7 +73,7 @@ pub fn proxy_typeof<'lua>(lua: &'lua Lua, value: LuaValue<'lua>) -> LuaResult("typeof")? + lua.named_registry_value::("typeof")? .call(value) } diff --git a/packages/lib/src/importer/require.rs b/packages/lib/src/importer/require.rs index 9b21b66..516e39c 100644 --- a/packages/lib/src/importer/require.rs +++ b/packages/lib/src/importer/require.rs @@ -44,7 +44,7 @@ impl<'lua> RequireContext<'lua> { pub fn new(lua: &'lua Lua, builtins_vec: Vec<(K, V)>) -> LuaResult where K: Into, - V: ToLua<'lua>, + V: IntoLua<'lua>, { let mut pwd = current_dir() .expect("Failed to access current working directory") @@ -55,7 +55,7 @@ impl<'lua> RequireContext<'lua> { } let mut builtins = HashMap::new(); for (key, value) in builtins_vec { - builtins.insert(key.into(), value.to_lua_multi(lua)?); + builtins.insert(key.into(), value.into_lua_multi(lua)?); } Ok(Self { pwd, @@ -187,7 +187,7 @@ async fn load_file<'lua>( // Load the file into a thread let loaded_func = lua .load(&contents) - .set_name(path_relative_no_extension)? + .set_name(path_relative_no_extension) .into_function()?; let loaded_thread = lua.create_thread(loaded_func)?; // Run the thread and wait for completion using the native task scheduler waker @@ -207,7 +207,7 @@ async fn load_file<'lua>( async fn load<'lua>( lua: &'lua Lua, - context: RequireContext<'lua>, + context: LuaUserDataRef<'lua, RequireContext<'lua>>, absolute_path: String, relative_path: String, has_acquired_lock: bool, @@ -247,7 +247,7 @@ async fn load<'lua>( pub fn create(lua: &'static Lua, builtins: Vec<(K, V)>) -> LuaResult where K: Clone + Into, - V: Clone + ToLua<'static>, + V: Clone + IntoLua<'static>, { let require_context = RequireContext::new(lua, builtins)?; let require_yield: LuaFunction = lua.named_registry_value("co.yield")?; @@ -261,7 +261,12 @@ where .with_value("print", require_print)? .with_function( "load", - |lua, (context, require_source, require_path): (RequireContext, String, String)| { + |lua, + (context, require_source, require_path): ( + LuaUserDataRef, + String, + String, + )| { let (absolute_path, relative_path) = context.get_paths(require_source, require_path)?; // NOTE: We can not acquire the lock in the async part of the require @@ -274,7 +279,7 @@ where .expect("Missing task scheduler as a lua app data"); sched.queue_async_task_inherited(lua.current_thread(), None, async { let rets = fut.await?; - let mult = rets.to_lua_multi(lua)?; + let mult = rets.into_lua_multi(lua)?; Ok(Some(mult)) }) }, @@ -283,8 +288,8 @@ where let require_fn_lua = lua .load(REQUIRE_IMPL_LUA) - .set_name("require")? - .set_environment(require_env)? + .set_name("require") + .set_environment(require_env) .into_function()?; Ok(require_fn_lua) } diff --git a/packages/lib/src/lib.rs b/packages/lib/src/lib.rs index ec579cc..60f6821 100644 --- a/packages/lib/src/lib.rs +++ b/packages/lib/src/lib.rs @@ -76,10 +76,10 @@ impl Lune { // Create the main thread and schedule it let main_chunk = lua .load(script_contents.as_ref()) - .set_name(script_name.as_ref())? + .set_name(script_name.as_ref()) .into_function()?; let main_thread = lua.create_thread(main_chunk)?; - let main_thread_args = LuaValue::Nil.to_lua_multi(lua)?; + let main_thread_args = LuaValue::Nil.into_lua_multi(lua)?; sched.schedule_blocking(main_thread, main_thread_args)?; // Keep running the scheduler until there are either no tasks // left to run, or until a task requests to exit the process diff --git a/packages/lib/src/lua/async_ext.rs b/packages/lib/src/lua/async_ext.rs index 720d08e..a113835 100644 --- a/packages/lib/src/lua/async_ext.rs +++ b/packages/lib/src/lua/async_ext.rs @@ -21,7 +21,7 @@ pub trait LuaAsyncExt { fn create_async_function<'lua, A, R, F, FR>(self, func: F) -> LuaResult> where A: FromLuaMulti<'static>, - R: ToLuaMulti<'static>, + R: IntoLuaMulti<'static>, F: 'static + Fn(&'lua Lua, A) -> FR, FR: 'static + Future>; @@ -36,7 +36,7 @@ impl LuaAsyncExt for &'static Lua { fn create_async_function<'lua, A, R, F, FR>(self, func: F) -> LuaResult> where A: FromLuaMulti<'static>, - R: ToLuaMulti<'static>, + R: IntoLuaMulti<'static>, F: 'static + Fn(&'lua Lua, A) -> FR, FR: 'static + Future>, { @@ -53,7 +53,7 @@ impl LuaAsyncExt for &'static Lua { .expect("Missing task scheduler as a lua app data"); sched.queue_async_task(thread, None, async { let rets = fut.await?; - let mult = rets.to_lua_multi(lua)?; + let mult = rets.into_lua_multi(lua)?; Ok(Some(mult)) }) }, @@ -61,8 +61,8 @@ impl LuaAsyncExt for &'static Lua { .build_readonly()?; let async_func = self .load(ASYNC_IMPL_LUA) - .set_name("async")? - .set_environment(async_env)? + .set_name("async") + .set_environment(async_env) .into_function()?; Ok(async_func) } @@ -88,8 +88,8 @@ impl LuaAsyncExt for &'static Lua { .build_readonly()?; let async_func = self .load(WAIT_IMPL_LUA) - .set_name("wait")? - .set_environment(async_env)? + .set_name("wait") + .set_environment(async_env) .into_function()?; Ok(async_func) } diff --git a/packages/lib/src/lua/create.rs b/packages/lib/src/lua/create.rs index cde5f2e..4ab8dde 100644 --- a/packages/lib/src/lua/create.rs +++ b/packages/lib/src/lua/create.rs @@ -118,8 +118,8 @@ pub fn create() -> LuaResult<&'static Lua> { dbg_trace_env.set("format", string.get::<_, LuaFunction>("format")?)?; let dbg_trace_fn = lua .load(TRACE_IMPL_LUA) - .set_name("=dbg.trace")? - .set_environment(dbg_trace_env)? + .set_name("=dbg.trace") + .set_environment(dbg_trace_env) .into_function()?; lua.set_named_registry_value("dbg.trace", dbg_trace_fn)?; // Modify the _VERSION global to also contain the current version of Lune diff --git a/packages/lib/src/lua/net/response.rs b/packages/lib/src/lua/net/response.rs index aeff87c..d9dcf78 100644 --- a/packages/lib/src/lua/net/response.rs +++ b/packages/lib/src/lua/net/response.rs @@ -82,8 +82,8 @@ impl<'lua> FromLua<'lua> for NetServeResponse { } } -impl<'lua> ToLua<'lua> for NetServeResponse { - fn to_lua(self, lua: &'lua Lua) -> LuaResult> { +impl<'lua> IntoLua<'lua> for NetServeResponse { + fn into_lua(self, lua: &'lua Lua) -> LuaResult> { if self.headers.len() > i32::MAX as usize { return Err(LuaError::ToLuaConversionError { from: "NetServeResponse", @@ -91,7 +91,7 @@ impl<'lua> ToLua<'lua> for NetServeResponse { message: Some("Too many header values".to_string()), }); } - let body = self.body.map(|b| lua.create_string(&b)).transpose()?; + let body = self.body.map(|b| lua.create_string(b)).transpose()?; let headers = lua.create_table_with_capacity(0, self.headers.len() as i32)?; for (key, value) in self.headers { headers.set(key, lua.create_string(&value)?)?; diff --git a/packages/lib/src/lua/net/websocket.rs b/packages/lib/src/lua/net/websocket.rs index 0c09827..e1ce2b9 100644 --- a/packages/lib/src/lua/net/websocket.rs +++ b/packages/lib/src/lua/net/websocket.rs @@ -72,8 +72,8 @@ where env: LuaTable<'lua>, ) -> LuaResult> { lua.load(WEB_SOCKET_IMPL_LUA) - .set_name("websocket")? - .set_environment(env)? + .set_name("websocket") + .set_environment(env) .eval() } } @@ -89,11 +89,11 @@ impl NetWebSocket { .with_async_function("next", next::)? .with_value( "setmetatable", - lua.named_registry_value::<_, LuaFunction>("tab.setmeta")?, + lua.named_registry_value::("tab.setmeta")?, )? .with_value( "freeze", - lua.named_registry_value::<_, LuaFunction>("tab.freeze")?, + lua.named_registry_value::("tab.freeze")?, )? .build_readonly()?; Self::into_lua_table_with_env(lua, socket_env) @@ -111,11 +111,11 @@ impl NetWebSocket { .with_async_function("next", next::)? .with_value( "setmetatable", - lua.named_registry_value::<_, LuaFunction>("tab.setmeta")?, + lua.named_registry_value::("tab.setmeta")?, )? .with_value( "freeze", - lua.named_registry_value::<_, LuaFunction>("tab.freeze")?, + lua.named_registry_value::("tab.freeze")?, )? .build_readonly()?; Self::into_lua_table_with_env(lua, socket_env) @@ -124,7 +124,10 @@ impl NetWebSocket { impl LuaUserData for NetWebSocket {} -fn close_code(_lua: &Lua, socket: NetWebSocket) -> LuaResult +fn close_code<'lua, T>( + _lua: &'lua Lua, + socket: LuaUserDataRef<'lua, NetWebSocket>, +) -> LuaResult> where T: AsyncRead + AsyncWrite + Unpin, { @@ -134,7 +137,10 @@ where }) } -async fn close(_lua: &Lua, (socket, code): (NetWebSocket, Option)) -> LuaResult<()> +async fn close<'lua, T>( + _lua: &'lua Lua, + (socket, code): (LuaUserDataRef<'lua, NetWebSocket>, Option), +) -> LuaResult<()> where T: AsyncRead + AsyncWrite + Unpin, { @@ -154,9 +160,13 @@ where res.await.map_err(LuaError::external) } -async fn send( - _lua: &Lua, - (socket, string, as_binary): (NetWebSocket, LuaString<'_>, Option), +async fn send<'lua, T>( + _lua: &'lua Lua, + (socket, string, as_binary): ( + LuaUserDataRef<'lua, NetWebSocket>, + LuaString<'lua>, + Option, + ), ) -> LuaResult<()> where T: AsyncRead + AsyncWrite + Unpin, @@ -171,7 +181,10 @@ where ws.send(msg).await.map_err(LuaError::external) } -async fn next(lua: &Lua, socket: NetWebSocket) -> LuaResult +async fn next<'lua, T>( + lua: &'lua Lua, + socket: LuaUserDataRef<'lua, NetWebSocket>, +) -> LuaResult> where T: AsyncRead + AsyncWrite + Unpin, { @@ -188,8 +201,8 @@ where }?; while let Some(msg) = &msg { let msg_string_opt = match msg { - WsMessage::Binary(bin) => Some(lua.create_string(&bin)?), - WsMessage::Text(txt) => Some(lua.create_string(&txt)?), + WsMessage::Binary(bin) => Some(lua.create_string(bin)?), + WsMessage::Text(txt) => Some(lua.create_string(txt)?), // Stop waiting for next message if we get a close message WsMessage::Close(_) => return Ok(LuaValue::Nil), // Ignore ping/pong/frame messages, they are handled by tungstenite diff --git a/packages/lib/src/lua/serde/encode_decode.rs b/packages/lib/src/lua/serde/encode_decode.rs index a4d62e2..e753847 100644 --- a/packages/lib/src/lua/serde/encode_decode.rs +++ b/packages/lib/src/lua/serde/encode_decode.rs @@ -70,7 +70,7 @@ impl EncodeDecodeConfig { s.as_bytes().to_vec() } }; - lua.create_string(&bytes) + lua.create_string(bytes) } pub fn deserialize_from_string<'lua>( diff --git a/packages/lib/src/lua/stdio/formatting.rs b/packages/lib/src/lua/stdio/formatting.rs index ba2d40f..a9e76d8 100644 --- a/packages/lib/src/lua/stdio/formatting.rs +++ b/packages/lib/src/lua/stdio/formatting.rs @@ -286,7 +286,7 @@ pub fn pretty_format_luau_error(e: &LuaError, colorized: bool) -> String { let msg = message .clone() .map_or_else(String::new, |m| format!("\nDetails:\n\t{m}")); - format!("Failed to convert Luau type '{from}' into Rust type '{to}'!{msg}") + format!("Expected argument of type '{to}', got '{from}'!{msg}") } e => format!("{e}"), }; @@ -455,7 +455,7 @@ fn call_table_tostring_metamethod<'a>(tab: &'a LuaTable<'a>) -> Option { fn call_userdata_tostring_metamethod<'a>(tab: &'a LuaAnyUserData<'a>) -> Option { let f = match tab.get_metatable() { Err(_) => None, - Ok(meta) => match meta.get::<_, LuaFunction>(LuaMetaMethod::ToString.name()) { + Ok(meta) => match meta.get::(LuaMetaMethod::ToString.name()) { Ok(method) => Some(method), Err(_) => None, }, diff --git a/packages/lib/src/lua/stdio/prompt.rs b/packages/lib/src/lua/stdio/prompt.rs index ed940cc..9cdd899 100644 --- a/packages/lib/src/lua/stdio/prompt.rs +++ b/packages/lib/src/lua/stdio/prompt.rs @@ -179,13 +179,13 @@ pub enum PromptResult { None, } -impl<'lua> ToLua<'lua> for PromptResult { - fn to_lua(self, lua: &'lua Lua) -> LuaResult> { +impl<'lua> IntoLua<'lua> for PromptResult { + fn into_lua(self, lua: &'lua Lua) -> LuaResult> { Ok(match self { Self::String(s) => LuaValue::String(lua.create_string(&s)?), Self::Boolean(b) => LuaValue::Boolean(b), Self::Index(i) => LuaValue::Number(i as f64), - Self::Indices(v) => v.to_lua(lua)?, + Self::Indices(v) => v.into_lua(lua)?, Self::None => LuaValue::Nil, }) } diff --git a/packages/lib/src/lua/table/builder.rs b/packages/lib/src/lua/table/builder.rs index 002c841..8eb6e96 100644 --- a/packages/lib/src/lua/table/builder.rs +++ b/packages/lib/src/lua/table/builder.rs @@ -18,8 +18,8 @@ impl TableBuilder { pub fn with_value(self, key: K, value: V) -> LuaResult where - K: ToLua<'static>, - V: ToLua<'static>, + K: IntoLua<'static>, + V: IntoLua<'static>, { self.tab.raw_set(key, value)?; Ok(self) @@ -27,8 +27,8 @@ impl TableBuilder { pub fn with_values(self, values: Vec<(K, V)>) -> LuaResult where - K: ToLua<'static>, - V: ToLua<'static>, + K: IntoLua<'static>, + V: IntoLua<'static>, { for (key, value) in values { self.tab.raw_set(key, value)?; @@ -38,7 +38,7 @@ impl TableBuilder { pub fn with_sequential_value(self, value: V) -> LuaResult where - V: ToLua<'static>, + V: IntoLua<'static>, { self.tab.raw_push(value)?; Ok(self) @@ -46,7 +46,7 @@ impl TableBuilder { pub fn with_sequential_values(self, values: Vec) -> LuaResult where - V: ToLua<'static>, + V: IntoLua<'static>, { for value in values { self.tab.raw_push(value)?; @@ -61,9 +61,9 @@ impl TableBuilder { pub fn with_function(self, key: K, func: F) -> LuaResult where - K: ToLua<'static>, + K: IntoLua<'static>, A: FromLuaMulti<'static>, - R: ToLuaMulti<'static>, + R: IntoLuaMulti<'static>, F: 'static + Fn(&'static Lua, A) -> LuaResult, { let f = self.lua.create_function(func)?; @@ -72,9 +72,9 @@ impl TableBuilder { pub fn with_async_function(self, key: K, func: F) -> LuaResult where - K: ToLua<'static>, + K: IntoLua<'static>, A: FromLuaMulti<'static>, - R: ToLuaMulti<'static>, + R: IntoLuaMulti<'static>, F: 'static + Fn(&'static Lua, A) -> FR, FR: 'static + Future>, { diff --git a/packages/lib/src/lua/task/ext/async_ext.rs b/packages/lib/src/lua/task/ext/async_ext.rs index 5b9f23e..ff96a53 100644 --- a/packages/lib/src/lua/task/ext/async_ext.rs +++ b/packages/lib/src/lua/task/ext/async_ext.rs @@ -32,7 +32,7 @@ pub trait TaskSchedulerAsyncExt<'fut> { ) -> LuaResult where 'sched: 'fut, - R: ToLuaMulti<'static>, + R: IntoLuaMulti<'static>, F: 'static + Fn(&'static Lua) -> FR, FR: 'static + Future>; @@ -89,13 +89,13 @@ impl<'fut> TaskSchedulerAsyncExt<'fut> for TaskScheduler<'fut> { ) -> LuaResult where 'sched: 'fut, // Scheduler must live at least as long as the future - R: ToLuaMulti<'static>, + R: IntoLuaMulti<'static>, F: 'static + Fn(&'static Lua) -> FR, FR: 'static + Future>, { self.queue_async_task(thread, None, async move { match func(self.lua).await { - Ok(res) => match res.to_lua_multi(self.lua) { + Ok(res) => match res.into_lua_multi(self.lua) { Ok(multi) => Ok(Some(multi)), Err(e) => Err(e), }, @@ -127,7 +127,7 @@ impl<'fut> TaskSchedulerAsyncExt<'fut> for TaskScheduler<'fut> { )) .await; let elapsed_secs = before.elapsed().as_secs_f64(); - let args = elapsed_secs.to_lua_multi(self.lua).unwrap(); + let args = elapsed_secs.into_lua_multi(self.lua).unwrap(); (Some(reference), Ok(Some(args))) })); Ok(reference) diff --git a/packages/lib/src/lua/task/proxy.rs b/packages/lib/src/lua/task/proxy.rs index 4fd2762..ee48a6e 100644 --- a/packages/lib/src/lua/task/proxy.rs +++ b/packages/lib/src/lua/task/proxy.rs @@ -50,8 +50,8 @@ impl<'lua> FromLua<'lua> for LuaThreadOrFunction<'lua> { } } -impl<'lua> ToLua<'lua> for LuaThreadOrFunction<'lua> { - fn to_lua(self, _: &'lua Lua) -> LuaResult> { +impl<'lua> IntoLua<'lua> for LuaThreadOrFunction<'lua> { + fn into_lua(self, _: &'lua Lua) -> LuaResult> { match self { Self::Thread(t) => Ok(LuaValue::Thread(t)), Self::Function(f) => Ok(LuaValue::Function(f)), @@ -87,8 +87,10 @@ impl<'lua> FromLua<'lua> for LuaThreadOrTaskReference<'lua> { match value { LuaValue::Thread(t) => Ok(Self::Thread(t)), LuaValue::UserData(u) => { - if let Ok(task) = TaskReference::from_lua(LuaValue::UserData(u), lua) { - Ok(Self::TaskReference(task)) + if let Ok(task) = + LuaUserDataRef::::from_lua(LuaValue::UserData(u), lua) + { + Ok(Self::TaskReference(*task)) } else { Err(LuaError::FromLuaConversionError { from: tname, @@ -106,10 +108,10 @@ impl<'lua> FromLua<'lua> for LuaThreadOrTaskReference<'lua> { } } -impl<'lua> ToLua<'lua> for LuaThreadOrTaskReference<'lua> { - fn to_lua(self, lua: &'lua Lua) -> LuaResult> { +impl<'lua> IntoLua<'lua> for LuaThreadOrTaskReference<'lua> { + fn into_lua(self, lua: &'lua Lua) -> LuaResult> { match self { - Self::TaskReference(t) => t.to_lua(lua), + Self::TaskReference(t) => t.into_lua(lua), Self::Thread(t) => Ok(LuaValue::Thread(t)), } } diff --git a/packages/lib/src/lua/task/scheduler.rs b/packages/lib/src/lua/task/scheduler.rs index 922a4dd..c3ba01c 100644 --- a/packages/lib/src/lua/task/scheduler.rs +++ b/packages/lib/src/lua/task/scheduler.rs @@ -70,12 +70,12 @@ impl<'fut> TaskScheduler<'fut> { let (tx, rx) = mpsc::unbounded_channel(); let tasks_current_lua_error = Arc::new(AsyncMutex::new(None)); let tasks_current_lua_error_inner = tasks_current_lua_error.clone(); - lua.set_interrupt( - move || match tasks_current_lua_error_inner.try_lock().unwrap().take() { + lua.set_interrupt(move |_| { + match tasks_current_lua_error_inner.try_lock().unwrap().take() { Some(err) => Err(err), None => Ok(LuaVmState::Continue), - }, - ); + } + }); Ok(Self { lua, guid: Cell::new(0),