Skip to content

Commit 10b5596

Browse files
authoredFeb 12, 2025
Rollup merge of #136784 - yotamofek:pr/rustdoc-remove-buffer-take2, r=GuillaumeGomez
Nuke `Buffer` abstraction from `librustdoc`, take 2 💣 In #136656 I found out that the for_html field in the Buffer struct was never read, and pondered if Buffer had any utility at all. `@GuillaumeGomez` said he agrees that it can be just removed. So this PR is me removing it. So, r? `@aDotInTheVoid` , maybe? Supersedes #136748
2 parents f0af030 + d99d8c2 commit 10b5596

File tree

13 files changed

+710
-648
lines changed

13 files changed

+710
-648
lines changed
 

‎src/librustdoc/html/format.rs

Lines changed: 9 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -37,113 +37,8 @@ use crate::html::render::Context;
3737
use crate::joined::Joined as _;
3838
use crate::passes::collect_intra_doc_links::UrlFragment;
3939

40-
pub(crate) trait Print {
41-
fn print(self, buffer: &mut Buffer);
42-
}
43-
44-
impl<F> Print for F
45-
where
46-
F: FnOnce(&mut Buffer),
47-
{
48-
fn print(self, buffer: &mut Buffer) {
49-
(self)(buffer)
50-
}
51-
}
52-
53-
impl Print for String {
54-
fn print(self, buffer: &mut Buffer) {
55-
buffer.write_str(&self);
56-
}
57-
}
58-
59-
impl Print for &'_ str {
60-
fn print(self, buffer: &mut Buffer) {
61-
buffer.write_str(self);
62-
}
63-
}
64-
65-
#[derive(Debug, Clone)]
66-
pub(crate) struct Buffer {
67-
for_html: bool,
68-
buffer: String,
69-
}
70-
71-
impl core::fmt::Write for Buffer {
72-
#[inline]
73-
fn write_str(&mut self, s: &str) -> fmt::Result {
74-
self.buffer.write_str(s)
75-
}
76-
77-
#[inline]
78-
fn write_char(&mut self, c: char) -> fmt::Result {
79-
self.buffer.write_char(c)
80-
}
81-
82-
#[inline]
83-
fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result {
84-
self.buffer.write_fmt(args)
85-
}
86-
}
87-
88-
impl Buffer {
89-
pub(crate) fn empty_from(v: &Buffer) -> Buffer {
90-
Buffer { for_html: v.for_html, buffer: String::new() }
91-
}
92-
93-
pub(crate) fn html() -> Buffer {
94-
Buffer { for_html: true, buffer: String::new() }
95-
}
96-
97-
pub(crate) fn new() -> Buffer {
98-
Buffer { for_html: false, buffer: String::new() }
99-
}
100-
101-
pub(crate) fn is_empty(&self) -> bool {
102-
self.buffer.is_empty()
103-
}
104-
105-
pub(crate) fn into_inner(self) -> String {
106-
self.buffer
107-
}
108-
109-
pub(crate) fn push(&mut self, c: char) {
110-
self.buffer.push(c);
111-
}
112-
113-
pub(crate) fn push_str(&mut self, s: &str) {
114-
self.buffer.push_str(s);
115-
}
116-
117-
pub(crate) fn push_buffer(&mut self, other: Buffer) {
118-
self.buffer.push_str(&other.buffer);
119-
}
120-
121-
// Intended for consumption by write! and writeln! (std::fmt) but without
122-
// the fmt::Result return type imposed by fmt::Write (and avoiding the trait
123-
// import).
124-
pub(crate) fn write_str(&mut self, s: &str) {
125-
self.buffer.push_str(s);
126-
}
127-
128-
// Intended for consumption by write! and writeln! (std::fmt) but without
129-
// the fmt::Result return type imposed by fmt::Write (and avoiding the trait
130-
// import).
131-
pub(crate) fn write_fmt(&mut self, v: fmt::Arguments<'_>) {
132-
self.buffer.write_fmt(v).unwrap();
133-
}
134-
135-
pub(crate) fn to_display<T: Print>(mut self, t: T) -> String {
136-
t.print(&mut self);
137-
self.into_inner()
138-
}
139-
140-
pub(crate) fn reserve(&mut self, additional: usize) {
141-
self.buffer.reserve(additional)
142-
}
143-
144-
pub(crate) fn len(&self) -> usize {
145-
self.buffer.len()
146-
}
40+
pub(crate) fn write_str(s: &mut String, f: fmt::Arguments<'_>) {
41+
s.write_fmt(f).unwrap();
14742
}
14843

14944
pub(crate) fn print_generic_bounds<'a, 'tcx: 'a>(
@@ -772,27 +667,27 @@ pub(crate) fn link_tooltip(did: DefId, fragment: &Option<UrlFragment>, cx: &Cont
772667
else {
773668
return String::new();
774669
};
775-
let mut buf = Buffer::new();
670+
let mut buf = String::new();
776671
let fqp = if *shortty == ItemType::Primitive {
777672
// primitives are documented in a crate, but not actually part of it
778673
&fqp[fqp.len() - 1..]
779674
} else {
780675
fqp
781676
};
782677
if let &Some(UrlFragment::Item(id)) = fragment {
783-
write!(buf, "{} ", cx.tcx().def_descr(id));
678+
write_str(&mut buf, format_args!("{} ", cx.tcx().def_descr(id)));
784679
for component in fqp {
785-
write!(buf, "{component}::");
680+
write_str(&mut buf, format_args!("{component}::"));
786681
}
787-
write!(buf, "{}", cx.tcx().item_name(id));
682+
write_str(&mut buf, format_args!("{}", cx.tcx().item_name(id)));
788683
} else if !fqp.is_empty() {
789684
let mut fqp_it = fqp.iter();
790-
write!(buf, "{shortty} {}", fqp_it.next().unwrap());
685+
write_str(&mut buf, format_args!("{shortty} {}", fqp_it.next().unwrap()));
791686
for component in fqp_it {
792-
write!(buf, "::{component}");
687+
write_str(&mut buf, format_args!("::{component}"));
793688
}
794689
}
795-
buf.into_inner()
690+
buf
796691
}
797692

798693
/// Used to render a [`clean::Path`].

‎src/librustdoc/html/highlight.rs

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_span::edition::Edition;
1414
use rustc_span::symbol::Symbol;
1515
use rustc_span::{BytePos, DUMMY_SP, Span};
1616

17-
use super::format::{self, Buffer};
17+
use super::format::{self, write_str};
1818
use crate::clean::PrimitiveType;
1919
use crate::html::escape::EscapeBodyText;
2020
use crate::html::render::{Context, LinkFromSrc};
@@ -48,7 +48,7 @@ pub(crate) enum Tooltip {
4848
/// Highlights `src` as an inline example, returning the HTML output.
4949
pub(crate) fn render_example_with_highlighting(
5050
src: &str,
51-
out: &mut Buffer,
51+
out: &mut String,
5252
tooltip: Tooltip,
5353
playground_button: Option<&str>,
5454
extra_classes: &[String],
@@ -59,61 +59,69 @@ pub(crate) fn render_example_with_highlighting(
5959
}
6060

6161
fn write_header(
62-
out: &mut Buffer,
62+
out: &mut String,
6363
class: &str,
64-
extra_content: Option<Buffer>,
64+
extra_content: Option<&str>,
6565
tooltip: Tooltip,
6666
extra_classes: &[String],
6767
) {
68-
write!(
68+
write_str(
6969
out,
70-
"<div class=\"example-wrap{}\">",
71-
match tooltip {
72-
Tooltip::Ignore => " ignore",
73-
Tooltip::CompileFail => " compile_fail",
74-
Tooltip::ShouldPanic => " should_panic",
75-
Tooltip::Edition(_) => " edition",
76-
Tooltip::None => "",
77-
},
70+
format_args!(
71+
"<div class=\"example-wrap{}\">",
72+
match tooltip {
73+
Tooltip::Ignore => " ignore",
74+
Tooltip::CompileFail => " compile_fail",
75+
Tooltip::ShouldPanic => " should_panic",
76+
Tooltip::Edition(_) => " edition",
77+
Tooltip::None => "",
78+
}
79+
),
7880
);
7981

8082
if tooltip != Tooltip::None {
8183
let edition_code;
82-
write!(
84+
write_str(
8385
out,
84-
"<a href=\"#\" class=\"tooltip\" title=\"{}\">ⓘ</a>",
85-
match tooltip {
86-
Tooltip::Ignore => "This example is not tested",
87-
Tooltip::CompileFail => "This example deliberately fails to compile",
88-
Tooltip::ShouldPanic => "This example panics",
89-
Tooltip::Edition(edition) => {
90-
edition_code = format!("This example runs with edition {edition}");
91-
&edition_code
86+
format_args!(
87+
"<a href=\"#\" class=\"tooltip\" title=\"{}\">ⓘ</a>",
88+
match tooltip {
89+
Tooltip::Ignore => "This example is not tested",
90+
Tooltip::CompileFail => "This example deliberately fails to compile",
91+
Tooltip::ShouldPanic => "This example panics",
92+
Tooltip::Edition(edition) => {
93+
edition_code = format!("This example runs with edition {edition}");
94+
&edition_code
95+
}
96+
Tooltip::None => unreachable!(),
9297
}
93-
Tooltip::None => unreachable!(),
94-
},
98+
),
9599
);
96100
}
97101

98102
if let Some(extra) = extra_content {
99-
out.push_buffer(extra);
103+
out.push_str(&extra);
100104
}
101105
if class.is_empty() {
102-
write!(
106+
write_str(
103107
out,
104-
"<pre class=\"rust{}{}\">",
105-
if extra_classes.is_empty() { "" } else { " " },
106-
extra_classes.join(" "),
108+
format_args!(
109+
"<pre class=\"rust{}{}\">",
110+
if extra_classes.is_empty() { "" } else { " " },
111+
extra_classes.join(" ")
112+
),
107113
);
108114
} else {
109-
write!(
115+
write_str(
110116
out,
111-
"<pre class=\"rust {class}{}{}\">",
112-
if extra_classes.is_empty() { "" } else { " " },
113-
extra_classes.join(" "),
117+
format_args!(
118+
"<pre class=\"rust {class}{}{}\">",
119+
if extra_classes.is_empty() { "" } else { " " },
120+
extra_classes.join(" ")
121+
),
114122
);
115123
}
116-
write!(out, "<code>");
124+
write_str(out, format_args!("<code>"));
117125
}
118126

119127
/// Check if two `Class` can be merged together. In the following rules, "unclassified" means `None`
@@ -398,8 +406,8 @@ pub(super) fn write_code(
398406
});
399407
}
400408

401-
fn write_footer(out: &mut Buffer, playground_button: Option<&str>) {
402-
writeln!(out, "</code></pre>{}</div>", playground_button.unwrap_or_default());
409+
fn write_footer(out: &mut String, playground_button: Option<&str>) {
410+
write_str(out, format_args_nl!("</code></pre>{}</div>", playground_button.unwrap_or_default()));
403411
}
404412

405413
/// How a span of text is classified. Mostly corresponds to token kinds.

‎src/librustdoc/html/highlight/tests.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use rustc_data_structures::fx::FxIndexMap;
33
use rustc_span::create_default_session_globals_then;
44

55
use super::{DecorationInfo, write_code};
6-
use crate::html::format::Buffer;
76

87
const STYLE: &str = r#"
98
<style>
@@ -22,9 +21,9 @@ fn test_html_highlighting() {
2221
create_default_session_globals_then(|| {
2322
let src = include_str!("fixtures/sample.rs");
2423
let html = {
25-
let mut out = Buffer::new();
24+
let mut out = String::new();
2625
write_code(&mut out, src, None, None, None);
27-
format!("{STYLE}<pre><code>{}</code></pre>\n", out.into_inner())
26+
format!("{STYLE}<pre><code>{out}</code></pre>\n")
2827
};
2928
expect_file!["fixtures/sample.html"].assert_eq(&html);
3029
});
@@ -36,9 +35,9 @@ fn test_dos_backline() {
3635
let src = "pub fn foo() {\r\n\
3736
println!(\"foo\");\r\n\
3837
}\r\n";
39-
let mut html = Buffer::new();
38+
let mut html = String::new();
4039
write_code(&mut html, src, None, None, None);
41-
expect_file!["fixtures/dos_line.html"].assert_eq(&html.into_inner());
40+
expect_file!["fixtures/dos_line.html"].assert_eq(&html);
4241
});
4342
}
4443

@@ -50,19 +49,19 @@ use self::whatever;
5049
let x = super::b::foo;
5150
let y = Self::whatever;";
5251

53-
let mut html = Buffer::new();
52+
let mut html = String::new();
5453
write_code(&mut html, src, None, None, None);
55-
expect_file!["fixtures/highlight.html"].assert_eq(&html.into_inner());
54+
expect_file!["fixtures/highlight.html"].assert_eq(&html);
5655
});
5756
}
5857

5958
#[test]
6059
fn test_union_highlighting() {
6160
create_default_session_globals_then(|| {
6261
let src = include_str!("fixtures/union.rs");
63-
let mut html = Buffer::new();
62+
let mut html = String::new();
6463
write_code(&mut html, src, None, None, None);
65-
expect_file!["fixtures/union.html"].assert_eq(&html.into_inner());
64+
expect_file!["fixtures/union.html"].assert_eq(&html);
6665
});
6766
}
6867

@@ -77,8 +76,8 @@ let a = 4;";
7776
decorations.insert("example", vec![(0, 10), (11, 21)]);
7877
decorations.insert("example2", vec![(22, 32)]);
7978

80-
let mut html = Buffer::new();
79+
let mut html = String::new();
8180
write_code(&mut html, src, None, Some(&DecorationInfo(decorations)), None);
82-
expect_file!["fixtures/decorations.html"].assert_eq(&html.into_inner());
81+
expect_file!["fixtures/decorations.html"].assert_eq(&html);
8382
});
8483
}

