Skip to content

Commit c0b1c6e

Browse files
committed
librustdoc: more usages of Joined::joined
1 parent 4a76615 commit c0b1c6e

File tree

3 files changed

+61
-43
lines changed

3 files changed

+61
-43
lines changed

src/librustdoc/clean/utils.rs

+39-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::assert_matches::debug_assert_matches;
2-
use std::fmt::Write as _;
2+
use std::fmt::{self, Display, Write as _};
33
use std::mem;
44
use std::sync::LazyLock as Lazy;
55

@@ -24,6 +24,7 @@ use crate::clean::{
2424
clean_middle_ty, inline,
2525
};
2626
use crate::core::DocContext;
27+
use crate::display::Joined as _;
2728

2829
#[cfg(test)]
2930
mod tests;
@@ -250,16 +251,20 @@ pub(crate) fn qpath_to_string(p: &hir::QPath<'_>) -> String {
250251
hir::QPath::LangItem(lang_item, ..) => return lang_item.name().to_string(),
251252
};
252253

253-
let mut s = String::new();
254-
for (i, seg) in segments.iter().enumerate() {
255-
if i > 0 {
256-
s.push_str("::");
257-
}
258-
if seg.ident.name != kw::PathRoot {
259-
s.push_str(seg.ident.as_str());
260-
}
261-
}
262-
s
254+
fmt::from_fn(|f| {
255+
segments
256+
.iter()
257+
.map(|seg| {
258+
fmt::from_fn(|f| {
259+
if seg.ident.name != kw::PathRoot {
260+
write!(f, "{}", seg.ident)?;
261+
}
262+
Ok(())
263+
})
264+
})
265+
.joined("::", f)
266+
})
267+
.to_string()
263268
}
264269

265270
pub(crate) fn build_deref_target_impls(
@@ -311,24 +316,37 @@ pub(crate) fn name_from_pat(p: &hir::Pat<'_>) -> Symbol {
311316
PatKind::TupleStruct(ref p, ..)
312317
| PatKind::Expr(PatExpr { kind: PatExprKind::Path(ref p), .. }) => qpath_to_string(p),
313318
PatKind::Or(pats) => {
314-
pats.iter().map(|p| name_from_pat(p).to_string()).collect::<Vec<String>>().join(" | ")
319+
fmt::from_fn(|f| pats.iter().map(|p| name_from_pat(p)).joined(" | ", f)).to_string()
320+
}
321+
PatKind::Tuple(elts, _) => {
322+
format!("({})", fmt::from_fn(|f| elts.iter().map(|p| name_from_pat(p)).joined(", ", f)))
315323
}
316-
PatKind::Tuple(elts, _) => format!(
317-
"({})",
318-
elts.iter().map(|p| name_from_pat(p).to_string()).collect::<Vec<String>>().join(", ")
319-
),
320324
PatKind::Deref(p) => format!("deref!({})", name_from_pat(p)),
321325
PatKind::Expr(..) => {
322326
warn!(
323327
"tried to get argument name from PatKind::Expr, which is silly in function arguments"
324328
);
325329
return Symbol::intern("()");
326330
}
327-
PatKind::Slice(begin, ref mid, end) => {
328-
let begin = begin.iter().map(|p| name_from_pat(p).to_string());
329-
let mid = mid.as_ref().map(|p| format!("..{}", name_from_pat(p))).into_iter();
330-
let end = end.iter().map(|p| name_from_pat(p).to_string());
331-
format!("[{}]", begin.chain(mid).chain(end).collect::<Vec<_>>().join(", "))
331+
PatKind::Slice(begin, mid, end) => {
332+
fn print_pat<'a>(pat: &'a Pat<'a>, wild: bool) -> impl Display + 'a {
333+
fmt::from_fn(move |f| {
334+
if wild {
335+
f.write_str("..")?;
336+
}
337+
name_from_pat(pat).fmt(f)
338+
})
339+
}
340+
341+
format!(
342+
"[{}]",
343+
fmt::from_fn(|f| {
344+
let begin = begin.iter().map(|p| print_pat(p, false));
345+
let mid = mid.map(|p| print_pat(p, true));
346+
let end = end.iter().map(|p| print_pat(p, false));
347+
begin.chain(mid).chain(end).joined(", ", f)
348+
})
349+
)
332350
}
333351
})
334352
}

