]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Support passing lifetime'd enums to C
authorMatt Corallo <git@bluematt.me>
Fri, 29 Dec 2023 20:50:22 +0000 (20:50 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 29 Dec 2023 20:50:22 +0000 (20:50 +0000)
While this is generally unsafe, we have to do something for
`CandidateRouteHop`, which is passed to the scorer. Because its
incredibly performance-sensitive, forcing the bindings users to
take a performance hit just to make the scoring interface marginally
safer seems like a bad trade-off.

c-bindings-gen/src/blocks.rs
c-bindings-gen/src/main.rs

index e419fe095abb8dd6bd5d6dc3b403090b5b664b82..664ca44b42414e142d4c5be4893646ebacdacddc 100644 (file)
@@ -748,6 +748,14 @@ 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, generics_impld: &syn::PathArguments, types: &TypeResolver, concrete_lifetimes: bool) {
+       maybe_write_generics_intern(w, generics, generics_impld, types, concrete_lifetimes, false);
+}
+
+pub fn maybe_write_non_lifetime_generics<W: std::io::Write>(w: &mut W, generics: &syn::Generics, generics_impld: &syn::PathArguments, types: &TypeResolver) {
+       maybe_write_generics_intern(w, generics, generics_impld, types, false, true);
+}
+
+fn maybe_write_generics_intern<W: std::io::Write>(w: &mut W, generics: &syn::Generics, generics_impld: &syn::PathArguments, types: &TypeResolver, concrete_lifetimes: bool, dummy_lifetimes: bool) {
        let mut gen_types = GenericTypes::new(None);
        assert!(gen_types.learn_generics(generics, types));
        if generics.params.is_empty() { return; }
@@ -789,7 +797,9 @@ pub fn maybe_write_generics<W: std::io::Write>(w: &mut W, generics: &syn::Generi
                                }
                        },
                        syn::GenericParam::Lifetime(lt) => {
-                               if concrete_lifetimes {
+                               if dummy_lifetimes {
+                                       write!(w, "'_").unwrap();
+                               } else if concrete_lifetimes {
                                        write!(w, "'static").unwrap();
                                } else {
                                        write!(w, "{}'{}", if idx != 0 { ", " } else { "" }, lt.lifetime.ident).unwrap();
index 7d6a452b04a08fe34d8cf53ee1bf001e1f968865..b72adca348e7056a5ee46500dc2ab3954f4db75b 100644 (file)
@@ -1809,7 +1809,11 @@ fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, type
 
        macro_rules! write_conv {
                ($fn_sig: expr, $to_c: expr, $ref: expr) => {
-                       writeln!(w, "\t#[allow(unused)]\n\tpub(crate) fn {} {{\n\t\tmatch {} {{", $fn_sig, if $to_c { "native" } else { "self" }).unwrap();
+                       writeln!(w, "\t#[allow(unused)]\n\tpub(crate) fn {} {{", $fn_sig).unwrap();
+                       if $to_c && $ref {
+                               writeln!(w, "\t\tlet native = unsafe {{ &*(native as *const _ as *const c_void as *const native{}) }};", e.ident).unwrap();
+                       }
+                       writeln!(w, "\t\tmatch {} {{", if $to_c { "native" } else { "self" }).unwrap();
                        for var in e.variants.iter() {
                                write!(w, "\t\t\t{}{}::{} ", if $to_c { "native" } else { "" }, e.ident, var.ident).unwrap();
                                let mut empty_tuple_variant = false;
@@ -1933,7 +1937,10 @@ fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, type
        }
        write_conv!(format!("into_native(self) -> native{}", e.ident), false, false);
        if is_clonable {
-               write_conv!(format!("from_native(native: &native{}) -> Self", e.ident), true, true);
+               let mut args = Vec::new();
+               maybe_write_non_lifetime_generics(&mut args, &e.generics, &syn::PathArguments::None, &types);
+               let fn_line = format!("from_native(native: &{}Import{}) -> Self", e.ident, String::from_utf8(args).unwrap());
+               write_conv!(fn_line, true, true);
        }
        write_conv!(format!("native_into(native: native{}) -> Self", e.ident), true, false);
        writeln!(w, "}}").unwrap();