From: Matt Corallo Date: Thu, 29 Feb 2024 14:55:39 +0000 (+0000) Subject: Handle `self: [&*] Self` as argument rather than just `[*&]self` X-Git-Tag: v0.0.123.0^2~11 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=de7d5a4df67ab3b0ebe3912813765621b5c0f177;p=ldk-c-bindings Handle `self: [&*] Self` as argument rather than just `[*&]self` --- diff --git a/c-bindings-gen/src/blocks.rs b/c-bindings-gen/src/blocks.rs index 664ca44..7ee216a 100644 --- a/c-bindings-gen/src/blocks.rs +++ b/c-bindings-gen/src/blocks.rs @@ -540,22 +540,32 @@ pub fn write_method_params(w: &mut W, sig: &syn::Signature, t let mut first_arg = true; let mut num_unused = 0; for inp in sig.inputs.iter() { + let mut handle_self = |is_ref: bool, is_mut: bool| { + write!(w, "{}this_arg: {}{}", if !is_ref { "mut " } else { "" }, + if is_ref { + match (self_ptr, is_mut) { + (true, true) => "*mut ", + (true, false) => "*const ", + (false, true) => "&mut ", + (false, false) => "&", + } + } else { "" }, this_param).unwrap(); + assert!(first_arg); + first_arg = false; + }; match inp { syn::FnArg::Receiver(recv) => { 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; + handle_self(recv.reference.is_some(), recv.mutability.is_some()); }, syn::FnArg::Typed(arg) => { + if let syn::Pat::Ident(id) = &*arg.pat { + if format!("{}", id.ident) == "self" { + handle_self(id.by_ref.is_some(), id.mutability.is_some()); + continue; + } + } + if types.skip_arg(&*arg.ty, generics) { continue; } if !arg.attrs.is_empty() { unimplemented!(); } // First get the c type so that we can check if it ends up being a reference: @@ -606,6 +616,12 @@ pub fn write_method_var_decl_body(w: &mut W, sig: &syn::Signa match inp { syn::FnArg::Receiver(_) => {}, syn::FnArg::Typed(arg) => { + if let syn::Pat::Ident(id) = &*arg.pat { + if format!("{}", id.ident) == "self" { + continue; + } + } + if types.skip_arg(&*arg.ty, generics) { continue; } if !arg.attrs.is_empty() { unimplemented!(); } macro_rules! write_new_var { @@ -666,6 +682,17 @@ pub fn write_method_call_params(w: &mut W, sig: &syn::Signatu } }, syn::FnArg::Typed(arg) => { + if let syn::Pat::Ident(id) = &*arg.pat { + if format!("{}", id.ident) == "self" { + if to_c { + if id.by_ref.is_none() && !matches!(&*arg.ty, syn::Type::Reference(_)) { unimplemented!(); } + write!(w, "self.this_arg").unwrap(); + first_arg = false; + } + continue; + } + } + if types.skip_arg(&*arg.ty, generics) { if !to_c { if !first_arg { diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 4572f7d..358e30b 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -1185,8 +1185,19 @@ fn writeln_impl(w: &mut W, w_uses: &mut HashSet { + takes_self = true; + break; + }, + syn::FnArg::Typed(ty) => { + if let syn::Pat::Ident(id) = &*ty.pat { + if format!("{}", id.ident) == "self" { + takes_self = true; + break; + } + } + } } } @@ -1456,10 +1467,23 @@ fn writeln_impl(w: &mut W, w_uses: &mut HashSet { + takes_self = true; + if r.mutability.is_some() { takes_mut_self = true; } + if r.reference.is_none() { takes_owned_self = true; } + break; + }, + syn::FnArg::Typed(ty) => { + if let syn::Pat::Ident(id) = &*ty.pat { + if format!("{}", id.ident) == "self" { + takes_self = true; + if id.mutability.is_some() { takes_mut_self = true; } + if id.by_ref.is_none() { takes_owned_self = true; } + break; + } + } + } } } if !takes_mut_self && !takes_self {