‎src/librustdoc/html/layout.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use std::fmt::{self, Display};
12
use std::path::PathBuf;
23

34
use rinja::Template;
45
use rustc_data_structures::fx::FxIndexMap;
56

67
use super::static_files::{STATIC_FILES, StaticFiles};
78
use crate::externalfiles::ExternalHtml;
8-
use crate::html::format::{Buffer, Print};
99
use crate::html::render::{StylePath, ensure_trailing_slash};
1010

1111
#[derive(Clone)]
@@ -71,7 +71,24 @@ struct PageLayout<'a> {
7171

7272
pub(crate) use crate::html::render::sidebar::filters;
7373

74-
pub(crate) fn render<T: Print, S: Print>(
74+
/// Implements [`Display`] for a function that accepts a mutable reference to a [`String`], and (optionally) writes to it.
75+
///
76+
/// The wrapped function will receive an empty string, and can modify it,
77+
/// and the `Display` implementation will write the contents of the string after the function has finished.
78+
pub(crate) struct BufDisplay<F>(pub F);
79+
80+
impl<F> Display for BufDisplay<F>
81+
where
82+
F: Fn(&mut String),
83+
{
84+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85+
let mut buf = String::new();
86+
self.0(&mut buf);
87+
f.write_str(&buf)
88+
}
89+
}
90+
91+
pub(crate) fn render<T: Display, S: Display>(
7592
layout: &Layout,
7693
page: &Page<'_>,
7794
sidebar: S,
@@ -98,8 +115,8 @@ pub(crate) fn render<T: Print, S: Print>(
98115
let mut themes: Vec<String> = style_files.iter().map(|s| s.basename().unwrap()).collect();
99116
themes.sort();
100117

101-
let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
102-
let sidebar = Buffer::html().to_display(sidebar);
118+
let content = t.to_string(); // Note: This must happen before making the sidebar.
119+
let sidebar = sidebar.to_string();
103120
PageLayout {
104121
static_root_path,
105122
page,

‎src/librustdoc/html/markdown.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ use crate::clean::RenderedLink;
5252
use crate::doctest;
5353
use crate::doctest::GlobalTestOptions;
5454
use crate::html::escape::{Escape, EscapeBodyText};
55-
use crate::html::format::Buffer;
5655
use crate::html::highlight;
5756
use crate::html::length_limit::HtmlWithLimit;
5857
use crate::html::render::small_url_encode;
@@ -329,7 +328,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
329328

330329
// insert newline to clearly separate it from the
331330
// previous block so we can shorten the html output
332-
let mut s = Buffer::new();
331+
let mut s = String::new();
333332
s.push('\n');
334333

335334
highlight::render_example_with_highlighting(
@@ -339,7 +338,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
339338
playground_button.as_deref(),
340339
&added_classes,
341340
);
342-
Some(Event::Html(s.into_inner().into()))
341+
Some(Event::Html(s.into()))
343342
}
344343
}
345344

‎src/librustdoc/html/render/context.rs

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::cell::RefCell;
22
use std::collections::BTreeMap;
3-
use std::io;
43
use std::path::{Path, PathBuf};
54
use std::sync::mpsc::{Receiver, channel};
5+
use std::{fmt, io};
66

77
use rinja::Template;
88
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
@@ -26,11 +26,12 @@ use crate::formats::FormatRenderer;
2626
use crate::formats::cache::Cache;
2727
use crate::formats::item_type::ItemType;
2828
use crate::html::escape::Escape;
29-
use crate::html::format::{Buffer, join_with_double_colon};
29+
use crate::html::format::join_with_double_colon;
30+
use crate::html::layout::{self, BufDisplay};
3031
use crate::html::markdown::{self, ErrorCodes, IdMap, plain_text_summary};
3132
use crate::html::render::write_shared::write_shared;
3233
use crate::html::url_parts_builder::UrlPartsBuilder;
33-
use crate::html::{layout, sources, static_files};
34+
use crate::html::{sources, static_files};
3435
use crate::scrape_examples::AllCallLocations;
3536
use crate::{DOC_RUST_LANG_ORG_VERSION, try_err};
3637

@@ -235,7 +236,7 @@ impl<'tcx> Context<'tcx> {
235236
};
236237

237238
if !render_redirect_pages {
238-
let mut page_buffer = Buffer::html();
239+
let mut page_buffer = String::new();
239240
print_item(self, it, &mut page_buffer);
240241
let page = layout::Page {
241242
css_class: tyname_s,
@@ -249,8 +250,10 @@ impl<'tcx> Context<'tcx> {
249250
layout::render(
250251
&self.shared.layout,
251252
&page,
252-
|buf: &mut _| print_sidebar(self, it, buf),
253-
move |buf: &mut Buffer| buf.push_buffer(page_buffer),
253+
BufDisplay(|buf: &mut String| {
254+
print_sidebar(self, it, buf);
255+
}),
256+
page_buffer,
254257
&self.shared.style_files,
255258
)
256259
} else {
@@ -627,7 +630,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
627630
rust_logo: has_doc_flag(self.tcx(), LOCAL_CRATE.as_def_id(), sym::rust_logo),
628631
};
629632
let all = shared.all.replace(AllTypes::new());
630-
let mut sidebar = Buffer::html();
633+
let mut sidebar = String::new();
631634

632635
// all.html is not customizable, so a blank id map is fine
633636
let blocks = sidebar_module_like(all.item_sections(), &mut IdMap::new(), ModuleLike::Crate);
@@ -646,8 +649,10 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
646649
let v = layout::render(
647650
&shared.layout,
648651
&page,
649-
sidebar.into_inner(),
650-
|buf: &mut Buffer| all.print(buf),
652+
sidebar,
653+
BufDisplay(|buf: &mut String| {
654+
all.print(buf);
655+
}),
651656
&shared.style_files,
652657
);
653658
shared.fs.write(final_file, v)?;
@@ -665,7 +670,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
665670
&shared.layout,
666671
&page,
667672
sidebar,
668-
|buf: &mut Buffer| {
673+
fmt::from_fn(|buf| {
669674
write!(
670675
buf,
671676
"<div class=\"main-heading\">\
@@ -684,7 +689,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
684689
<script defer src=\"{static_root_path}{settings_js}\"></script>",
685690
static_root_path = page.get_static_root_path(),
686691
settings_js = static_files::STATIC_FILES.settings_js,
687-
);
692+
)?;
688693
// Pre-load all theme CSS files, so that switching feels seamless.
689694
//
690695
// When loading settings.html as a popover, the equivalent HTML is
@@ -697,10 +702,11 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
697702
as=\"style\">",
698703
root_path = page.static_root_path.unwrap_or(""),
699704
suffix = page.resource_suffix,
700-
);
705+
)?;
701706
}
702707
}
703-
},
708+
Ok(())
709+
}),
704710
&shared.style_files,
705711
);
706712
shared.fs.write(settings_file, v)?;
@@ -716,25 +722,22 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
716722
&shared.layout,
717723
&page,
718724
sidebar,
719-
|buf: &mut Buffer| {
720-
write!(
721-
buf,
722-
"<div class=\"main-heading\">\
723-
<h1>Rustdoc help</h1>\
724-
<span class=\"out-of-band\">\
725-
<a id=\"back\" href=\"javascript:void(0)\" onclick=\"history.back();\">\
726-
Back\
727-
</a>\
728-
</span>\
729-
</div>\
730-
<noscript>\
731-
<section>\
732-
<p>You need to enable JavaScript to use keyboard commands or search.</p>\
733-
<p>For more information, browse the <a href=\"{DOC_RUST_LANG_ORG_VERSION}/rustdoc/\">rustdoc handbook</a>.</p>\
734-
</section>\
735-
</noscript>",
736-
)
737-
},
725+
format_args!(
726+
"<div class=\"main-heading\">\
727+
<h1>Rustdoc help</h1>\
728+
<span class=\"out-of-band\">\
729+
<a id=\"back\" href=\"javascript:void(0)\" onclick=\"history.back();\">\
730+
Back\
731+
</a>\
732+
</span>\
733+
</div>\
734+
<noscript>\
735+
<section>\
736+
<p>You need to enable JavaScript to use keyboard commands or search.</p>\
737+
<p>For more information, browse the <a href=\"{DOC_RUST_LANG_ORG_VERSION}/rustdoc/\">rustdoc handbook</a>.</p>\
738+
</section>\
739+
</noscript>",
740+
),
738741
&shared.style_files,
739742
);
740743
shared.fs.write(help_file, v)?;

