Skip to content

Commit 395eec8

Browse files
ranilececton
authored andcommitted
Remove ToHtml trait (yewstack#3453)
* remove ToHtml trait * re-add display impls * make Vec::clone expilit * fix doc * fix conflicting impls Into<Html> and Display can't be implemented on the same type * update docs * blanket impl won't work here * bring back `Vec<VNode>: IntoPropValue<VNode>` * macro tests * Revert "fix conflicting impls" This reverts commit 52f3c1f. These impls are fine now * make examples compile * .clone() should be before .into() * Rc VList * Make use of ImplicitClone and AttrValue in example (There is more work to do but it's complicated so I will do it in another PR) * Impl ImplicitClone on VChild --------- Co-authored-by: Cecile Tonglet <[email protected]>
1 parent 22999ce commit 395eec8

File tree

16 files changed

+168
-293
lines changed

16 files changed

+168
-293
lines changed

examples/function_todomvc/src/state.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::rc::Rc;
22

33
use serde::{Deserialize, Serialize};
44
use strum_macros::{Display, EnumIter};
5+
use yew::html::IntoPropValue;
56
use yew::prelude::*;
67

78
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
@@ -42,8 +43,8 @@ impl Filter {
4243
}
4344
}
4445

45-
impl ToHtml for Filter {
46-
fn to_html(&self) -> Html {
46+
impl IntoPropValue<Html> for Filter {
47+
fn into_prop_value(self) -> Html {
4748
html! {<>{self.to_string()}</>}
4849
}
4950
}

examples/nested_list/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub struct Props {
77
#[prop_or_default]
88
pub hide: bool,
99
pub on_hover: Callback<Hovered>,
10-
pub name: String,
10+
pub name: AttrValue,
1111
#[prop_or_default]
1212
pub children: Children,
1313
}

examples/nested_list/src/list.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@ impl List {
7373
.filter(|c| !c.props.hide)
7474
.enumerate()
7575
.map(|(i, mut c)| {
76-
let mut props = (*c.props).clone();
77-
props.name = format!("#{} - {}", i + 1, props.name);
78-
c.props = Rc::new(props);
76+
let props = Rc::make_mut(&mut c.props);
77+
props.name = format!("#{} - {}", i + 1, props.name).into();
7978
c
8079
})
8180
.collect::<Html>()

examples/nested_list/src/main.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::fmt;
88
use std::ops::Deref;
99
use std::rc::Rc;
1010

11-
use yew::html::{ImplicitClone, Scope};
11+
use yew::html::{ImplicitClone, IntoPropValue, Scope};
1212
use yew::prelude::*;
1313

1414
pub struct WeakComponentLink<COMP: Component>(Rc<RefCell<Option<Scope<COMP>>>>);
@@ -40,14 +40,16 @@ impl<COMP: Component> PartialEq for WeakComponentLink<COMP> {
4040
}
4141
}
4242

43-
#[derive(Debug)]
43+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4444
pub enum Hovered {
4545
Header,
46-
Item(String),
46+
Item(AttrValue),
4747
List,
4848
None,
4949
}
5050

51+
impl ImplicitClone for Hovered {}
52+
5153
impl fmt::Display for Hovered {
5254
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5355
write!(
@@ -63,8 +65,8 @@ impl fmt::Display for Hovered {
6365
}
6466
}
6567

66-
impl ToHtml for Hovered {
67-
fn to_html(&self) -> yew::Html {
68+
impl IntoPropValue<Html> for &Hovered {
69+
fn into_prop_value(self) -> Html {
6870
html! {<>{self.to_string()}</>}
6971
}
7072
}

examples/timer/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl Component for App {
139139
{ &self.time }
140140
</div>
141141
<div id="messages">
142-
{ for self.messages.iter().map(|message| html! { <p>{ message }</p> }) }
142+
{ for self.messages.iter().map(|message| html! { <p>{ *message }</p> }) }
143143
</div>
144144
</div>
145145
</>

examples/timer_functional/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ fn App() -> Html {
105105
.iter()
106106
.map(|message| {
107107
key += 1;
108-
html! { <p key={ key }>{ message }</p> }
108+
html! { <p key={ key }>{ *message }</p> }
109109
})
110110
.collect();
111111

examples/todomvc/src/state.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use serde_derive::{Deserialize, Serialize};
22
use strum_macros::{Display, EnumIter};
3+
use yew::html::IntoPropValue;
34
use yew::prelude::*;
45

56
#[derive(Debug, Serialize, Deserialize)]
@@ -142,8 +143,8 @@ impl Filter {
142143
}
143144
}
144145

145-
impl ToHtml for Filter {
146-
fn to_html(&self) -> yew::Html {
146+
impl IntoPropValue<Html> for Filter {
147+
fn into_prop_value(self) -> yew::Html {
147148
html! { <>{self.to_string()}</> }
148149
}
149150
}

packages/yew-macro/tests/html_macro/component-fail.stderr

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -463,16 +463,7 @@ error[E0277]: the trait bound `(): IntoPropValue<String>` is not satisfied
463463
| |
464464
| required by a bound introduced by this call
465465
|
466-
= help: the following other types implement trait `IntoPropValue<T>`:
467-
<&'static [(K, V)] as IntoPropValue<implicit_clone::unsync::IMap<K, V>>>
468-
<&'static [T] as IntoPropValue<implicit_clone::unsync::IArray<T>>>
469-
<&'static str as IntoPropValue<Classes>>
470-
<&'static str as IntoPropValue<Option<String>>>
471-
<&'static str as IntoPropValue<Option<implicit_clone::unsync::IString>>>
472-
<&'static str as IntoPropValue<String>>
473-
<&'static str as IntoPropValue<implicit_clone::unsync::IString>>
474-
<&T as IntoPropValue<Option<T>>>
475-
and $N others
466+
= help: the trait `IntoPropValue<VNode>` is implemented for `()`
476467
note: required by a bound in `ChildPropertiesBuilder::string`
477468
--> tests/html_macro/component-fail.rs:4:17
478469
|
@@ -706,7 +697,9 @@ error[E0277]: the trait bound `yew::virtual_dom::VText: IntoPropValue<ChildrenRe
706697
117 | html! { <ChildContainer>{ "Not allowed" }</ChildContainer> };
707698
| ^^^^^^^^^^^^^^ the trait `IntoPropValue<ChildrenRenderer<VChild<Child>>>` is not implemented for `yew::virtual_dom::VText`
708699
|
709-
= help: the trait `IntoPropValue<ChildrenRenderer<VNode>>` is implemented for `yew::virtual_dom::VText`
700+
= help: the following other types implement trait `IntoPropValue<T>`:
701+
<yew::virtual_dom::VText as IntoPropValue<ChildrenRenderer<VNode>>>
702+
<yew::virtual_dom::VText as IntoPropValue<VNode>>
710703
note: required by a bound in `ChildContainerPropertiesBuilder::children`
711704
--> tests/html_macro/component-fail.rs:24:17
712705
|

packages/yew-macro/tests/html_macro/element-fail.stderr

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -434,50 +434,23 @@ error[E0277]: the trait bound `(): IntoPropValue<Option<implicit_clone::unsync::
434434
43 | html! { <input type={()} /> };
435435
| ^^ the trait `IntoPropValue<Option<implicit_clone::unsync::IString>>` is not implemented for `()`
436436
|
437-
= help: the following other types implement trait `IntoPropValue<T>`:
438-
<&'static [(K, V)] as IntoPropValue<implicit_clone::unsync::IMap<K, V>>>
439-
<&'static [T] as IntoPropValue<implicit_clone::unsync::IArray<T>>>
440-
<&'static str as IntoPropValue<Classes>>
441-
<&'static str as IntoPropValue<Option<String>>>
442-
<&'static str as IntoPropValue<Option<implicit_clone::unsync::IString>>>
443-
<&'static str as IntoPropValue<String>>
444-
<&'static str as IntoPropValue<implicit_clone::unsync::IString>>
445-
<&T as IntoPropValue<Option<T>>>
446-
and $N others
437+
= help: the trait `IntoPropValue<VNode>` is implemented for `()`
447438

448439
error[E0277]: the trait bound `(): IntoPropValue<Option<implicit_clone::unsync::IString>>` is not satisfied
449440
--> tests/html_macro/element-fail.rs:44:27
450441
|
451442
44 | html! { <input value={()} /> };
452443
| ^^ the trait `IntoPropValue<Option<implicit_clone::unsync::IString>>` is not implemented for `()`
453444
|
454-
= help: the following other types implement trait `IntoPropValue<T>`:
455-
<&'static [(K, V)] as IntoPropValue<implicit_clone::unsync::IMap<K, V>>>
456-
<&'static [T] as IntoPropValue<implicit_clone::unsync::IArray<T>>>
457-
<&'static str as IntoPropValue<Classes>>
458-
<&'static str as IntoPropValue<Option<String>>>
459-
<&'static str as IntoPropValue<Option<implicit_clone::unsync::IString>>>
460-
<&'static str as IntoPropValue<String>>
461-
<&'static str as IntoPropValue<implicit_clone::unsync::IString>>
462-
<&T as IntoPropValue<Option<T>>>
463-
and $N others
445+
= help: the trait `IntoPropValue<VNode>` is implemented for `()`
464446

465447
error[E0277]: the trait bound `(): IntoPropValue<Option<implicit_clone::unsync::IString>>` is not satisfied
466448
--> tests/html_macro/element-fail.rs:45:22
467449
|
468450
45 | html! { <a href={()} /> };
469451
| ^^ the trait `IntoPropValue<Option<implicit_clone::unsync::IString>>` is not implemented for `()`
470452
|
471-
= help: the following other types implement trait `IntoPropValue<T>`:
472-
<&'static [(K, V)] as IntoPropValue<implicit_clone::unsync::IMap<K, V>>>
473-
<&'static [T] as IntoPropValue<implicit_clone::unsync::IArray<T>>>
474-
<&'static str as IntoPropValue<Classes>>
475-
<&'static str as IntoPropValue<Option<String>>>
476-
<&'static str as IntoPropValue<Option<implicit_clone::unsync::IString>>>
477-
<&'static str as IntoPropValue<String>>
478-
<&'static str as IntoPropValue<implicit_clone::unsync::IString>>
479-
<&T as IntoPropValue<Option<T>>>
480-
and $N others
453+
= help: the trait `IntoPropValue<VNode>` is implemented for `()`
481454

482455
error[E0277]: the trait bound `NotToString: IntoPropValue<Option<implicit_clone::unsync::IString>>` is not satisfied
483456
--> tests/html_macro/element-fail.rs:46:28
@@ -493,7 +466,7 @@ error[E0277]: the trait bound `NotToString: IntoPropValue<Option<implicit_clone:
493466
<&'static str as IntoPropValue<Option<implicit_clone::unsync::IString>>>
494467
<&'static str as IntoPropValue<String>>
495468
<&'static str as IntoPropValue<implicit_clone::unsync::IString>>
496-
<&T as IntoPropValue<Option<T>>>
469+
<&String as IntoPropValue<VNode>>
497470
and $N others
498471

499472
error[E0277]: the trait bound `Option<NotToString>: IntoPropValue<Option<implicit_clone::unsync::IString>>` is not satisfied
@@ -510,6 +483,7 @@ error[E0277]: the trait bound `Option<NotToString>: IntoPropValue<Option<implici
510483
<Option<Rc<str>> as IntoPropValue<Option<implicit_clone::unsync::IString>>>
511484
<Option<String> as IntoPropValue<Option<implicit_clone::unsync::IString>>>
512485
<Option<VChild<T>> as IntoPropValue<Option<ChildrenRenderer<C>>>>
486+
<Option<VNode> as IntoPropValue<VNode>>
513487

514488
error[E0277]: the trait bound `Option<{integer}>: IntoPropValue<Option<implicit_clone::unsync::IString>>` is not satisfied
515489
--> tests/html_macro/element-fail.rs:48:22
@@ -525,6 +499,7 @@ error[E0277]: the trait bound `Option<{integer}>: IntoPropValue<Option<implicit_
525499
<Option<Rc<str>> as IntoPropValue<Option<implicit_clone::unsync::IString>>>
526500
<Option<String> as IntoPropValue<Option<implicit_clone::unsync::IString>>>
527501
<Option<VChild<T>> as IntoPropValue<Option<ChildrenRenderer<C>>>>
502+
<Option<VNode> as IntoPropValue<VNode>>
528503

529504
error[E0277]: expected a `Fn<(MouseEvent,)>` closure, found `{integer}`
530505
--> tests/html_macro/element-fail.rs:51:28
@@ -613,16 +588,7 @@ error[E0277]: the trait bound `(): IntoPropValue<yew::NodeRef>` is not satisfied
613588
56 | html! { <input ref={()} /> };
614589
| ^^ the trait `IntoPropValue<yew::NodeRef>` is not implemented for `()`
615590
|
616-
= help: the following other types implement trait `IntoPropValue<T>`:
617-
<&'static [(K, V)] as IntoPropValue<implicit_clone::unsync::IMap<K, V>>>
618-
<&'static [T] as IntoPropValue<implicit_clone::unsync::IArray<T>>>
619-
<&'static str as IntoPropValue<Classes>>
620-
<&'static str as IntoPropValue<Option<String>>>
621-
<&'static str as IntoPropValue<Option<implicit_clone::unsync::IString>>>
622-
<&'static str as IntoPropValue<String>>
623-
<&'static str as IntoPropValue<implicit_clone::unsync::IString>>
624-
<&T as IntoPropValue<Option<T>>>
625-
and $N others
591+
= help: the trait `IntoPropValue<VNode>` is implemented for `()`
626592
= note: this error originates in the macro `html` (in Nightly builds, run with -Z macro-backtrace for more info)
627593

628594
error[E0277]: the trait bound `Option<yew::NodeRef>: IntoPropValue<yew::NodeRef>` is not satisfied
@@ -639,6 +605,7 @@ error[E0277]: the trait bound `Option<yew::NodeRef>: IntoPropValue<yew::NodeRef>
639605
<Option<Rc<str>> as IntoPropValue<Option<implicit_clone::unsync::IString>>>
640606
<Option<String> as IntoPropValue<Option<implicit_clone::unsync::IString>>>
641607
<Option<VChild<T>> as IntoPropValue<Option<ChildrenRenderer<C>>>>
608+
<Option<VNode> as IntoPropValue<VNode>>
642609
= note: this error originates in the macro `html` (in Nightly builds, run with -Z macro-backtrace for more info)
643610

644611
error[E0277]: expected a `Fn<(MouseEvent,)>` closure, found `yew::Callback<String>`
@@ -682,7 +649,7 @@ error[E0277]: the trait bound `NotToString: IntoPropValue<Option<implicit_clone:
682649
<&'static str as IntoPropValue<Option<implicit_clone::unsync::IString>>>
683650
<&'static str as IntoPropValue<String>>
684651
<&'static str as IntoPropValue<implicit_clone::unsync::IString>>
685-
<&T as IntoPropValue<Option<T>>>
652+
<&String as IntoPropValue<VNode>>
686653
and $N others
687654

688655
error[E0277]: the trait bound `(): IntoPropValue<yew::NodeRef>` is not satisfied
@@ -691,16 +658,7 @@ error[E0277]: the trait bound `(): IntoPropValue<yew::NodeRef>` is not satisfied
691658
62 | html! { <input ref={()} /> };
692659
| ^^ the trait `IntoPropValue<yew::NodeRef>` is not implemented for `()`
693660
|
694-
= help: the following other types implement trait `IntoPropValue<T>`:
695-
<&'static [(K, V)] as IntoPropValue<implicit_clone::unsync::IMap<K, V>>>
696-
<&'static [T] as IntoPropValue<implicit_clone::unsync::IArray<T>>>
697-
<&'static str as IntoPropValue<Classes>>
698-
<&'static str as IntoPropValue<Option<String>>>
699-
<&'static str as IntoPropValue<Option<implicit_clone::unsync::IString>>>
700-
<&'static str as IntoPropValue<String>>
701-
<&'static str as IntoPropValue<implicit_clone::unsync::IString>>
702-
<&T as IntoPropValue<Option<T>>>
703-
and $N others
661+
= help: the trait `IntoPropValue<VNode>` is implemented for `()`
704662
= note: this error originates in the macro `html` (in Nightly builds, run with -Z macro-backtrace for more info)
705663

706664
error[E0277]: the trait bound `implicit_clone::unsync::IString: From<{integer}>` is not satisfied

packages/yew/src/html/component/children.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,11 @@ where
193193
/// ```
194194
/// # let children = Children::new(Vec::new());
195195
/// # use yew::{classes, html, Children};
196+
/// # let _ =
196197
/// children.map(|children| {
197198
/// html! {
198199
/// <div class={classes!("container")}>
199-
/// {children}
200+
/// {children.clone()}
200201
/// </div>
201202
/// }
202203
/// })

0 commit comments

Comments
 (0)