Merge pull request #19 from TheBlueMatt/2021-04-invoice-incl
[ldk-c-bindings] / c-bindings-gen / src / blocks.rs
index 2c51572a9378150fdc861c12c5c9e3edec11c7aa..82b10ad831bd4ca2ef21ec3c1d9804d5dbe6e26c 100644 (file)
@@ -137,12 +137,8 @@ pub fn write_result_block<W: std::io::Write>(w: &mut W, mangled_container: &str,
        writeln!(w, "\t}}").unwrap();
        writeln!(w, "}}").unwrap();
 
-       // TODO: Templates should use () now that they can, too
-       let templ_ok_type = if ok_type != "()" { ok_type } else { "u8" };
-       let templ_err_type = if err_type != "()" { err_type } else { "u8" };
-
-       writeln!(w, "impl From<crate::c_types::CResultTempl<{}, {}>> for {} {{", templ_ok_type, templ_err_type, mangled_container).unwrap();
-       writeln!(w, "\tfn from(mut o: crate::c_types::CResultTempl<{}, {}>) -> Self {{", templ_ok_type, templ_err_type).unwrap();
+       writeln!(w, "impl From<crate::c_types::CResultTempl<{}, {}>> for {} {{", ok_type, err_type, mangled_container).unwrap();
+       writeln!(w, "\tfn from(mut o: crate::c_types::CResultTempl<{}, {}>) -> Self {{", ok_type, err_type).unwrap();
        writeln!(w, "\t\tlet contents = if o.result_ok {{").unwrap();
        if ok_type != "()" {
                writeln!(w, "\t\t\tlet result = unsafe {{ o.contents.result }};").unwrap();
@@ -442,14 +438,16 @@ pub fn write_method_params<W: std::io::Write>(w: &mut W, sig: &syn::Signature, t
        for inp in sig.inputs.iter() {
                match inp {
                        syn::FnArg::Receiver(recv) => {
-                               if !recv.attrs.is_empty() || recv.reference.is_none() { unimplemented!(); }
-                               write!(w, "this_arg: {}{}",
-                                       match (self_ptr, recv.mutability.is_some()) {
-                                               (true, true) => "*mut ",
-                                               (true, false) => "*const ",
-                                               (false, true) => "&mut ",
-                                               (false, false) => "&",
-                                       }, this_param).unwrap();
+                               if !recv.attrs.is_empty() { unimplemented!(); }
+                               write!(w, "{}this_arg: {}{}", if recv.reference.is_none() { "mut " } else { "" },
+                                       if recv.reference.is_some() {
+                                               match (self_ptr, recv.mutability.is_some()) {
+                                                       (true, true) => "*mut ",
+                                                       (true, false) => "*const ",
+                                                       (false, true) => "&mut ",
+                                                       (false, false) => "&",
+                                               }
+                                       } else { "" }, this_param).unwrap();
                                assert!(first_arg);
                                first_arg = false;
                        },
@@ -556,8 +554,9 @@ pub fn write_method_call_params<W: std::io::Write>(w: &mut W, sig: &syn::Signatu
        for inp in sig.inputs.iter() {
                match inp {
                        syn::FnArg::Receiver(recv) => {
-                               if !recv.attrs.is_empty() || recv.reference.is_none() { unimplemented!(); }
+                               if !recv.attrs.is_empty() { unimplemented!(); }
                                if to_c {
+                                       if recv.reference.is_none() { unimplemented!(); }
                                        write!(w, "self.this_arg").unwrap();
                                        first_arg = false;
                                }
@@ -645,7 +644,7 @@ pub fn write_method_call_params<W: std::io::Write>(w: &mut W, sig: &syn::Signatu
 /// Prints concrete generic parameters for a struct/trait/function, including the less-than and
 /// greater-than symbols, if any generic parameters are defined.
 pub fn maybe_write_generics<W: std::io::Write>(w: &mut W, generics: &syn::Generics, types: &TypeResolver, concrete_lifetimes: bool) {
-       let mut gen_types = GenericTypes::new();
+       let mut gen_types = GenericTypes::new(None);
        assert!(gen_types.learn_generics(generics, types));
        if !generics.params.is_empty() {
                write!(w, "<").unwrap();