Skip to content

Commit

Permalink
fix review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <marin.versic101@gmail.com>
  • Loading branch information
mversic committed Jun 6, 2022
1 parent 7ca0327 commit 2c64900
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions ffi/derive/src/arg.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Logic related to FFI function argument. Visitor implementation visits the given type
//! and collects the FFI conversion information into the [`Arg`] struct
#![allow(clippy::unimplemented)]

use proc_macro2::Span;
Expand Down Expand Up @@ -99,7 +101,6 @@ impl Arg {
}
}

/// Struct representing a method/function argument
struct TypeVisitor<'ast> {
self_ty: &'ast Path,
name: &'ast Ident,
Expand All @@ -120,6 +121,7 @@ impl<'ast> TypeVisitor<'ast> {
ffi_to_src: None,
}
}

fn visit_item_binding(&mut self, seg: &'ast syn::PathSegment) {
let bindings = generic_arg_bindings(seg);

Expand Down Expand Up @@ -352,13 +354,16 @@ impl<'ast> Visit<'ast> for TypeVisitor<'ast> {
}
};
});
self.ffi_to_src = Some(parse_quote! { #ok_ffi_to_src });
self.ffi_to_src = Some(parse_quote! {
#ok_ffi_to_src
let #arg_name = Ok(#arg_name);
});
}
_ => {
if self.is_input {
self.ffi_type = Some(parse_quote! { *const #node });
self.src_to_ffi = Some(parse_quote! {
let #arg_name: *const _ = #arg_name;
let #arg_name: *const _ = &#arg_name;
});
self.ffi_to_src = Some(parse_quote! {
let #arg_name = Clone::clone(&*#arg_name);
Expand Down Expand Up @@ -459,16 +464,18 @@ impl<'ast> Visit<'ast> for TypeVisitor<'ast> {

pub fn generic_arg_types(seg: &syn::PathSegment) -> Vec<&Type> {
if let AngleBracketed(arguments) = &seg.arguments {
let mut args = vec![];

for arg in &arguments.args {
if let syn::GenericArgument::Type(ty) = &arg {
args.push(ty);
}
}

return args;
};
return arguments
.args
.iter()
.filter_map(|arg| {
if let syn::GenericArgument::Type(ty) = &arg {
Some(ty)
} else {
None
}
})
.collect();
}

abort!(seg, "Type not found in the given path segment")
}
Expand Down

0 comments on commit 2c64900

Please sign in to comment.