Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve html! "for" iterable ergonomics #875

Merged
merged 2 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions crates/macro/src/html_tree/html_iterable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ impl ToTokens for HtmlIterable {
let expr = &self.0;
let new_tokens = quote_spanned! {expr.span()=> {
let mut __yew_vlist = ::yew::virtual_dom::VList::default();
let __yew_nodes: &mut ::std::iter::Iterator<Item = _> = &mut(#expr);
for __yew_node in __yew_nodes.into_iter() {
for __yew_node in ::std::iter::IntoIterator::into_iter(#expr) {
Copy link
Contributor

@jplatte jplatte Jan 15, 2020

Choose a reason for hiding this comment

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

If you're using it directly in a for loop, the call to into_iter is redundant, as that's already what for does internally (documented here).

Suggested change
for __yew_node in ::std::iter::IntoIterator::into_iter(#expr) {
for __yew_node in #expr {

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah great point, thanks!

__yew_vlist.add_child(__yew_node.into());
}
::yew::virtual_dom::VNode::from(__yew_vlist)
Expand Down
4 changes: 2 additions & 2 deletions tests/macro/html-iterable-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ error[E0277]: `()` is not an iterator
| ^^ `()` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `()`
= note: required for the cast to the object type `dyn std::iter::Iterator<Item = _>`
= note: required by `std::iter::IntoIterator::into_iter`

error[E0277]: `()` is not an iterator
--> $DIR/html-iterable-fail.rs:6:17
Expand All @@ -20,7 +20,7 @@ error[E0277]: `()` is not an iterator
| ^^^^ `()` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `()`
= note: required for the cast to the object type `dyn std::iter::Iterator<Item = _>`
= note: required by `std::iter::IntoIterator::into_iter`

error[E0277]: `()` doesn't implement `std::fmt::Display`
--> $DIR/html-iterable-fail.rs:7:17
Expand Down
4 changes: 4 additions & 0 deletions tests/macro/html-iterable-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ use std::iter;

fn compile_pass() {
html! { for iter::empty::<Html>() };
html! { for Vec::<Html>::new() };
html! { for Vec::<Html>::new().into_iter() };
html! { for (0..3).map(|num| { html! { <span>{num}</span> } }) };
html! { for {iter::empty::<Html>()} };

let empty: Vec<Html> = Vec::new();
html! { for empty.into_iter() };

let empty: Vec<Html> = Vec::new();
html! { for empty };
}

fn main() {}