Skip to content

Commit da922cf

Browse files
authored
Unrolled build for rust-lang#141288
Rollup merge of rust-lang#141288 - yotamofek:pr/rustdoc/nuke-bufdisplay, r=GuillaumeGomez Get rid of unnecessary `BufDisplay` abstraction r? `@GuillaumeGomez` , since you reviewed the introduction of `BufDisplay` in rust-lang#136784 . Not sure when it became unnecessary, but it did :) (feel free to reassign if you wish)
2 parents bc82152 + 8682dfc commit da922cf

File tree

5 files changed

+39
-55
lines changed

5 files changed

+39
-55
lines changed

src/librustdoc/html/layout.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt::{self, Display};
1+
use std::fmt::Display;
22
use std::path::PathBuf;
33

44
use askama::Template;
@@ -71,23 +71,6 @@ struct PageLayout<'a> {
7171

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

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-
9174
pub(crate) fn render<T: Display, S: Display>(
9275
layout: &Layout,
9376
page: &Page<'_>,

src/librustdoc/html/render/context.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ use crate::formats::cache::Cache;
2828
use crate::formats::item_type::ItemType;
2929
use crate::html::escape::Escape;
3030
use crate::html::format::join_with_double_colon;
31-
use crate::html::layout::{self, BufDisplay};
3231
use crate::html::markdown::{self, ErrorCodes, IdMap, plain_text_summary};
3332
use crate::html::render::write_shared::write_shared;
3433
use crate::html::url_parts_builder::UrlPartsBuilder;
35-
use crate::html::{sources, static_files};
34+
use crate::html::{layout, sources, static_files};
3635
use crate::scrape_examples::AllCallLocations;
3736
use crate::{DOC_RUST_LANG_ORG_VERSION, try_err};
3837

@@ -250,9 +249,7 @@ impl<'tcx> Context<'tcx> {
250249
layout::render(
251250
&self.shared.layout,
252251
&page,
253-
BufDisplay(|buf: &mut String| {
254-
print_sidebar(self, it, buf);
255-
}),
252+
fmt::from_fn(|f| print_sidebar(self, it, f)),
256253
content,
257254
&self.shared.style_files,
258255
)

src/librustdoc/html/render/mod.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ fn document(
538538
}
539539

540540
fmt::from_fn(move |f| {
541-
document_item_info(cx, item, parent).render_into(f).unwrap();
541+
document_item_info(cx, item, parent).render_into(f)?;
542542
if parent.is_none() {
543543
write!(f, "{}", document_full_collapsible(item, cx, heading_offset))
544544
} else {
@@ -582,7 +582,7 @@ fn document_short(
582582
show_def_docs: bool,
583583
) -> impl fmt::Display {
584584
fmt::from_fn(move |f| {
585-
document_item_info(cx, item, Some(parent)).render_into(f).unwrap();
585+
document_item_info(cx, item, Some(parent)).render_into(f)?;
586586
if !show_def_docs {
587587
return Ok(());
588588
}
@@ -661,7 +661,7 @@ fn document_full_inner(
661661
};
662662

663663
if let clean::ItemKind::FunctionItem(..) | clean::ItemKind::MethodItem(..) = kind {
664-
render_call_locations(f, cx, item);
664+
render_call_locations(f, cx, item)?;
665665
}
666666
Ok(())
667667
})
@@ -2584,11 +2584,15 @@ const MAX_FULL_EXAMPLES: usize = 5;
25842584
const NUM_VISIBLE_LINES: usize = 10;
25852585

25862586
/// Generates the HTML for example call locations generated via the --scrape-examples flag.
2587-
fn render_call_locations<W: fmt::Write>(mut w: W, cx: &Context<'_>, item: &clean::Item) {
2587+
fn render_call_locations<W: fmt::Write>(
2588+
mut w: W,
2589+
cx: &Context<'_>,
2590+
item: &clean::Item,
2591+
) -> fmt::Result {
25882592
let tcx = cx.tcx();
25892593
let def_id = item.item_id.expect_def_id();
25902594
let key = tcx.def_path_hash(def_id);
2591-
let Some(call_locations) = cx.shared.call_locations.get(&key) else { return };
2595+
let Some(call_locations) = cx.shared.call_locations.get(&key) else { return Ok(()) };
25922596

25932597
// Generate a unique ID so users can link to this section for a given method
25942598
let id = cx.derive_id("scraped-examples");
@@ -2602,8 +2606,7 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &Context<'_>, item: &clean
26022606
</h5>",
26032607
root_path = cx.root_path(),
26042608
id = id
2605-
)
2606-
.unwrap();
2609+
)?;
26072610

26082611
// Create a URL to a particular location in a reverse-dependency's source file
26092612
let link_to_loc = |call_data: &CallData, loc: &CallLocation| -> (String, String) {
@@ -2705,7 +2708,8 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &Context<'_>, item: &clean
27052708
title: init_title,
27062709
locations: locations_encoded,
27072710
}),
2708-
);
2711+
)
2712+
.unwrap();
27092713

27102714
true
27112715
};
@@ -2761,8 +2765,7 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &Context<'_>, item: &clean
27612765
<div class=\"hide-more\">Hide additional examples</div>\
27622766
<div class=\"more-scraped-examples\">\
27632767
<div class=\"toggle-line\"><div class=\"toggle-line-inner\"></div></div>"
2764-
)
2765-
.unwrap();
2768+
)?;
27662769

