From 9e603cece63bb6f4e3d5f98c08e98f91f3f518bd Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 29 Jan 2020 15:18:01 +0100 Subject: [PATCH] Simplify yew-macro a bit (#902) * yew-macro: Simplify Properties validation * Fix most clippy warnings --- crates/macro/Cargo.toml | 1 - crates/macro/build.rs | 7 ---- crates/macro/src/derive_props/builder.rs | 7 +--- crates/macro/src/html_tree/html_component.rs | 39 ++++++-------------- crates/macro/src/html_tree/html_iterable.rs | 2 +- crates/macro/src/html_tree/html_prop.rs | 2 +- 6 files changed, 15 insertions(+), 43 deletions(-) delete mode 100644 crates/macro/build.rs diff --git a/crates/macro/Cargo.toml b/crates/macro/Cargo.toml index b24411a0b5c..d5b5e81d6b7 100644 --- a/crates/macro/Cargo.toml +++ b/crates/macro/Cargo.toml @@ -32,7 +32,6 @@ trybuild = "1.0" yew = { path = "../..", features = ["std_web"] } [build-dependencies] -autocfg = "1.0.0" [features] doc_test = [] diff --git a/crates/macro/build.rs b/crates/macro/build.rs deleted file mode 100644 index a5de3dc6e7c..00000000000 --- a/crates/macro/build.rs +++ /dev/null @@ -1,7 +0,0 @@ -extern crate autocfg; - -pub fn main() { - if autocfg::new().probe_rustc_version(1, 36) { - println!("cargo:rustc-cfg=has_maybe_uninit"); - } -} diff --git a/crates/macro/src/derive_props/builder.rs b/crates/macro/src/derive_props/builder.rs index d17ae1ca343..dd28914d194 100644 --- a/crates/macro/src/derive_props/builder.rs +++ b/crates/macro/src/derive_props/builder.rs @@ -46,11 +46,8 @@ impl ToTokens for PropsBuilder<'_> { // Each builder step implements the `BuilderStep` trait and `step_generics` is used to // enforce that. let step_generic_param = Ident::new("YEW_PROPS_BUILDER_STEP", Span::call_site()); - let step_generics = with_param_bounds( - &generics, - step_generic_param.clone(), - step_trait.clone().to_owned(), - ); + let step_generics = + with_param_bounds(&generics, step_generic_param.clone(), (*step_trait).clone()); let builder = quote! { #( diff --git a/crates/macro/src/html_tree/html_component.rs b/crates/macro/src/html_tree/html_component.rs index ff4f53b1cce..7893c93aab2 100644 --- a/crates/macro/src/html_tree/html_component.rs +++ b/crates/macro/src/html_tree/html_component.rs @@ -88,38 +88,21 @@ impl ToTokens for HtmlComponent { } = self; let validate_props = if let Props::List(ListProps { props, .. }) = props { - let prop_ref = Ident::new("__yew_prop_ref", Span::call_site()); let check_props = props.iter().map(|HtmlProp { label, .. }| { - quote! { #prop_ref.#label; } + quote! { props.#label; } }); let check_children = if !children.is_empty() { - quote! { #prop_ref.children; } + quote! { props.children; } } else { quote! {} }; - // This is a hack to avoid allocating memory but still have a reference to a props - // struct so that attributes can be checked against it - - #[cfg(has_maybe_uninit)] - let unallocated_prop_ref = quote! { - let #prop_ref: <#ty as ::yew::html::Component>::Properties = unsafe { - ::std::mem::MaybeUninit::uninit().assume_init() - }; - }; - - #[cfg(not(has_maybe_uninit))] - let unallocated_prop_ref = quote! { - let #prop_ref: <#ty as ::yew::html::Component>::Properties = unsafe { - ::std::mem::uninitialized() - }; - }; - quote! { - #unallocated_prop_ref - #check_children - #(#check_props)* + let _ = |props: <#ty as ::yew::html::Component>::Properties| { + #check_children + #(#check_props)* + }; } } else { quote! {} @@ -283,7 +266,7 @@ impl PeekValue for HtmlComponentOpen { let (punct, cursor) = cursor.punct()?; (punct.as_char() == '<').as_option()?; let (typ, _) = HtmlComponent::peek_type(cursor)?; - return Some(typ); + Some(typ) } } @@ -333,7 +316,7 @@ impl PeekValue for HtmlComponentClose { let (punct, _) = cursor.punct()?; (punct.as_char() == '>').as_option()?; - return Some(typ); + Some(typ) } } impl Parse for HtmlComponentClose { @@ -378,7 +361,7 @@ impl Props { impl PeekValue for Props { fn peek(cursor: Cursor) -> Option { let (ident, _) = cursor.ident()?; - let prop_type = if ident.to_string() == "with" { + let prop_type = if ident == "with" { PropType::With } else { PropType::List @@ -411,7 +394,7 @@ impl Parse for ListProps { } let ref_position = props.iter().position(|p| p.label.to_string() == "ref"); - let node_ref = ref_position.and_then(|i| Some(props.remove(i).value)); + let node_ref = ref_position.map(|i| props.remove(i).value); for prop in &props { if prop.label.to_string() == "ref" { return Err(syn::Error::new_spanned(&prop.label, "too many refs set")); @@ -452,7 +435,7 @@ struct WithProps { impl Parse for WithProps { fn parse(input: ParseStream) -> ParseResult { let with = input.parse::()?; - if with.to_string() != "with" { + if with != "with" { return Err(input.error("expected to find `with` token")); } let props = input.parse::()?; diff --git a/crates/macro/src/html_tree/html_iterable.rs b/crates/macro/src/html_tree/html_iterable.rs index ba80943d2b5..712a018f55a 100644 --- a/crates/macro/src/html_tree/html_iterable.rs +++ b/crates/macro/src/html_tree/html_iterable.rs @@ -12,7 +12,7 @@ pub struct HtmlIterable(Expr); impl PeekValue<()> for HtmlIterable { fn peek(cursor: Cursor) -> Option<()> { let (ident, _) = cursor.ident()?; - (ident.to_string() == "for").as_option() + (ident == "for").as_option() } } diff --git a/crates/macro/src/html_tree/html_prop.rs b/crates/macro/src/html_tree/html_prop.rs index 8a47abdd0f7..1744f710854 100644 --- a/crates/macro/src/html_tree/html_prop.rs +++ b/crates/macro/src/html_tree/html_prop.rs @@ -16,7 +16,7 @@ impl PeekValue<()> for HtmlProp { fn peek(cursor: Cursor) -> Option<()> { let (_, cursor) = HtmlPropLabel::peek(cursor)?; let (punct, _) = cursor.punct()?; - return (punct.as_char() == '=').as_option(); + (punct.as_char() == '=').as_option() } }