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

Conversation

jstarry
Copy link
Member

@jstarry jstarry commented Jan 15, 2020

Fixes: #871

Problem

Poor ergonomics when using html! { for .. } . When passing a local variable into html! as part of { for my_var }, it is an error for that local variable not to be mut.

Solution

Use std::iter::IntoIterator::into_iter(#expr) as suggested by @jplatte

@@ -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!

@jplatte
Copy link
Contributor

jplatte commented Jan 15, 2020

Works as expected!

@jstarry jstarry merged commit 3d7bad9 into yewstack:master Jan 15, 2020
@jstarry jstarry deleted the improve-for-iter branch January 15, 2020 09:58
llebout pushed a commit to llebout/yew that referenced this pull request Jan 20, 2020
* Improve html! iteratable ergonomics

* feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Allow non-mut local variables in html!'s for expression
2 participants