Correctly handle methods on an enum that take self
[ldk-c-bindings] / c-bindings-gen / src / main.rs
index c869e327bcdcf7956c83772a88da08893caffc57..149ae76791a9c82b94355535269e4eb5ba33d31f 100644 (file)
@@ -606,11 +606,7 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct,
                                                let local_var = types.write_to_c_conversion_new_var(w, &format_ident!("inner_val"), &ref_type, Some(&gen_types), true);
                                                if local_var { write!(w, "\n\t").unwrap(); }
                                                types.write_to_c_conversion_inline_prefix(w, &ref_type, Some(&gen_types), true);
-                                               if local_var {
-                                                       write!(w, "inner_val").unwrap();
-                                               } else {
-                                                       write!(w, "(*inner_val)").unwrap();
-                                               }
+                                               write!(w, "inner_val").unwrap();
                                                types.write_to_c_conversion_inline_suffix(w, &ref_type, Some(&gen_types), true);
                                                writeln!(w, "\n}}").unwrap();
                                        }
@@ -997,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();
@@ -1486,20 +1490,17 @@ fn walk_ast<'a>(ast_storage: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a
                                                if process_alias {
                                                        match &*t.ty {
                                                                syn::Type::Path(p) => {
+                                                                       let t_ident = &t.ident;
+
                                                                        // If its a path with no generics, assume we don't map the aliased type and map it opaque
-                                                                       let mut segments = syn::punctuated::Punctuated::new();
-                                                                       segments.push(syn::PathSegment {
-                                                                               ident: t.ident.clone(),
-                                                                               arguments: syn::PathArguments::None,
-                                                                       });
-                                                                       let path_obj = syn::Path { leading_colon: None, segments };
+                                                                       let path_obj = parse_quote!(#t_ident);
                                                                        let args_obj = p.path.segments.last().unwrap().arguments.clone();
                                                                        match crate_types.reverse_alias_map.entry(import_resolver.maybe_resolve_path(&p.path, None).unwrap()) {
                                                                                hash_map::Entry::Occupied(mut e) => { e.get_mut().push((path_obj, args_obj)); },
                                                                                hash_map::Entry::Vacant(e) => { e.insert(vec![(path_obj, args_obj)]); },
                                                                        }
 
-                                                                       crate_types.opaques.insert(type_path.clone(), &t.ident);
+                                                                       crate_types.opaques.insert(type_path, t_ident);
                                                                },
                                                                _ => {
                                                                        crate_types.type_aliases.insert(type_path, import_resolver.resolve_imported_refs((*t.ty).clone()));