Correctly handle methods on an enum that take self
authorMatt Corallo <git@bluematt.me>
Sun, 18 Apr 2021 03:06:01 +0000 (03:06 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 26 Apr 2021 17:26:03 +0000 (17:26 +0000)
c-bindings-gen/src/main.rs

index a283c35d40d41d0612836ee3befdee6725f53182..149ae76791a9c82b94355535269e4eb5ba33d31f 100644 (file)
@@ -993,12 +993,20 @@ fn writeln_impl<W: std::io::Write>(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();