‎src/librustdoc/html/render/mod.rs

Lines changed: 207 additions & 159 deletions
Large diffs are not rendered by default.

‎src/librustdoc/html/render/print_item.rs

Lines changed: 347 additions & 257 deletions
Large diffs are not rendered by default.

‎src/librustdoc/html/render/sidebar.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use super::{Context, ItemSection, item_ty_to_section};
1111
use crate::clean;
1212
use crate::formats::Impl;
1313
use crate::formats::item_type::ItemType;
14-
use crate::html::format::Buffer;
1514
use crate::html::markdown::{IdMap, MarkdownWithToc};
1615

1716
#[derive(Clone, Copy)]
@@ -114,7 +113,7 @@ pub(crate) mod filters {
114113
}
115114
}
116115

117-
pub(super) fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) {
116+
pub(super) fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut String) {
118117
let mut ids = IdMap::new();
119118
let mut blocks: Vec<LinkBlock<'_>> = docblock_toc(cx, it, &mut ids).into_iter().collect();
120119
let deref_id_map = cx.deref_id_map.borrow();

‎src/librustdoc/html/render/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::cmp::Ordering;
22

3+
use super::AllTypes;
34
use super::print_item::compare_names;
4-
use super::{AllTypes, Buffer};
55

66
#[test]
77
fn test_compare_names() {
@@ -47,8 +47,8 @@ fn test_all_types_prints_header_once() {
4747
// Regression test for #82477
4848
let all_types = AllTypes::new();
4949

50-
let mut buffer = Buffer::new();
50+
let mut buffer = String::new();
5151
all_types.print(&mut buffer);
5252

53-
assert_eq!(1, buffer.into_inner().matches("List of all items").count());
53+
assert_eq!(1, buffer.matches("List of all items").count());
5454
}

‎src/librustdoc/html/render/write_shared.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ use crate::docfs::PathError;
4444
use crate::error::Error;
4545
use crate::formats::Impl;
4646
use crate::formats::item_type::ItemType;
47-
use crate::html::format::Buffer;
4847
use crate::html::layout;
4948
use crate::html::render::ordered_json::{EscapedJson, OrderedJson};
5049
use crate::html::render::search_index::{SerializedSearchIndex, build_index};
@@ -622,7 +621,6 @@ impl TypeAliasPart {
622621
// to make that functionality work here, it needs to be called with
623622
// each type alias, and if it gives a different result, split the impl
624623
for &(type_alias_fqp, type_alias_item) in type_aliases {
625-
let mut buf = Buffer::html();
626624
cx.id_map.borrow_mut().clear();
627625
cx.deref_id_map.borrow_mut().clear();
628626
let target_did = impl_
@@ -638,23 +636,26 @@ impl TypeAliasPart {
638636
} else {
639637
AssocItemLink::Anchor(None)
640638
};
641-
super::render_impl(
642-
&mut buf,
643-
cx,
644-
impl_,
645-
type_alias_item,
646-
assoc_link,
647-
RenderMode::Normal,
648-
None,
649-
&[],
650-
ImplRenderingParameters {
651-
show_def_docs: true,
652-
show_default_items: true,
653-
show_non_assoc_items: true,
654-
toggle_open_by_default: true,
655-
},
656-
);
657-
let text = buf.into_inner();
639+
let text = {
640+
let mut buf = String::new();
641+
super::render_impl(
642+
&mut buf,
643+
cx,
644+
impl_,
645+
type_alias_item,
646+
assoc_link,
647+
RenderMode::Normal,
648+
None,
649+
&[],
650+
ImplRenderingParameters {
651+
show_def_docs: true,
652+
show_default_items: true,
653+
show_non_assoc_items: true,
654+
toggle_open_by_default: true,
655+
},
656+
);
657+
buf
658+
};
658659
let type_alias_fqp = (*type_alias_fqp).iter().join("::");
659660
if Some(&text) == ret.last().map(|s: &AliasSerializableImpl| &s.text) {
660661
ret.last_mut()

‎src/librustdoc/html/sources.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ use rustc_session::Session;
1111
use rustc_span::{FileName, FileNameDisplayPreference, RealFileName, sym};
1212
use tracing::info;
1313

14+
use super::highlight;
15+
use super::layout::{self, BufDisplay};
16+
use super::render::Context;
1417
use crate::clean;
1518
use crate::clean::utils::has_doc_flag;
1619
use crate::docfs::PathError;
1720
use crate::error::Error;
18-
use crate::html::render::Context;
19-
use crate::html::{highlight, layout};
2021
use crate::visit::DocVisitor;
2122

2223
pub(crate) fn render(cx: &mut Context<'_>, krate: &clean::Crate) -> Result<(), Error> {
@@ -237,21 +238,22 @@ impl SourceCollector<'_, '_> {
237238
resource_suffix: &shared.resource_suffix,
238239
rust_logo: has_doc_flag(self.cx.tcx(), LOCAL_CRATE.as_def_id(), sym::rust_logo),
239240
};
241+
let source_context = SourceContext::Standalone { file_path };
240242
let v = layout::render(
241243
&shared.layout,
242244
&page,
243245
"",
244-
|buf: &mut _| {
246+
BufDisplay(|buf: &mut String| {
245247
print_src(
246248
buf,
247249
contents,
248250
file_span,
249251
self.cx,
250252
&root_path,
251253
&highlight::DecorationInfo::default(),
252-
SourceContext::Standalone { file_path },
253-
)
254-
},
254+
&source_context,
255+
);
256+
}),
255257
&shared.style_files,
256258
);
257259
shared.fs.write(cur, v)?;
@@ -301,7 +303,7 @@ pub(crate) struct ScrapedInfo<'a> {
301303
#[derive(Template)]
302304
#[template(path = "scraped_source.html")]
303305
struct ScrapedSource<'a, Code: std::fmt::Display> {
304-
info: ScrapedInfo<'a>,
306+
info: &'a ScrapedInfo<'a>,
305307
code_html: Code,
306308
max_nb_digits: u32,
307309
}
@@ -328,7 +330,7 @@ pub(crate) fn print_src(
328330
context: &Context<'_>,
329331
root_path: &str,
330332
decoration_info: &highlight::DecorationInfo,
331-
source_context: SourceContext<'_>,
333+
source_context: &SourceContext<'_>,
332334
) {
333335
let mut lines = s.lines().count();
334336
let line_info = if let SourceContext::Embedded(ref info) = source_context {

‎src/librustdoc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#![feature(box_patterns)]
88
#![feature(debug_closure_helpers)]
99
#![feature(file_buffered)]
10+
#![feature(format_args_nl)]
1011
#![feature(if_let_guard)]
1112
#![feature(impl_trait_in_assoc_type)]
1213
#![feature(iter_intersperse)]

0 commit comments

Comments
 (0)
Please sign in to comment.