Skip to content

Migrate item_proc_macro to Askama #112031

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

Merged
merged 1 commit into from
May 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,30 +1420,36 @@ fn item_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
}

fn item_proc_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, m: &clean::ProcMacro) {
wrap_item(w, |w| {
fn item_proc_macro(
w: &mut impl fmt::Write,
cx: &mut Context<'_>,
it: &clean::Item,
m: &clean::ProcMacro,
) {
let mut buffer = Buffer::new();
Copy link
Contributor

Choose a reason for hiding this comment

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

Just passing by on some changes happening around :)

Wondering, does the usage of Buffer here costs us some extra allocations? I believe we can use display_fn() to reduce that (?)

CMIIW @GuillaumeGomez

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This are some of my initial contributions to the compiler : )
I was thinking might not be a straightforward replacement in this case, as you'd need to handle the different MacroKind cases inside the provided function, which could make the code a bit more complex. Also, it would require changing wrap_item function, since we are passing a closure that modifies a buffer.

Copy link
Member

Choose a reason for hiding this comment

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

Well, simplest answer: let's run perf check on this PR. :)

wrap_item(&mut buffer, |buffer| {
let name = it.name.expect("proc-macros always have names");
match m.kind {
MacroKind::Bang => {
write!(w, "{}!() {{ /* proc-macro */ }}", name);
write!(buffer, "{}!() {{ /* proc-macro */ }}", name);
}
MacroKind::Attr => {
write!(w, "#[{}]", name);
write!(buffer, "#[{}]", name);
}
MacroKind::Derive => {
write!(w, "#[derive({})]", name);
write!(buffer, "#[derive({})]", name);
if !m.helpers.is_empty() {
w.push_str("\n{\n");
w.push_str(" // Attributes available to this derive:\n");
buffer.push_str("\n{\n");
buffer.push_str(" // Attributes available to this derive:\n");
for attr in &m.helpers {
writeln!(w, " #[{}]", attr);
writeln!(buffer, " #[{}]", attr);
}
w.push_str("}\n");
buffer.push_str("}\n");
}
}
}
});
write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
write!(w, "{}{}", buffer.into_inner(), document(cx, it, None, HeadingOffset::H2)).unwrap();
}

fn item_primitive(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) {
Expand Down