X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=c-bindings-gen%2Fsrc%2Fblocks.rs;h=b6622617731fb14947cfe00e3f3f41582307a21b;hb=3277946b600ce546f849166f7f59b5ac4b539d3e;hp=47785a726a8f1b1527b48e5b2b2ca825792c3ee8;hpb=9d4a50249197353575a3ab2dea753b2b93a8f8c5;p=ldk-c-bindings diff --git a/c-bindings-gen/src/blocks.rs b/c-bindings-gen/src/blocks.rs index 47785a7..b662261 100644 --- a/c-bindings-gen/src/blocks.rs +++ b/c-bindings-gen/src/blocks.rs @@ -295,6 +295,34 @@ pub fn write_tuple_block(w: &mut W, mangled_container: &str, writeln!(w, "pub extern \"C\" fn {}_free(_res: {}) {{ }}", mangled_container, mangled_container).unwrap(); } +/// Writes out a C-callable concrete Option struct and utility methods +pub fn write_option_block(w: &mut W, mangled_container: &str, inner_type: &str, clonable: bool) { + writeln!(w, "#[repr(C)]").unwrap(); + if clonable { + writeln!(w, "#[derive(Clone)]").unwrap(); + } + writeln!(w, "pub enum {} {{", mangled_container).unwrap(); + writeln!(w, "\tSome({}),", inner_type).unwrap(); + writeln!(w, "\tNone").unwrap(); + writeln!(w, "}}").unwrap(); + + writeln!(w, "impl {} {{", mangled_container).unwrap(); + writeln!(w, "\t#[allow(unused)] pub(crate) fn is_some(&self) -> bool {{").unwrap(); + writeln!(w, "\t\tif let Self::Some(_) = self {{ true }} else {{ false }}").unwrap(); + writeln!(w, "\t}}").unwrap(); + writeln!(w, "\t#[allow(unused)] pub(crate) fn take(mut self) -> {} {{", inner_type).unwrap(); + writeln!(w, "\t\tif let Self::Some(v) = self {{ v }} else {{ unreachable!() }}").unwrap(); + writeln!(w, "\t}}").unwrap(); + writeln!(w, "}}").unwrap(); + + writeln!(w, "#[no_mangle]").unwrap(); + writeln!(w, "pub extern \"C\" fn {}_free(_res: {}) {{ }}", mangled_container, mangled_container).unwrap(); + if clonable { + writeln!(w, "#[no_mangle]").unwrap(); + writeln!(w, "pub extern \"C\" fn {}_clone(orig: &{}) -> {} {{ orig.clone() }}", mangled_container, mangled_container, mangled_container).unwrap(); + } +} + /// Prints the docs from a given attribute list unless its tagged no export pub fn writeln_docs(w: &mut W, attrs: &[syn::Attribute], prefix: &str) { for attr in attrs.iter() {