Skip to content

Commit

Permalink
Fix FromLua derive proc macro to cover more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
khvzak committed Jan 25, 2024
1 parent e97e69a commit 145c5b3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions mlua_derive/src/from_lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
fn from_lua(value: ::mlua::Value<'_>, _: &'_ ::mlua::Lua) -> ::mlua::Result<Self> {
match value {
::mlua::Value::UserData(ud) => Ok(ud.borrow::<Self>()?.clone()),
_ => Err(::mlua::Error::FromLuaConversionError {
Expand Down
4 changes: 2 additions & 2 deletions tests/userdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<MyUserData2<'static, i32>>(|reg| {
reg.add_function("val", |_, this: MyUserData2<'static, i32>| Ok(*this.0));
Expand Down

0 comments on commit 145c5b3

Please sign in to comment.