src/librustdoc/doctest/make.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Logic for transforming the raw code given by the user into something actually
22
//! runnable, e.g. by adding a `main` function if it doesn't already exist.
33
4+
use std::fmt::{self, Write as _};
45
use std::io;
56
use std::sync::Arc;
67

@@ -17,6 +18,7 @@ use rustc_span::symbol::sym;
1718
use tracing::debug;
1819

1920
use super::GlobalTestOptions;
21+
use crate::display::Joined as _;
2022
use crate::html::markdown::LangString;
2123

2224
/// This struct contains information about the doctest itself which is then used to generate
@@ -232,13 +234,15 @@ impl DocTestBuilder {
232234

233235
// add extra 4 spaces for each line to offset the code block
234236
if opts.insert_indent_space {
235-
prog.push_str(
236-
&everything_else
237+
write!(
238+
prog,
239+
"{}",
240+
fmt::from_fn(|f| everything_else
237241
.lines()
238-
.map(|line| format!(" {}", line))
239-
.collect::<Vec<String>>()
240-
.join("\n"),
241-
);
242+
.map(|line| fmt::from_fn(move |f| write!(f, " {line}")))
243+
.joined("\n", f))
244+
)
245+
.unwrap();
242246
} else {
243247
prog.push_str(everything_else);
244248
};

src/librustdoc/html/render/print_item.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::cmp::Ordering;
22
use std::fmt;
33
use std::fmt::Display;
44

5-
use itertools::Itertools;
65
use rinja::Template;
76
use rustc_abi::VariantIdx;
87
use rustc_data_structures::captures::Captures;
@@ -514,11 +513,7 @@ fn item_module(w: &mut String, cx: &Context<'_>, item: &clean::Item, items: &[cl
514513
class = myitem.type_(),
515514
unsafety_flag = unsafety_flag,
516515
href = item_path(myitem.type_(), myitem.name.unwrap().as_str()),
517-
title = [myitem.type_().to_string(), full_path(cx, myitem)]
518-
.iter()
519-
.filter_map(|s| if !s.is_empty() { Some(s.as_str()) } else { None })
520-
.collect::<Vec<_>>()
521-
.join(" "),
516+
title = format_args!("{} {}", myitem.type_(), full_path(cx, myitem)),
522517
),
523518
);
524519
}
@@ -915,7 +910,7 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
915910
w,
916911
format_args!(
917912
"<div class=\"stab must_implement\">At least one of the `{}` methods is required.</div>",
918-
list.iter().join("`, `")
913+
fmt::from_fn(|f| list.iter().joined("`, `", f))
919914
),
920915
);
921916
}
@@ -1168,17 +1163,18 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
11681163
js_src_path.extend(cx.current.iter().copied());
11691164
js_src_path.push_fmt(format_args!("{}.{}.js", it.type_(), it.name.unwrap()));
11701165
}
1171-
let extern_crates = extern_crates
1172-
.into_iter()
1173-
.map(|cnum| tcx.crate_name(cnum).to_string())
1174-
.collect::<Vec<_>>()
1175-
.join(",");
1176-
let (extern_before, extern_after) =
1177-
if extern_crates.is_empty() { ("", "") } else { (" data-ignore-extern-crates=\"", "\"") };
1166+
let extern_crates = fmt::from_fn(|f| {
1167+
if !extern_crates.is_empty() {
1168+
f.write_str(" data-ignore-extern-crates=\"")?;
1169+
extern_crates.iter().map(|&cnum| tcx.crate_name(cnum)).joined(",", f)?;
1170+
f.write_str("\"")?;
1171+
}
1172+
Ok(())
1173+
});
11781174
write_str(
11791175
w,
11801176
format_args!(
1181-
"<script src=\"{src}\"{extern_before}{extern_crates}{extern_after} async></script>",
1177+
"<script src=\"{src}\"{extern_crates} async></script>",
11821178
src = js_src_path.finish()
11831179
),
11841180
);
@@ -1400,7 +1396,7 @@ fn item_type_alias(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean
14001396
.collect();
14011397
js_src_path.extend(target_fqp[..target_fqp.len() - 1].iter().copied());
14021398
js_src_path.push_fmt(format_args!("{target_type}.{}.js", target_fqp.last().unwrap()));
1403-
let self_path = self_fqp.iter().map(Symbol::as_str).collect::<Vec<&str>>().join("::");
1399+
let self_path = fmt::from_fn(|f| self_fqp.iter().joined("::", f));
14041400
write_str(
14051401
w,
14061402
format_args!(

0 commit comments

Comments
 (0)