Skip to content
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

Migrate item_foreign_type to Askama #112033

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
14 changes: 8 additions & 6 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1578,21 +1578,23 @@ fn item_static(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item,
write!(w, "{}", document(cx, it, None, HeadingOffset::H2)).unwrap();
}

fn item_foreign_type(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) {
wrap_item(w, |w| {
w.write_str("extern {\n");
render_attributes_in_code(w, it, cx.tcx());
fn item_foreign_type(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item) {
let mut buffer = Buffer::new();
wrap_item(&mut buffer, |buffer| {
Copy link
Member

Choose a reason for hiding this comment

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

Let's wait for #112030 (comment) to be solved first.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here render attributes expects a buffer so we might have to create one here or change the implementation of render_attributes in code to take both a buffer and an impl write

render_attributes_in_code(w, it, cx.tcx());
     |         ------------------------- ^ expected `&mut Buffer`, found `&mut impl fmt::Write`
     |         |
     |         arguments to this function are incorrect
     |

How we could modify render_attributess_in_code

enum WriteWrapper<'a> {
    Buffer(&'a mut html::format::Buffer),
    Writer(&'a mut dyn fmt::Write),
}

impl fmt::Write for WriteWrapper<'_> {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        match self {
            WriteWrapper::Buffer(buffer) => buffer.write_str(s),
            WriteWrapper::Writer(writer) => writer.write_str(s),
        }
    }
}

fn render_attributes_in_code(w: &mut WriteWrapper<'_>, it: &clean::Item, tcx: TyCtxt<'_>) {
    for a in it.attributes(tcx, false) {
        write!(w, "<div class=\"code-attribute\">{}</div>", a);
    }
}

Copy link
Member

Choose a reason for hiding this comment

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

Or you can update render_attributes_in_code to make it take a impl Write instead of Buffer. ;)

fn render_attributes_in_code(w: &mut Buffer, it: &clean::Item, tcx: TyCtxt<'_>) {
    for a in it.attributes(tcx, false) {
        write!(w, "<div class=\"code-attribute\">{}</div>", a);
    }
}

Copy link
Member

Choose a reason for hiding this comment

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

(Unless I missed something obvious, Buffer implements Write so it should be the only change required?)

Copy link
Contributor

Choose a reason for hiding this comment

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

We can actually remove the use of Buffer here and only rely on write!().

Recently, I made a change (to be merged soon) on render_attributes_in_code() that doesn't have Buffer nor impl Write in its input. Instead, it returns impl Display, which then can be used for write!().

See the change here

buffer.write_str("extern {\n");
render_attributes_in_code(buffer, it, cx.tcx());
write!(
w,
buffer,
" {}type {};\n}}",
visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
it.name.unwrap(),
);
});

write!(w, "{}", document(cx, it, None, HeadingOffset::H2));
write!(w, "{}{}", buffer.into_inner(), document(cx, it, None, HeadingOffset::H2)).unwrap();
sladyn98 marked this conversation as resolved.
Show resolved Hide resolved

write!(w, "{}", render_assoc_items(cx, it, it.item_id.expect_def_id(), AssocItemRender::All))
.unwrap();
}

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