Skip to content

Commit

Permalink
Merge pull request #5 from cecton/allow-clones
Browse files Browse the repository at this point in the history
  • Loading branch information
cecton committed Oct 30, 2023
2 parents bb2772d + 7319dbc commit 51220ab
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
28 changes: 16 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,31 @@ pub fn autoprops_component(

let mut fields = Vec::new();
let mut arg_types = Vec::new();
let mut clones = Vec::new();
let mut partial_eq_constraints = Vec::new();
for input in function.sig.inputs.iter() {
if let FnArg::Typed(PatType { pat, ty, attrs, .. }) = input {
let Type::Reference(ty) = ty.as_ref() else {
panic!(
"Invalid argument: {} (must be a reference)",
input.to_token_stream()
);
};
let mut end_ty = ty;

if let Type::Reference(ty_ref) = ty.as_ref() {
end_ty = &ty_ref.elem;
} else {
clones.push(quote! {
let #pat = #pat.clone();
});
}

let ty = &ty.elem;
fields.push(quote! {
#(#attrs)*
pub #pat: #ty
pub #pat: #end_ty
});
arg_types.push(ty.clone());
partial_eq_constraints.push(quote! { #end_ty: PartialEq, });
} else {
panic!("Invalid argument");
}
}

let partial_eq_constraints = arg_types.iter().map(|ty| quote! { #ty: PartialEq });

let (impl_generics, ty_generics, _) = generics.split_for_impl();
let bounds = generics.where_clause.clone();

Expand All @@ -139,8 +142,8 @@ pub fn autoprops_component(
} else {
quote! {
where
#(#partial_eq_constraints),*
#bounds,
#(#partial_eq_constraints)*
#bounds
}
};

Expand All @@ -159,6 +162,7 @@ pub fn autoprops_component(
#[::yew::function_component(#component_name)]
#[allow(non_snake_case)]
#visibility fn #fn_name #impl_generics (#destructure: &#struct_name #ty_generics) -> ::yew::Html #where_clause {
#(#clones)*
#function_block
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use yew::prelude::*;
use yew_autoprops::autoprops_component;

#[autoprops_component]
Expand Down
7 changes: 0 additions & 7 deletions tests/cases/require-refs-fail.stderr

This file was deleted.

0 comments on commit 51220ab

Please sign in to comment.