Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow implicitly cloning arguments instead of referencing #5

Merged
merged 6 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.