Skip to content

Commit 2534e96

Browse files
committed
[bindings] Avoid guessing whether resolved type is a ref in blocks
In some cases, things which are a Rust Reference (ie slices), we may still want to map them as a non-reference and need to put a "mut " in front of the variable name in a function decl. This worked fine by just checking for the slice case, except that we are about to add support for type aliases, which no longer match the naive case. Instead, we can just have the types module print out the C type and check if it begins with a '&' to figure out if it is a reference.
1 parent 980523f commit 2534e96

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

c-bindings-gen/src/blocks.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,15 @@ pub fn write_method_params<W: std::io::Write>(w: &mut W, sig: &syn::Signature, a
113113
syn::FnArg::Typed(arg) => {
114114
if types.skip_arg(&*arg.ty, generics) { continue; }
115115
if !arg.attrs.is_empty() { unimplemented!(); }
116-
let mut is_ref = if let syn::Type::Reference(_) = *arg.ty { true } else { false };
117-
if let syn::Type::Reference(syn::TypeReference { ref elem, .. }) = *arg.ty {
118-
if let syn::Type::Slice(_) = &**elem {
119-
// Slices are mapped to non-ref Vec types, so we want them to be mut
120-
// letting us drain(..) the underlying Vec.
121-
is_ref = false;
122-
}
123-
}
116+
// First get the c type so that we can check if it ends up being a reference:
117+
let mut c_type = Vec::new();
118+
types.write_c_type(&mut c_type, &*arg.ty, generics, false);
124119
match &*arg.pat {
125120
syn::Pat::Ident(ident) => {
126121
if !ident.attrs.is_empty() || ident.subpat.is_some() {
127122
unimplemented!();
128123
}
129-
write!(w, "{}{}{}: ", if first_arg { "" } else { ", " }, if is_ref || !fn_decl { "" } else { "mut " }, ident.ident).unwrap();
124+
write!(w, "{}{}{}: ", if first_arg { "" } else { ", " }, if !fn_decl || c_type[0] == '&' as u8 || c_type[0] == '*' as u8 { "" } else { "mut " }, ident.ident).unwrap();
130125
first_arg = false;
131126
},
132127
syn::Pat::Wild(wild) => {
@@ -136,7 +131,7 @@ pub fn write_method_params<W: std::io::Write>(w: &mut W, sig: &syn::Signature, a
136131
},
137132
_ => unimplemented!(),
138133
}
139-
types.write_c_type(w, &*arg.ty, generics, false);
134+
w.write(&c_type).unwrap();
140135
}
141136
}
142137
}

0 commit comments

Comments
 (0)