From: Matt Corallo Date: Sun, 18 Apr 2021 18:10:39 +0000 (+0000) Subject: Support taking an owned Self in function exports X-Git-Tag: v0.0.98~10^2~8 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=4155b5fbd153403860a800b89b020284a632c59f;p=ldk-c-bindings Support taking an owned Self in function exports --- diff --git a/c-bindings-gen/src/blocks.rs b/c-bindings-gen/src/blocks.rs index 2c51572..2832349 100644 --- a/c-bindings-gen/src/blocks.rs +++ b/c-bindings-gen/src/blocks.rs @@ -442,14 +442,16 @@ pub fn write_method_params(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 +558,9 @@ pub fn write_method_call_params(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; } diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 149ae76..75b537c 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -987,10 +987,12 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ write_method_var_decl_body(w, &m.sig, "", types, Some(&meth_gen_types), false); let mut takes_self = false; let mut takes_mut_self = false; + let mut takes_owned_self = false; for inp in m.sig.inputs.iter() { if let syn::FnArg::Receiver(r) = inp { takes_self = true; if r.mutability.is_some() { takes_mut_self = true; } + if r.reference.is_none() { takes_owned_self = true; } } } if !takes_mut_self && !takes_self { @@ -999,7 +1001,9 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ match &declared_type { DeclType::MirroredEnum => write!(w, "this_arg.to_native().{}(", m.sig.ident).unwrap(), DeclType::StructImported => { - if takes_mut_self { + if takes_owned_self { + write!(w, "(*unsafe {{ Box::from_raw(this_arg.take_inner()) }}).{}(", m.sig.ident).unwrap(); + } else 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();