rustdoc: Add ItemTemplate trait and related functions to avoid repetitively wrapping existing functions#111946
Conversation
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @jsha (or someone else) soon. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
|
FYI: "Return position impl Trait in traits" RFC is now entering its final comment period (comment) Through this, we might be able to enhance the trait further. |
| {% endif %} | ||
| {{ self.render_assoc_items() | safe }} | ||
| {{ self.document_type_layout() | safe }} | ||
| {{ self::item_template_render_assoc_items(self.borrow()) | safe }} |
There was a problem hiding this comment.
I just thought about something: could we instead of calling .borrow() change the API of item_template_render_assoc_items to take a &RefCell<T> where T: ItemTemplate or something equivalent to not have to write .borrow() every time? I'm not sure if it's possible though. If you could confirm it, it'd be awesome!
There was a problem hiding this comment.
self::item_template_render_attributes_in_pre(self) | safe expands to
- Without
.borrow():&::askama::filters::safe(::askama::Html, self::item_template_render_attributes_in_pre(&(self)))? - With
.borrow():&::askama::filters::safe(::askama::Html, self::item_template_render_attributes_in_pre({self.borrow()}))?
I'm not sure why the function didn't catch &(self) as &impl ItemTemplate, but self.borrow() does here (self.borrow() returns &ItemUnion) ...
There was a problem hiding this comment.
I found out that we can remove .borrow() calls by:
- Implementing
ItemTemplatetrait for&ItemUnion- The compiler suggested this after I removed
.borrow() - We should use blanket impls to prevent writing the same impl blocks multiple times
- The compiler suggested this after I removed
- Implementing
askama::Templatetrait for&ItemUnion- Due to the derive macro doesn't implement
askama::Templatefor&T - Can't apply blanket impls here since
Templateis from an external crate (not sure if we should make a feature request for this to be available) - Implication: we need to copy over the associated types and wrap methods from
T - Possible to use macros here
- Due to the derive macro doesn't implement
There was a problem hiding this comment.
I'm pretty much stuck at this point, what do you think? @GuillaumeGomez
There was a problem hiding this comment.
I really like the first suggestion which also seems to be the simplest to be implemented (but maybe I'm wrong there). Thanks for taking a look! So what do you prefer: making the changes here directly or opening an issue and fixing it in a follow-up PR?
There was a problem hiding this comment.
Oops, I laid them out wrongly, they should be implemented together.
Doing only the 1st item will cause errors, and the compiler suggested to do the 2nd item to fix it.
There was a problem hiding this comment.
Oh I see. Well, that still seems doable. Then same question for how you prefer things to roll out. :)
There was a problem hiding this comment.
I really like the first suggestion which also seems to be the simplest to be implemented (but maybe I'm wrong there). Thanks for taking a look! So what do you prefer: making the changes here directly or opening an issue and fixing it in a follow-up PR?
Nevertheless, I'd prefer to do a follow-up PR and open up a new issue. I would like to see opinions from others (in case there exists something simpler).
There was a problem hiding this comment.
Let's go this way then! I r+ this PR. Please open an issue. ;)
|
Looks good to me, thanks! Just one thing I'm wondering about but after that we can merge. |
|
Thanks for the work! @bors r+ rollup |
…iaskrgr Rollup of 6 pull requests Successful merges: - rust-lang#108630 (Fix docs for `alloc::realloc`) - rust-lang#109084 (rustc driver: Remove argument 0 before at-expansion to prevent ICE) - rust-lang#111181 (fix(parse): return unpected when current token is EOF) - rust-lang#111656 (Use an unbounded lifetime in `String::leak`.) - rust-lang#111946 (rustdoc: Add `ItemTemplate` trait and related functions to avoid repetitively wrapping existing functions) - rust-lang#112018 (Clean up usage of `cx.tcx` when `tcx` is already set into a variable) r? `@ghost` `@rustbot` modify labels: rollup
…, r=GuillaumeGomez rustdoc: Add `item_template` macro Closes rust-lang#112021 This change removes the use of `self.borrows()` in Askama templates, removes code duplication from `item_and_mut_cx()`, and improved readability by eliminating the prefix `item_template_` when calling from the template. References: - Discussion issue: rust-lang#112021 - `ItemTemplate` PR: rust-lang#111946 r? `@GuillaumeGomez`
Context: #111430 (comment)
This trait will be used extensively in performing migrations to Askama templates (tracking issue: #108868)