From ebd1e5ae1d62958a9000992adf4a6cd915bac3c8 Mon Sep 17 00:00:00 2001 From: Hamza Date: Mon, 9 Aug 2021 23:30:22 +0500 Subject: [PATCH 1/6] Add custom type for attribute values --- .../yew-macro/src/html_tree/html_element.rs | 2 +- packages/yew-macro/src/stringify.rs | 8 +- .../tests/html_macro/component-fail.stderr | 8 +- .../tests/html_macro/element-fail.stderr | 40 +++++----- .../tests/html_macro/html-element-pass.rs | 10 +-- packages/yew/src/html/classes.rs | 5 +- packages/yew/src/html/conversion.rs | 5 +- packages/yew/src/virtual_dom/mod.rs | 77 ++++++++++++++++++- 8 files changed, 113 insertions(+), 42 deletions(-) diff --git a/packages/yew-macro/src/html_tree/html_element.rs b/packages/yew-macro/src/html_tree/html_element.rs index 043d383b915..791b5b7408a 100644 --- a/packages/yew-macro/src/html_tree/html_element.rs +++ b/packages/yew-macro/src/html_tree/html_element.rs @@ -168,7 +168,7 @@ impl ToTokens for HtmlElement { expr => Value::Dynamic(quote_spanned! {expr.span()=> if #expr { ::std::option::Option::Some( - ::std::borrow::Cow::<'static, ::std::primitive::str>::Borrowed(#key) + ::yew::virtual_dom::AttrValue::Static(#key) ) } else { ::std::option::Option::None diff --git a/packages/yew-macro/src/stringify.rs b/packages/yew-macro/src/stringify.rs index f6d3fad8f66..82d60389dcf 100644 --- a/packages/yew-macro/src/stringify.rs +++ b/packages/yew-macro/src/stringify.rs @@ -6,17 +6,17 @@ use syn::{Expr, Lit, LitStr}; /// Stringify a value at runtime. fn stringify_at_runtime(src: impl ToTokens) -> TokenStream { quote_spanned! {src.span()=> - ::std::convert::Into::<::std::borrow::Cow::<'static, ::std::primitive::str>>::into(#src) + ::std::convert::Into::<::yew::virtual_dom::AttrValue>::into(#src) } } -/// Create `Cow<'static, str>` construction calls. +/// Create `AttrValue` construction calls. /// /// This is deliberately not implemented for strings to preserve spans. pub trait Stringify { /// Try to turn the value into a string literal. fn try_into_lit(&self) -> Option; - /// Create `Cow<'static, str>` however possible. + /// Create `AttrValue` however possible. fn stringify(&self) -> TokenStream; /// Optimize literals to `&'static str`, otherwise keep the value as is. @@ -71,7 +71,7 @@ impl Stringify for LitStr { fn stringify(&self) -> TokenStream { quote_spanned! {self.span()=> - ::std::borrow::Cow::<'static, ::std::primitive::str>::Borrowed(#self) + ::yew::virtual_dom::AttrValue::Static(#self) } } } diff --git a/packages/yew-macro/tests/html_macro/component-fail.stderr b/packages/yew-macro/tests/html_macro/component-fail.stderr index f52324423a9..ec0fe6427e8 100644 --- a/packages/yew-macro/tests/html_macro/component-fail.stderr +++ b/packages/yew-macro/tests/html_macro/component-fail.stderr @@ -342,8 +342,8 @@ error[E0277]: the trait bound `{integer}: IntoPropValue` is not satisfie | ^ the trait `IntoPropValue` is not implemented for `{integer}` | = help: the following implementations were found: - <&'static str as IntoPropValue>> - <&'static str as IntoPropValue>>> + <&'static str as IntoPropValue> + <&'static str as IntoPropValue>> <&'static str as IntoPropValue>> <&'static str as IntoPropValue> and 11 others @@ -355,8 +355,8 @@ error[E0277]: the trait bound `{integer}: IntoPropValue` is not satisfie | ^ the trait `IntoPropValue` is not implemented for `{integer}` | = help: the following implementations were found: - <&'static str as IntoPropValue>> - <&'static str as IntoPropValue>>> + <&'static str as IntoPropValue> + <&'static str as IntoPropValue>> <&'static str as IntoPropValue>> <&'static str as IntoPropValue> and 11 others diff --git a/packages/yew-macro/tests/html_macro/element-fail.stderr b/packages/yew-macro/tests/html_macro/element-fail.stderr index 4ce990ed9e8..b74e16e446a 100644 --- a/packages/yew-macro/tests/html_macro/element-fail.stderr +++ b/packages/yew-macro/tests/html_macro/element-fail.stderr @@ -246,60 +246,60 @@ error[E0308]: mismatched types 40 | html! {