Fix #2553 - automatically convert closure to callback for component properties (#2554)

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

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

* test stderr.
This commit is contained in:
Finn Bear 2022-03-27 15:43:53 -07:00 committed by GitHub
parent e1f89a8352
commit ea8a530454
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 2 deletions

View File

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

View File

@ -303,6 +303,7 @@ error[E0277]: the trait bound `Option<NotToString>: IntoPropValue<Option<AttrVal
= help: the following implementations were found: = help: the following implementations were found:
<Option<&'static str> as IntoPropValue<Option<AttrValue>>> <Option<&'static str> as IntoPropValue<Option<AttrValue>>>
<Option<&'static str> as IntoPropValue<Option<String>>> <Option<&'static str> as IntoPropValue<Option<String>>>
<Option<F> as IntoPropValue<Option<yew::Callback<I, O>>>>
<Option<String> as IntoPropValue<Option<AttrValue>>> <Option<String> as IntoPropValue<Option<AttrValue>>>
<Option<std::rc::Rc<str>> as IntoPropValue<Option<AttrValue>>> <Option<std::rc::Rc<str>> as IntoPropValue<Option<AttrValue>>>
note: required by `into_prop_value` note: required by `into_prop_value`
@ -320,6 +321,7 @@ error[E0277]: the trait bound `Option<{integer}>: IntoPropValue<Option<AttrValue
= help: the following implementations were found: = help: the following implementations were found:
<Option<&'static str> as IntoPropValue<Option<AttrValue>>> <Option<&'static str> as IntoPropValue<Option<AttrValue>>>
<Option<&'static str> as IntoPropValue<Option<String>>> <Option<&'static str> as IntoPropValue<Option<String>>>
<Option<F> as IntoPropValue<Option<yew::Callback<I, O>>>>
<Option<String> as IntoPropValue<Option<AttrValue>>> <Option<String> as IntoPropValue<Option<AttrValue>>>
<Option<std::rc::Rc<str>> as IntoPropValue<Option<AttrValue>>> <Option<std::rc::Rc<str>> as IntoPropValue<Option<AttrValue>>>
note: required by `into_prop_value` note: required by `into_prop_value`
@ -416,6 +418,7 @@ error[E0277]: the trait bound `Option<yew::NodeRef>: IntoPropValue<yew::NodeRef>
= help: the following implementations were found: = help: the following implementations were found:
<Option<&'static str> as IntoPropValue<Option<AttrValue>>> <Option<&'static str> as IntoPropValue<Option<AttrValue>>>
<Option<&'static str> as IntoPropValue<Option<String>>> <Option<&'static str> as IntoPropValue<Option<String>>>
<Option<F> as IntoPropValue<Option<yew::Callback<I, O>>>>
<Option<String> as IntoPropValue<Option<AttrValue>>> <Option<String> as IntoPropValue<Option<AttrValue>>>
<Option<std::rc::Rc<str>> as IntoPropValue<Option<AttrValue>>> <Option<std::rc::Rc<str>> as IntoPropValue<Option<AttrValue>>>
note: required by `into_prop_value` note: required by `into_prop_value`

View File

@ -1,3 +1,4 @@
use super::super::callback::Callback;
use super::{Component, NodeRef, Scope}; use super::{Component, NodeRef, Scope};
use crate::virtual_dom::AttrValue; use crate::virtual_dom::AttrValue;
use std::rc::Rc; use std::rc::Rc;
@ -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 { macro_rules! impl_into_prop {
(|$value:ident: $from_ty:ty| -> $to_ty:ty { $conversion:expr }) => { (|$value:ident: $from_ty:ty| -> $to_ty:ty { $conversion:expr }) => {
// implement V -> T // implement V -> T
@ -111,4 +139,14 @@ mod test {
let _: Option<AttrValue> = "foo".into_prop_value(); let _: Option<AttrValue> = "foo".into_prop_value();
let _: Option<AttrValue> = Rc::<str>::from("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();
}
} }