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

Improved template loop generation #401

Merged
merged 2 commits into from
Dec 16, 2020
Merged

Conversation

vallentin
Copy link
Collaborator

@vallentin vallentin commented Dec 15, 2020

Fixes #107
Fixes #333
Closes #221

No test cases were modified, and this is a non-breaking change.

This PR improves the generation of TemplateLoop, such that it has a higher chance to produce Rust code that will actually compile, based on assumptions that can be drawn from the context of the expression used. See the comments in the code, for what those assumptions are.


Old Explanation (from #396 related to this change):

Based on the context of where it is used, what can it be assumed to be?

  • With loops the assumption can be made that:
    • *Call(..) and Index(..) likely return something that implements IntoIterator so don't borrow
    • If not a *Call(..) but self. is used, then assume it needs to be borrowed
    • Otherwise, assume that IntoIterator is implemented and the value can be moved

@vallentin
Copy link
Collaborator Author

vallentin commented Dec 15, 2020

Forgot to include "(fixes #107, #333)" in the commit message, guess you can add it while merging :)

Copy link
Owner

@djc djc left a comment

Choose a reason for hiding this comment

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

For this change, it would actually be good just to add some simple test cases that exercise the new heuristics, for example, one test case per match arm you've added. Sound good?

I also prefer to rebase PRs, in which case I don't get to rewrite the commit message, so please just add the issue numbers. Should be easy with git commit --amend && git push -f.

@vallentin
Copy link
Collaborator Author

vallentin commented Dec 15, 2020

I've added a few tests.


I wanted to add a test using repeat().take() and/or zip(), but that won't work until I've made a PR (again) for is_copyable and not-borrowing literals. However, before I do that, I'll look into it a bit more, as I think there's a few cases that would be non-breaking changes.

#[derive(Template)]
#[template(
    source = "{% for i in ::std::iter::repeat(\"a\").take(5) %}{{ i }}{% endfor %}",
    ext = "txt"
)]
struct ForPathCallTemplate {}

#[test]
fn test_for_path_call() {
    let t = ForPathCallTemplate {};
    assert_eq!(t.render().unwrap(), "aaaaa");
}

Copy link
Owner

@djc djc left a comment

Choose a reason for hiding this comment

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

This is awesome, thanks!

@djc djc merged commit 6615d12 into djc:main Dec 16, 2020
@zakcutner zakcutner mentioned this pull request Feb 18, 2021
@vallentin vallentin deleted the implicit-borrow-loop branch February 18, 2021 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants