Skip to content

Commit

Permalink
Fix #2553 - automatically convert closure to callback for component p…
Browse files Browse the repository at this point in the history
…roperties (#2554)

* Fix #2553 - automatically convert closure to callback for component properties.

* Support F -> Option<Callback<_>> too.

* test stderr.
  • Loading branch information
finnbear committed Mar 27, 2022
1 parent e1f89a8 commit ea8a530
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/yew-macro/tests/html_macro/component-fail.stderr
Expand Up @@ -346,7 +346,7 @@ error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfie
<&'static str as IntoPropValue<Classes>>
<&'static str as IntoPropValue<Option<AttrValue>>>
<&'static str as IntoPropValue<Option<String>>>
and 15 others
and 18 others

error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfied
--> tests/html_macro/component-fail.rs:79:34
Expand All @@ -359,7 +359,7 @@ error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfie
<&'static str as IntoPropValue<Classes>>
<&'static str as IntoPropValue<Option<AttrValue>>>
<&'static str as IntoPropValue<Option<String>>>
and 15 others
and 18 others

error[E0308]: mismatched types
--> tests/html_macro/component-fail.rs:80:31
Expand Down
3 changes: 3 additions & 0 deletions packages/yew-macro/tests/html_macro/element-fail.stderr
Expand Up @@ -303,6 +303,7 @@ error[E0277]: the trait bound `Option<NotToString>: IntoPropValue<Option<AttrVal
= help: the following implementations were found:
<Option<&'static str> as IntoPropValue<Option<AttrValue>>>
<Option<&'static str> as IntoPropValue<Option<String>>>
<Option<F> as IntoPropValue<Option<yew::Callback<I, O>>>>
<Option<String> as IntoPropValue<Option<AttrValue>>>
<Option<std::rc::Rc<str>> as IntoPropValue<Option<AttrValue>>>
note: required by `into_prop_value`
Expand All @@ -320,6 +321,7 @@ error[E0277]: the trait bound `Option<{integer}>: IntoPropValue<Option<AttrValue
= help: the following implementations were found:
<Option<&'static str> as IntoPropValue<Option<AttrValue>>>
<Option<&'static str> as IntoPropValue<Option<String>>>
<Option<F> as IntoPropValue<Option<yew::Callback<I, O>>>>
<Option<String> as IntoPropValue<Option<AttrValue>>>
<Option<std::rc::Rc<str>> as IntoPropValue<Option<AttrValue>>>
note: required by `into_prop_value`
Expand Down Expand Up @@ -416,6 +418,7 @@ error[E0277]: the trait bound `Option<yew::NodeRef>: IntoPropValue<yew::NodeRef>
= help: the following implementations were found:
<Option<&'static str> as IntoPropValue<Option<AttrValue>>>
<Option<&'static str> as IntoPropValue<Option<String>>>
<Option<F> as IntoPropValue<Option<yew::Callback<I, O>>>>
<Option<String> as IntoPropValue<Option<AttrValue>>>
<Option<std::rc::Rc<str>> as IntoPropValue<Option<AttrValue>>>
note: required by `into_prop_value`
Expand Down
38 changes: 38 additions & 0 deletions 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;
Expand Down Expand Up @@ -64,6 +65,33 @@ where
}
}

impl<I, O, F> IntoPropValue<Callback<I, O>> for F
where
F: 'static + Fn(I) -> O,
{
fn into_prop_value(self) -> Callback<I, O> {
Callback::from(self)
}
}

impl<I, O, F> IntoPropValue<Option<Callback<I, O>>> for F
where
F: 'static + Fn(I) -> O,
{
fn into_prop_value(self) -> Option<Callback<I, O>> {
Some(Callback::from(self))
}
}

impl<I, O, F> IntoPropValue<Option<Callback<I, O>>> for Option<F>
where
F: 'static + Fn(I) -> O,
{
fn into_prop_value(self) -> Option<Callback<I, O>> {
self.map(|f| Callback::from(f))
}
}

macro_rules! impl_into_prop {
(|$value:ident: $from_ty:ty| -> $to_ty:ty { $conversion:expr }) => {
// implement V -> T
Expand Down Expand Up @@ -111,4 +139,14 @@ mod test {
let _: Option<AttrValue> = "foo".into_prop_value();
let _: Option<AttrValue> = Rc::<str>::from("foo").into_prop_value();
}

#[test]
fn test_callback() {
let _: Callback<String> = (|_: String| ()).into_prop_value();
let _: Option<Callback<String>> = (|_: String| ()).into_prop_value();
let _: Option<Callback<String>> = Some(|_: String| ()).into_prop_value();
let _: Callback<String, String> = (|s: String| s).into_prop_value();
let _: Option<Callback<String, String>> = (|s: String| s).into_prop_value();
let _: Option<Callback<String, String>> = Some(|s: String| s).into_prop_value();
}
}

1 comment on commit ea8a530

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yew master branch benchmarks (Lower is better)

Benchmark suite Current: ea8a530 Previous: e1f89a8 Ratio
yew-struct-keyed 01_run1k 237.946 175.63150000000002 1.35
yew-struct-keyed 02_replace1k 231.9485 186.3975 1.24
yew-struct-keyed 03_update10th1k_x16 402.2355 383.614 1.05
yew-struct-keyed 04_select1k 73.894 81.82900000000001 0.90
yew-struct-keyed 05_swap1k 94.2395 92.8895 1.01
yew-struct-keyed 06_remove-one-1k 30.8645 29.4085 1.05
yew-struct-keyed 07_create10k 2680.2865 2061.8379999999997 1.30
yew-struct-keyed 08_create1k-after1k_x2 498.7965 395.1085 1.26
yew-struct-keyed 09_clear1k_x8 241.5745 205.3965 1.18
yew-struct-keyed 21_ready-memory 1.2447853088378906 0.9634475708007812 1.29
yew-struct-keyed 22_run-memory 1.45648193359375 1.5048255920410156 0.97
yew-struct-keyed 23_update5-memory 1.5057640075683594 1.4602203369140625 1.03
yew-struct-keyed 24_run5-memory 1.5095291137695312 1.5121383666992188 1.00
yew-struct-keyed 25_run-clear-memory 1.1240959167480469 1.1243438720703125 1.00
yew-struct-keyed 31_startup-ci 1732.398 1730.208 1.00
yew-struct-keyed 32_startup-bt 37.907999999999994 29.46599999999999 1.29
yew-struct-keyed 34_startup-totalbytes 330.5556640625 330.5556640625 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.