Skip to content

Commit 58a622b

Browse files
authored
Rollup merge of rust-lang#136599 - yotamofek:pr/rustdoc-more-joined, r=GuillaumeGomez
librustdoc: more usages of `Joined::joined` Some missed opportunities from rust-lang#136244 r? ```@GuillaumeGomez``` since you reviewed the last one (feel free to re-assign, of course 😊) First two commits are just drive-by cleanups
2 parents 7edd17c + c0b1c6e commit 58a622b

File tree

4 files changed

+70
-51
lines changed

4 files changed

+70
-51
lines changed

src/librustdoc/clean/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1150,9 +1150,9 @@ fn clean_args_from_types_and_body_id<'tcx>(
11501150
Arguments {
11511151
values: types
11521152
.iter()
1153-
.enumerate()
1154-
.map(|(i, ty)| Argument {
1155-
name: name_from_pat(body.params[i].pat),
1153+
.zip(body.params)
1154+
.map(|(ty, param)| Argument {
1155+
name: name_from_pat(param.pat),
11561156
type_: clean_ty(ty, cx),
11571157
is_const: false,
11581158
})

src/librustdoc/clean/utils.rs

+45-26
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(
@@ -299,35 +304,49 @@ pub(crate) fn name_from_pat(p: &hir::Pat<'_>) -> Symbol {
299304

300305
Symbol::intern(&match p.kind {
301306
// FIXME(never_patterns): does this make sense?
302-
PatKind::Wild | PatKind::Err(_) | PatKind::Never | PatKind::Struct(..) => {
307+
PatKind::Wild
308+
| PatKind::Err(_)
309+
| PatKind::Never
310+
| PatKind::Struct(..)
311+
| PatKind::Range(..) => {
303312
return kw::Underscore;
304313
}
305314
PatKind::Binding(_, _, ident, _) => return ident.name,
315+
PatKind::Box(p) | PatKind::Ref(p, _) | PatKind::Guard(p, _) => return name_from_pat(p),
306316
PatKind::TupleStruct(ref p, ..)
307317
| PatKind::Expr(PatExpr { kind: PatExprKind::Path(ref p), .. }) => qpath_to_string(p),
308318
PatKind::Or(pats) => {
309-
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)))
310323
}
311-
PatKind::Tuple(elts, _) => format!(
312-
"({})",
313-
elts.iter().map(|p| name_from_pat(p).to_string()).collect::<Vec<String>>().join(", ")
314-
),
315-
PatKind::Box(p) => return name_from_pat(p),
316324
PatKind::Deref(p) => format!("deref!({})", name_from_pat(p)),
317-
PatKind::Ref(p, _) => return name_from_pat(p),
318325
PatKind::Expr(..) => {
319326
warn!(
320327
"tried to get argument name from PatKind::Expr, which is silly in function arguments"
321328
);
322329
return Symbol::intern("()");
323330
}
324-
PatKind::Guard(p, _) => return name_from_pat(p),
325-
PatKind::Range(..) => return kw::Underscore,
326-
PatKind::Slice(begin, ref mid, end) => {
327-
let begin = begin.iter().map(|p| name_from_pat(p).to_string());
328-
let mid = mid.as_ref().map(|p| format!("..{}", name_from_pat(p))).into_iter();
329-
let end = end.iter().map(|p| name_from_pat(p).to_string());
330-
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+
)
331350
}
332351
})
333352
}

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)