This repository was archived by the owner on Mar 11, 2025. It is now read-only.
Support single-expression closures in templates#611
Open
jannik4 wants to merge 4 commits intoaskama-rs:mainfrom
Open
Support single-expression closures in templates#611jannik4 wants to merge 4 commits intoaskama-rs:mainfrom
jannik4 wants to merge 4 commits intoaskama-rs:mainfrom
Conversation
vallentin
reviewed
Jan 19, 2022
|
|
||
| #[derive(Template)] | ||
| #[template( | ||
| source = r#"{{ user_opt.map(|user| user.flag).unwrap_or(FALSE) }}"#, |
Collaborator
There was a problem hiding this comment.
I understand why FALSE is needed, but it's still possible to do the following, right?
Suggested change
| source = r#"{{ user_opt.map(|user| user.flag).unwrap_or(FALSE) }}"#, | |
| source = r#"{{ user_opt.map(|user| user.flag).copied().unwrap_or(false) }}"#, |
Contributor
Author
There was a problem hiding this comment.
Yes, that should work too. I used FALSE to statically ensure that there really is a reference. But since copied only works on references, that's probably better here.
Member
|
Please have a look at jannik4#1. You don't need to add explicit lifetimes in all these places. |
5633250 to
704f8f1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #284 (and #398?)
Example
Drawbacks
I think the only inconvenience of the current implementation is that
Copytypes can cause problems in combination with the automatic borrowing, see below. This should always be fixable by a clone, in this casedata.flag.clone(). (Edit: Or by usingcopiedon the Option as mentioned by @vallentin)The alternative would be to not automatically borrow the closure expression. But then non
Copytypes would not work anymore.