-
Notifications
You must be signed in to change notification settings - Fork 13.9k
rustdoc: Add item_template macro
#112202
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 item_template macro
#112202
Conversation
|
So overall it looks quite nice minus the few improvements I suggested. But one thing I'm considering here would be to directly use a macro. Technical wondering: do we really need to use Anyway, what do you think of this @rust-lang/rustdoc ? |
|
Even extracting the struct name from the AST can be nontrivial so I do think we need However in this case I think a regular macro should be fine? |
|
I'm not really sure how to achieve similar results here using regular macros ( One problem using regular macros here is when we need to add extra fields to the struct other than For example #[derive(Template)]
#[template(path = "item_struct.html")]
struct ItemStruct<'a, 'cx> {
cx: std::cell::RefCell<&'a mut Context<'cx>>,
it: &'a clean::Item,
s: &'a clean::Struct, // <-- extra here
should_render_fields: bool, // <-- extra here
} |
|
I suppose something like this would work: macro_rules! item_template {
(struct $name:ident {
cx: std::cell::RefCell<&'a mut Context<'cx>>,
it: &'a clean::Item
$rest:tt*) => {{
// implementation
}}
} |
|
Managed to pull it off (along with method selection), and the usage looks something like this: item_template!(
#[template(path = "item_union.html")]
struct ItemUnion<'a, 'cx> {
cx: RefCell<&'a mut Context<'cx>>,
it: &'a clean::Item,
s: &'a clean::Union,
},
methods = [document, document_type_layout, render_attributes_in_pre, render_assoc_items]
);I think I prefer using regular macros instead of derives to prevent adding dependencies and create a new crate just for this issue. Note: will remove the derive macro once this is finalized |
|
Looks great like this. 👍 |
Lemme clean things up and remove the draft status once it's done :) |
ItemTemplate derive macroItemTemplate macro
ItemTemplate macroitem_template macro
|
Bump @GuillaumeGomez whenever you are free :) |
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.
| ($method:tt $($rest:tt)*) => { | |
| ($method:ident $($rest:tt)*) => { |
And very likely needs to add another case with:
($token:tt $($rest:tt)*) => {
compile_error!(concat!("unexpected token: ", stringify($token));
}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.
Since we only expect idents for methods, seems like we can restrict them further by only allowing idents as input.
In this case, we need to modify the match rules in item_template_methods!()
Wdyt?
Ignore above: sounds better to catch everything with tt first, then match on ident on unknown method case
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 methods = part. It makes the macro easier to understand when using it/reading code. Well done! 👍
|
One other thing: can you put the macros at the top of the file (right after the imports) please? |
26612ee to
23590f6
Compare
|
Please just add an explanation on top of the macro on what it does and how to call it. Then squash your commit and it'll be time for r+. Nice work! |
c9e1809 to
052aef1
Compare
052aef1 to
e240dab
Compare
|
Thanks! @bors r+ rollup |
|
☀️ Test successful - checks-actions |
|
Finished benchmarking commit (7b6093e): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 647.255s -> 647.688s (0.07%) |
|
This PR introduces an ICE regression in geo v0.22.1 if you build with |
Closes #112021
This change removes the use of
self.borrows()in Askama templates, removes code duplication fromitem_and_mut_cx(), and improved readability by eliminating the prefixitem_template_when calling from the template.References:
.borrow()calls in Askama templates forItemTemplateimplementors #112021ItemTemplatePR: rustdoc: AddItemTemplatetrait and related functions to avoid repetitively wrapping existing functions #111946r? @GuillaumeGomez