From 145c5b316b335b81b8b537641806b63193803761 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Thu, 25 Jan 2024 10:26:43 +0000 Subject: [PATCH] Fix `FromLua` derive proc macro to cover more cases --- mlua_derive/src/from_lua.rs | 7 ++++--- tests/userdata.rs | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/mlua_derive/src/from_lua.rs b/mlua_derive/src/from_lua.rs index 9e311e44..3d4e4edf 100644 --- a/mlua_derive/src/from_lua.rs +++ b/mlua_derive/src/from_lua.rs @@ -7,16 +7,17 @@ pub fn from_lua(input: TokenStream) -> TokenStream { ident, generics, .. } = parse_macro_input!(input as DeriveInput); + let ident_str = ident.to_string(); + let (impl_generics, ty_generics, _) = generics.split_for_impl(); let where_clause = match &generics.where_clause { Some(where_clause) => quote! { #where_clause, Self: 'static + Clone }, None => quote! { where Self: 'static + Clone }, }; - let ident_str = ident.to_string(); quote! { - impl #generics ::mlua::FromLua<'_> for #ident #generics #where_clause { + impl #impl_generics ::mlua::FromLua<'_> for #ident #ty_generics #where_clause { #[inline] - fn from_lua(value: ::mlua::Value<'_>, lua: &'_ ::mlua::Lua) -> ::mlua::Result { + fn from_lua(value: ::mlua::Value<'_>, _: &'_ ::mlua::Lua) -> ::mlua::Result { match value { ::mlua::Value::UserData(ud) => Ok(ud.borrow::()?.clone()), _ => Err(::mlua::Error::FromLuaConversionError { diff --git a/tests/userdata.rs b/tests/userdata.rs index 141e3294..0a8d3298 100644 --- a/tests/userdata.rs +++ b/tests/userdata.rs @@ -991,9 +991,9 @@ fn test_userdata_derive() -> Result<()> { // More complex struct where generics and where clause #[derive(Clone, Copy, mlua::FromLua)] - struct MyUserData2<'a, T>(&'a T) + struct MyUserData2<'a, T: ?Sized>(&'a T) where - T: ?Sized; + T: Copy; lua.register_userdata_type::>(|reg| { reg.add_function("val", |_, this: MyUserData2<'static, i32>| Ok(*this.0));