Expose struct method calls on trait structs to C++ directly
[ldk-c-bindings] / c-bindings-gen / src / blocks.rs
index 88172722efbda088aef61443fba43e2af6515e07..643a8be7c3c1fdb5052dde93872d3b14456c3e95 100644 (file)
@@ -20,7 +20,7 @@ use crate::types::*;
 /// Writes out a C++ wrapper class for the given type, which contains various utilities to access
 /// the underlying C-mapped type safely avoiding some common memory management issues by handling
 /// resource-freeing and prevending accidental raw copies.
-pub fn write_cpp_wrapper(cpp_header_file: &mut File, ty: &str, has_destructor: bool) {
+pub fn write_cpp_wrapper(cpp_header_file: &mut File, ty: &str, has_destructor: bool, trait_methods: Option<Vec<(String, String)>>) {
        writeln!(cpp_header_file, "class {} {{", ty).unwrap();
        writeln!(cpp_header_file, "private:").unwrap();
        writeln!(cpp_header_file, "\tLDK{} self;", ty).unwrap();
@@ -39,6 +39,14 @@ pub fn write_cpp_wrapper(cpp_header_file: &mut File, ty: &str, has_destructor: b
        writeln!(cpp_header_file, "\tLDK{}* operator ->() {{ return &self; }}", ty).unwrap();
        writeln!(cpp_header_file, "\tconst LDK{}* operator &() const {{ return &self; }}", ty).unwrap();
        writeln!(cpp_header_file, "\tconst LDK{}* operator ->() const {{ return &self; }}", ty).unwrap();
+       if let Some(methods) = trait_methods {
+               for (meth_name, meth_docs) in methods {
+                       cpp_header_file.write_all(meth_docs.as_bytes()).unwrap();
+                       // Note that we have zero logic to print C/C__ code for a given function. Instead, we
+                       // simply use sed to replace the following in genbindings.sh
+                       writeln!(cpp_header_file, "XXX {} {}", ty, meth_name).unwrap();
+               }
+       }
        writeln!(cpp_header_file, "}};").unwrap();
 }
 
@@ -137,12 +145,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();