27672770
// Only generate inline code for MAX_FULL_EXAMPLES number of examples. Otherwise we could
27682771
// make the page arbitrarily huge!
@@ -2774,23 +2777,21 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &Context<'_>, item: &clean
27742777
if it.peek().is_some() {
27752778
w.write_str(
27762779
r#"<div class="example-links">Additional examples can be found in:<br><ul>"#,
2777-
)
2778-
.unwrap();
2779-
it.for_each(|(_, call_data)| {
2780+
)?;
2781+
it.try_for_each(|(_, call_data)| {
27802782
let (url, _) = link_to_loc(call_data, &call_data.locations[0]);
27812783
write!(
27822784
w,
27832785
r#"<li><a href="{url}">{name}</a></li>"#,
27842786
url = url,
27852787
name = call_data.display_name
27862788
)
2787-
.unwrap();
2788-
});
2789-
w.write_str("</ul></div>").unwrap();
2789+
})?;
2790+
w.write_str("</ul></div>")?;
27902791
}
27912792

2792-
w.write_str("</div></details>").unwrap();
2793+
w.write_str("</div></details>")?;
27932794
}
27942795

2795-
w.write_str("</div>").unwrap();
2796+
w.write_str("</div>")
27962797
}

src/librustdoc/html/render/sidebar.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::borrow::Cow;
22
use std::cmp::Ordering;
3+
use std::fmt;
34

45
use askama::Template;
56
use rustc_data_structures::fx::FxHashSet;
@@ -135,7 +136,11 @@ pub(crate) mod filters {
135136
}
136137
}
137138

138-
pub(super) fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut String) {
139+
pub(super) fn print_sidebar(
140+
cx: &Context<'_>,
141+
it: &clean::Item,
142+
mut buffer: impl fmt::Write,
143+
) -> fmt::Result {
139144
let mut ids = IdMap::new();
140145
let mut blocks: Vec<LinkBlock<'_>> = docblock_toc(cx, it, &mut ids).into_iter().collect();
141146
let deref_id_map = cx.deref_id_map.borrow();
@@ -195,7 +200,8 @@ pub(super) fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Str
195200
blocks,
196201
path,
197202
};
198-
sidebar.render_into(buffer).unwrap();
203+
sidebar.render_into(&mut buffer)?;
204+
Ok(())
199205
}
200206

201207
fn get_struct_fields_name<'a>(fields: &'a [clean::Item]) -> Vec<Link<'a>> {

src/librustdoc/html/sources.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ 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};
1614
use super::render::Context;
15+
use super::{highlight, layout};
1716
use crate::clean;
1817
use crate::clean::utils::has_doc_flag;
1918
use crate::docfs::PathError;
@@ -243,16 +242,16 @@ impl SourceCollector<'_, '_> {
243242
&shared.layout,
244243
&page,
245244
"",
246-
BufDisplay(|buf: &mut String| {
245+
fmt::from_fn(|f| {
247246
print_src(
248-
buf,
247+
f,
249248
contents,
250249
file_span,
251250
self.cx,
252251
&root_path,
253252
&highlight::DecorationInfo::default(),
254253
&source_context,
255-
);
254+
)
256255
}),
257256
&shared.style_files,
258257
);
@@ -331,7 +330,7 @@ pub(crate) fn print_src(
331330
root_path: &str,
332331
decoration_info: &highlight::DecorationInfo,
333332
source_context: &SourceContext<'_>,
334-
) {
333+
) -> fmt::Result {
335334
let mut lines = s.lines().count();
336335
let line_info = if let SourceContext::Embedded(info) = source_context {
337336
highlight::LineInfo::new_scraped(lines as u32, info.offset as u32)
@@ -367,12 +366,10 @@ pub(crate) fn print_src(
367366
},
368367
max_nb_digits,
369368
}
370-
.render_into(&mut writer)
371-
.unwrap(),
369+
.render_into(&mut writer),
372370
SourceContext::Embedded(info) => {
373-
ScrapedSource { info, code_html: code, max_nb_digits }
374-
.render_into(&mut writer)
375-
.unwrap();
371+
ScrapedSource { info, code_html: code, max_nb_digits }.render_into(&mut writer)
376372
}
377-
};
373+
}?;
374+
Ok(())
378375
}

0 commit comments

Comments
 (0)