From 1b1691ff11dc335218e5fbe21c94d7b83fd1acc2 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 18 Apr 2021 03:06:01 +0000 Subject: [PATCH] Correctly handle methods on an enum that take self --- c-bindings-gen/src/main.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index a283c35..149ae76 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -993,12 +993,20 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ if r.mutability.is_some() { takes_mut_self = true; } } } - if takes_mut_self { - write!(w, "unsafe {{ &mut (*(this_arg.inner as *mut native{})) }}.{}(", ident, m.sig.ident).unwrap(); - } else if takes_self { - write!(w, "unsafe {{ &*this_arg.inner }}.{}(", m.sig.ident).unwrap(); - } else { + if !takes_mut_self && !takes_self { write!(w, "{}::{}(", resolved_path, m.sig.ident).unwrap(); + } else { + match &declared_type { + DeclType::MirroredEnum => write!(w, "this_arg.to_native().{}(", m.sig.ident).unwrap(), + DeclType::StructImported => { + if takes_mut_self { + write!(w, "unsafe {{ &mut (*(this_arg.inner as *mut native{})) }}.{}(", ident, m.sig.ident).unwrap(); + } else { + write!(w, "unsafe {{ &*this_arg.inner }}.{}(", m.sig.ident).unwrap(); + } + }, + _ => unimplemented!(), + } } write_method_call_params(w, &m.sig, "", types, Some(&meth_gen_types), &ret_type, false); writeln!(w, "\n}}\n").unwrap(); -- 2.30.2