Skip to content

librustdoc: impl core::fmt::Write for rustdoc::html::render::Buffer #92758

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 3 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ crate struct Buffer {
buffer: String,
}

impl core::fmt::Write for Buffer {
Copy link
Contributor

@djc djc Jan 11, 2022

Choose a reason for hiding this comment

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

I would probably just forward all three methods directly (I'm unsure about performance effects), since it's pretty trivial to do, and also annotate each of them with #[inline].

And we should probably remove the inherent write_str() and write_fmt() methods, replacing their usage with these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I forwarded all the methods and then tried to use the trait methods but i could not make head or tail of the errors that came :(
I then tried to call the traits as <Self as Write>::write_fmt(....) which worked. Have not pushed those changes up though, as I am not sure if they are sane :(

#[inline]
fn write_str(&mut self, s: &str) -> fmt::Result {
self.buffer.write_str(s)
}

#[inline]
fn write_char(&mut self, c: char) -> fmt::Result {
self.buffer.write_char(c)
}

#[inline]
fn write_fmt(self: &mut Self, args: fmt::Arguments<'_>) -> fmt::Result {
self.buffer.write_fmt(args)
}
}

impl Buffer {
crate fn empty_from(v: &Buffer) -> Buffer {
Buffer { for_html: v.for_html, buffer: String::new() }
Expand Down
3 changes: 1 addition & 2 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer,
src_href: src_href.as_deref(),
};

let heading = item_vars.render().unwrap();
buf.write_str(&heading);
item_vars.render_into(buf).unwrap();

match *item.kind {
clean::ModuleItem(ref m) => item_module(buf, cx, item, &m.items),
Expand Down