Skip to content

Commit de7d5a4

Browse files
committed
Handle self: [&*] Self as argument rather than just [*&]self
1 parent 10b9b31 commit de7d5a4

File tree

2 files changed

+68
-17
lines changed

2 files changed

+68
-17
lines changed

c-bindings-gen/src/blocks.rs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -540,22 +540,32 @@ pub fn write_method_params<W: std::io::Write>(w: &mut W, sig: &syn::Signature, t
540540
let mut first_arg = true;
541541
let mut num_unused = 0;
542542
for inp in sig.inputs.iter() {
543+
let mut handle_self = |is_ref: bool, is_mut: bool| {
544+
write!(w, "{}this_arg: {}{}", if !is_ref { "mut " } else { "" },
545+
if is_ref {
546+
match (self_ptr, is_mut) {
547+
(true, true) => "*mut ",
548+
(true, false) => "*const ",
549+
(false, true) => "&mut ",
550+
(false, false) => "&",
551+
}
552+
} else { "" }, this_param).unwrap();
553+
assert!(first_arg);
554+
first_arg = false;
555+
};
543556
match inp {
544557
syn::FnArg::Receiver(recv) => {
545558
if !recv.attrs.is_empty() { unimplemented!(); }
546-
write!(w, "{}this_arg: {}{}", if recv.reference.is_none() { "mut " } else { "" },
547-
if recv.reference.is_some() {
548-
match (self_ptr, recv.mutability.is_some()) {
549-
(true, true) => "*mut ",
550-
(true, false) => "*const ",
551-
(false, true) => "&mut ",
552-
(false, false) => "&",
553-
}
554-
} else { "" }, this_param).unwrap();
555-
assert!(first_arg);
556-
first_arg = false;
559+
handle_self(recv.reference.is_some(), recv.mutability.is_some());
557560
},
558561
syn::FnArg::Typed(arg) => {
562+
if let syn::Pat::Ident(id) = &*arg.pat {
563+
if format!("{}", id.ident) == "self" {
564+
handle_self(id.by_ref.is_some(), id.mutability.is_some());
565+
continue;
566+
}
567+
}
568+
559569
if types.skip_arg(&*arg.ty, generics) { continue; }
560570
if !arg.attrs.is_empty() { unimplemented!(); }
561571
// 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: std::io::Write>(w: &mut W, sig: &syn::Signa
606616
match inp {
607617
syn::FnArg::Receiver(_) => {},
608618
syn::FnArg::Typed(arg) => {
619+
if let syn::Pat::Ident(id) = &*arg.pat {
620+
if format!("{}", id.ident) == "self" {
621+
continue;
622+
}
623+
}
624+
609625
if types.skip_arg(&*arg.ty, generics) { continue; }
610626
if !arg.attrs.is_empty() { unimplemented!(); }
611627
macro_rules! write_new_var {
@@ -666,6 +682,17 @@ pub fn write_method_call_params<W: std::io::Write>(w: &mut W, sig: &syn::Signatu
666682
}
667683
},
668684
syn::FnArg::Typed(arg) => {
685+
if let syn::Pat::Ident(id) = &*arg.pat {
686+
if format!("{}", id.ident) == "self" {
687+
if to_c {
688+
if id.by_ref.is_none() && !matches!(&*arg.ty, syn::Type::Reference(_)) { unimplemented!(); }
689+
write!(w, "self.this_arg").unwrap();
690+
first_arg = false;
691+
}
692+
continue;
693+
}
694+
}
695+
669696
if types.skip_arg(&*arg.ty, generics) {
670697
if !to_c {
671698
if !first_arg {

c-bindings-gen/src/main.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,8 +1185,19 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
11851185
write_method_var_decl_body(w, &$trait_meth.sig, "", &mut trait_resolver, Some(&meth_gen_types), false);
11861186
let mut takes_self = false;
11871187
for inp in $m.sig.inputs.iter() {
1188-
if let syn::FnArg::Receiver(_) = inp {
1189-
takes_self = true;
1188+
match inp {
1189+
syn::FnArg::Receiver(_) => {
1190+
takes_self = true;
1191+
break;
1192+
},
1193+
syn::FnArg::Typed(ty) => {
1194+
if let syn::Pat::Ident(id) = &*ty.pat {
1195+
if format!("{}", id.ident) == "self" {
1196+
takes_self = true;
1197+
break;
1198+
}
1199+
}
1200+
}
11901201
}
11911202
}
11921203

@@ -1456,10 +1467,23 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
14561467
let mut takes_mut_self = false;
14571468
let mut takes_owned_self = false;
14581469
for inp in m.sig.inputs.iter() {
1459-
if let syn::FnArg::Receiver(r) = inp {
1460-
takes_self = true;
1461-
if r.mutability.is_some() { takes_mut_self = true; }
1462-
if r.reference.is_none() { takes_owned_self = true; }
1470+
match inp {
1471+
syn::FnArg::Receiver(r) => {
1472+
takes_self = true;
1473+
if r.mutability.is_some() { takes_mut_self = true; }
1474+
if r.reference.is_none() { takes_owned_self = true; }
1475+
break;
1476+
},
1477+
syn::FnArg::Typed(ty) => {
1478+
if let syn::Pat::Ident(id) = &*ty.pat {
1479+
if format!("{}", id.ident) == "self" {
1480+
takes_self = true;
1481+
if id.mutability.is_some() { takes_mut_self = true; }
1482+
if id.by_ref.is_none() { takes_owned_self = true; }
1483+
break;
1484+
}
1485+
}
1486+
}
14631487
}
14641488
}
14651489
if !takes_mut_self && !takes_self {

0 commit comments

Comments
 (0)