-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
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. |
@@ -19,5 +19,5 @@ <h2 id="fields" class="fields small-section-header"> | |||
{{ self.document_field(field) | safe }} | |||
{% endfor %} | |||
{% 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found out that we can remove .borrow()
calls by:
- Implementing
ItemTemplate
trait 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::Template
trait for&ItemUnion
- Due to the derive macro doesn't implement
askama::Template
for&T
- Can't apply blanket impls here since
Template
is 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty much stuck at this point, what do you think? @GuillaumeGomez
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)