From 4167b2e958cfc94132d8df5c899843ec24935ea7 Mon Sep 17 00:00:00 2001 From: Finn Bear Date: Sat, 26 Mar 2022 22:48:20 -0700 Subject: [PATCH 1/4] Fix 2553 - automatically convert closure to callback for component properties. --- packages/yew/src/html/conversion.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/packages/yew/src/html/conversion.rs b/packages/yew/src/html/conversion.rs index aa0b6c7d077..c25b3d1a8ff 100644 --- a/packages/yew/src/html/conversion.rs +++ b/packages/yew/src/html/conversion.rs @@ -1,3 +1,4 @@ +use super::super::callback::Callback; use super::{Component, NodeRef, Scope}; use crate::virtual_dom::AttrValue; use std::rc::Rc; @@ -64,6 +65,24 @@ where } } +impl IntoPropValue> for F + where + F: 'static + Fn(I) -> O, +{ + fn into_prop_value(self) -> Callback { + Callback::from(self) + } +} + +impl IntoPropValue>> for Option + where + F: 'static + Fn(I) -> O, +{ + fn into_prop_value(self) -> Option> { + self.map(|f| Callback::from(f)) + } +} + macro_rules! impl_into_prop { (|$value:ident: $from_ty:ty| -> $to_ty:ty { $conversion:expr }) => { // implement V -> T @@ -111,4 +130,12 @@ mod test { let _: Option = "foo".into_prop_value(); let _: Option = Rc::::from("foo").into_prop_value(); } + + #[test] + fn test_callback() { + let _: Callback = (|_: String| ()).into_prop_value(); + let _: Option> = Some(|_: String| ()).into_prop_value(); + let _: Callback = (|s: String| s).into_prop_value(); + let _: Option> = Some(|s: String| s).into_prop_value(); + } } From d61e368fbbf433e4fd24fd40781840efaff41282 Mon Sep 17 00:00:00 2001 From: Finn Bear Date: Sat, 26 Mar 2022 22:50:42 -0700 Subject: [PATCH 2/4] Support F -> Option> too. --- packages/yew/src/html/conversion.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/yew/src/html/conversion.rs b/packages/yew/src/html/conversion.rs index c25b3d1a8ff..4d74df659f8 100644 --- a/packages/yew/src/html/conversion.rs +++ b/packages/yew/src/html/conversion.rs @@ -74,6 +74,15 @@ impl IntoPropValue> for F } } +impl IntoPropValue>> for F + where + F: 'static + Fn(I) -> O, +{ + fn into_prop_value(self) -> Option> { + Some(Callback::from(self)) + } +} + impl IntoPropValue>> for Option where F: 'static + Fn(I) -> O, @@ -134,8 +143,10 @@ mod test { #[test] fn test_callback() { let _: Callback = (|_: String| ()).into_prop_value(); + let _: Option> = (|_: String| ()).into_prop_value(); let _: Option> = Some(|_: String| ()).into_prop_value(); let _: Callback = (|s: String| s).into_prop_value(); + let _: Option> = (|s: String| s).into_prop_value(); let _: Option> = Some(|s: String| s).into_prop_value(); } } From 7cceced422b81ae86f4dcf96b85f57c591f7e0d4 Mon Sep 17 00:00:00 2001 From: Finn Bear Date: Sat, 26 Mar 2022 22:53:34 -0700 Subject: [PATCH 3/4] fmt. --- packages/yew/src/html/conversion.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/yew/src/html/conversion.rs b/packages/yew/src/html/conversion.rs index 4d74df659f8..62408ab64d5 100644 --- a/packages/yew/src/html/conversion.rs +++ b/packages/yew/src/html/conversion.rs @@ -66,8 +66,8 @@ where } impl IntoPropValue> for F - where - F: 'static + Fn(I) -> O, +where + F: 'static + Fn(I) -> O, { fn into_prop_value(self) -> Callback { Callback::from(self) @@ -75,8 +75,8 @@ impl IntoPropValue> for F } impl IntoPropValue>> for F - where - F: 'static + Fn(I) -> O, +where + F: 'static + Fn(I) -> O, { fn into_prop_value(self) -> Option> { Some(Callback::from(self)) @@ -84,8 +84,8 @@ impl IntoPropValue>> for F } impl IntoPropValue>> for Option - where - F: 'static + Fn(I) -> O, +where + F: 'static + Fn(I) -> O, { fn into_prop_value(self) -> Option> { self.map(|f| Callback::from(f)) From cc84ab792765deba735cfd3fd1d1484497ca6791 Mon Sep 17 00:00:00 2001 From: Finn Bear Date: Sun, 27 Mar 2022 11:16:28 -0700 Subject: [PATCH 4/4] test stderr. --- packages/yew-macro/tests/html_macro/component-fail.stderr | 4 ++-- packages/yew-macro/tests/html_macro/element-fail.stderr | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/yew-macro/tests/html_macro/component-fail.stderr b/packages/yew-macro/tests/html_macro/component-fail.stderr index 3a0eb2af361..5374e76ba40 100644 --- a/packages/yew-macro/tests/html_macro/component-fail.stderr +++ b/packages/yew-macro/tests/html_macro/component-fail.stderr @@ -346,7 +346,7 @@ error[E0277]: the trait bound `{integer}: IntoPropValue` is not satisfie <&'static str as IntoPropValue> <&'static str as IntoPropValue>> <&'static str as IntoPropValue>> - and 15 others + and 18 others error[E0277]: the trait bound `{integer}: IntoPropValue` is not satisfied --> tests/html_macro/component-fail.rs:79:34 @@ -359,7 +359,7 @@ error[E0277]: the trait bound `{integer}: IntoPropValue` is not satisfie <&'static str as IntoPropValue> <&'static str as IntoPropValue>> <&'static str as IntoPropValue>> - and 15 others + and 18 others error[E0308]: mismatched types --> tests/html_macro/component-fail.rs:80:31 diff --git a/packages/yew-macro/tests/html_macro/element-fail.stderr b/packages/yew-macro/tests/html_macro/element-fail.stderr index 5c2652e2680..61b85a3fc60 100644 --- a/packages/yew-macro/tests/html_macro/element-fail.stderr +++ b/packages/yew-macro/tests/html_macro/element-fail.stderr @@ -303,6 +303,7 @@ error[E0277]: the trait bound `Option: IntoPropValue as IntoPropValue>> as IntoPropValue>> + as IntoPropValue>>> as IntoPropValue>> > as IntoPropValue>> note: required by `into_prop_value` @@ -320,6 +321,7 @@ error[E0277]: the trait bound `Option<{integer}>: IntoPropValue as IntoPropValue>> as IntoPropValue>> + as IntoPropValue>>> as IntoPropValue>> > as IntoPropValue>> note: required by `into_prop_value` @@ -416,6 +418,7 @@ error[E0277]: the trait bound `Option: IntoPropValue = help: the following implementations were found: as IntoPropValue>> as IntoPropValue>> + as IntoPropValue>>> as IntoPropValue>> > as IntoPropValue>> note: required by `into_prop_value`