Skip to content

Commit 927edf9

Browse files
committed
another attempt
1 parent 66e29e0 commit 927edf9

File tree

4 files changed

+76
-77
lines changed

4 files changed

+76
-77
lines changed

src/librustdoc/clean/cfg.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_span::Span;
1414
use rustc_span::symbol::{Symbol, sym};
1515

1616
use crate::html::escape::Escape;
17-
use crate::join::Join as _;
17+
use crate::join::Joined as _;
1818

1919
#[cfg(test)]
2020
mod tests;
@@ -417,25 +417,27 @@ impl Display<'_> {
417417
}
418418
};
419419

420-
(|| {
421-
sub_cfgs.iter().map(|sub_cfg| {
422-
fmt::from_fn(move |fmt| {
423-
if let Cfg::Cfg(_, Some(feat)) = sub_cfg
424-
&& short_longhand
425-
{
426-
if self.1.is_html() {
427-
write!(fmt, "<code>{feat}</code>")?;
420+
fmt::from_fn(|f| {
421+
sub_cfgs
422+
.iter()
423+
.map(|sub_cfg| {
424+
fmt::from_fn(move |fmt| {
425+
if let Cfg::Cfg(_, Some(feat)) = sub_cfg
426+
&& short_longhand
427+
{
428+
if self.1.is_html() {
429+
write!(fmt, "<code>{feat}</code>")?;
430+
} else {
431+
write!(fmt, "`{feat}`")?;
432+
}
428433
} else {
429-
write!(fmt, "`{feat}`")?;
434+
write_with_opt_paren(fmt, !sub_cfg.is_all(), Display(sub_cfg, self.1))?;
430435
}
431-
} else {
432-
write_with_opt_paren(fmt, !sub_cfg.is_all(), Display(sub_cfg, self.1))?;
433-
}
434-
Ok(())
436+
Ok(())
437+
})
435438
})
436-
})
439+
.joined(separator, f)
437440
})
438-
.join(separator)
439441
.fmt(fmt)?;
440442

441443
Ok(())
@@ -450,8 +452,10 @@ impl fmt::Display for Display<'_> {
450452
let separator =
451453
if sub_cfgs.iter().all(Cfg::is_simple) { " nor " } else { ", nor " };
452454
fmt.write_str("neither ")?;
453-
(|| {
454-
sub_cfgs.iter().map(|sub_cfg| {
455+
456+
sub_cfgs
457+
.iter()
458+
.map(|sub_cfg| {
455459
fmt::from_fn(|fmt| {
456460
write_with_opt_paren(
457461
fmt,
@@ -460,9 +464,7 @@ impl fmt::Display for Display<'_> {
460464
)
461465
})
462466
})
463-
})
464-
.join(separator)
465-
.fmt(fmt)
467+
.joined(separator, fmt)
466468
}
467469
ref simple @ Cfg::Cfg(..) => write!(fmt, "non-{}", Display(simple, self.1)),
468470
ref c => write!(fmt, "not ({})", Display(c, self.1)),

src/librustdoc/html/format.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::formats::cache::Cache;
3333
use crate::formats::item_type::ItemType;
3434
use crate::html::escape::{Escape, EscapeBodyText};
3535
use crate::html::render::Context;
36-
use crate::join::Join as _;
36+
use crate::join::Joined as _;
3737
use crate::passes::collect_intra_doc_links::UrlFragment;
3838

3939
pub(crate) trait Print {
@@ -150,13 +150,13 @@ pub(crate) fn print_generic_bounds<'a, 'tcx: 'a>(
150150
cx: &'a Context<'tcx>,
151151
) -> impl Display + 'a + Captures<'tcx> {
152152
fmt::from_fn(move |f| {
153-
(|| {
154-
let mut bounds_dup = FxHashSet::default();
153+
let mut bounds_dup = FxHashSet::default();
155154

156-
bounds.iter().filter(move |b| bounds_dup.insert(*b)).map(|bound| bound.print(cx))
157-
})
158-
.join(" + ")
159-
.fmt(f)
155+
bounds
156+
.iter()
157+
.filter(move |b| bounds_dup.insert(*b))
158+
.map(|bound| bound.print(cx))
159+
.joined(" + ", f)
160160
})
161161
}
162162

@@ -171,7 +171,7 @@ impl clean::GenericParamDef {
171171

172172
if !outlives.is_empty() {
173173
f.write_str(": ")?;
174-
write!(f, "{}", (|| outlives.iter().map(|lt| lt.print())).join(" + "))?;
174+
outlives.iter().map(|lt| lt.print()).joined(" + ", f)?; // TODO: keep fmt options?
175175
}
176176

177177
Ok(())
@@ -221,7 +221,8 @@ impl clean::Generics {
221221
return Ok(());
222222
}
223223

224-
let real_params = (|| real_params.clone().map(|g| g.print(cx))).join(", ");
224+
let real_params =
225+
fmt::from_fn(|f| real_params.clone().map(|g| g.print(cx)).joined(", ", f));
225226
if f.alternate() {
226227
write!(f, "<{:#}>", real_params)
227228
} else {
@@ -293,7 +294,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
293294
})
294295
};
295296

296-
let where_preds = where_predicates.join(",");
297+
let where_preds = fmt::from_fn(|f| where_predicates().joined(",", f));
297298
let clause = if f.alternate() {
298299
if ending == Ending::Newline {
299300
format!(" where{where_preds},")
@@ -390,7 +391,7 @@ impl clean::GenericBound {
390391
} else {
391392
f.write_str("use&lt;")?;
392393
}
393-
(|| args.iter()).join(", ").fmt(f)?;
394+
args.iter().joined(", ", f)?;
394395
if f.alternate() { f.write_str(">") } else { f.write_str("&gt;") }
395396
}
396397
})
@@ -494,7 +495,7 @@ pub(crate) enum HrefError {
494495
// Panics if `syms` is empty.
495496
pub(crate) fn join_with_double_colon(syms: &[Symbol]) -> String {
496497
let mut s = String::with_capacity(estimate_item_path_byte_length(syms.len()));
497-
write!(s, "{}", (|| syms.iter()).join("::")).unwrap();
498+
write!(s, "{}", fmt::from_fn(|f| syms.iter().joined("::", f))).unwrap();
498499
s
499500
}
500501

@@ -544,14 +545,14 @@ fn generate_macro_def_id_path(
544545
let url = match cache.extern_locations[&def_id.krate] {
545546
ExternalLocation::Remote(ref s) => {
546547
// `ExternalLocation::Remote` always end with a `/`.
547-
format!("{s}{path}", path = (|| path.iter()).join("/"))
548+
format!("{s}{path}", path = fmt::from_fn(|f| path.iter().joined("/", f)))
548549
}
549550
ExternalLocation::Local => {
550551
// `root_path` always end with a `/`.
551552
format!(
552553
"{root_path}{path}",
553554
root_path = root_path.unwrap_or(""),
554-
path = (|| path.iter()).join("/")
555+
path = fmt::from_fn(|f| path.iter().joined("/", f))
555556
)
556557
}
557558
ExternalLocation::Unknown => {
@@ -915,7 +916,7 @@ fn tybounds<'a, 'tcx: 'a>(
915916
cx: &'a Context<'tcx>,
916917
) -> impl Display + 'a + Captures<'tcx> {
917918
fmt::from_fn(move |f| {
918-
(|| bounds.iter().map(|bound| bound.print(cx))).join(" + ").fmt(f)?;
919+
bounds.iter().map(|bound| bound.print(cx)).joined(" + ", f)?;
919920
if let Some(lt) = lt {
920921
// We don't need to check `alternate` since we can be certain that
921922
// the lifetime doesn't contain any characters which need escaping.
@@ -934,7 +935,7 @@ fn print_higher_ranked_params_with_space<'a, 'tcx: 'a>(
934935
if !params.is_empty() {
935936
f.write_str(keyword)?;
936937
f.write_str(if f.alternate() { "<" } else { "&lt;" })?;
937-
(|| params.iter().map(|lt| lt.print(cx))).join(", ").fmt(f)?;
938+
params.iter().map(|lt| lt.print(cx)).joined(", ", f)?;
938939
f.write_str(if f.alternate() { "> " } else { "&gt; " })?;
939940
}
940941
Ok(())
@@ -1025,12 +1026,15 @@ fn fmt_type(
10251026
primitive_link(
10261027
f,
10271028
PrimitiveType::Tuple,
1028-
format_args!("({})", (|| generic_names.iter()).join(", ")),
1029+
format_args!(
1030+
"({})",
1031+
fmt::from_fn(|f| generic_names.iter().joined(", ", f))
1032+
),
10291033
cx,
10301034
)
10311035
} else {
10321036
f.write_str("(")?;
1033-
(|| many.iter().map(|item| item.print(cx))).join(", ").fmt(f)?;
1037+
many.iter().map(|item| item.print(cx)).joined(", ", f)?;
10341038
f.write_str(")")
10351039
}
10361040
}
@@ -1360,16 +1364,15 @@ impl clean::Arguments {
13601364
cx: &'a Context<'tcx>,
13611365
) -> impl Display + 'a + Captures<'tcx> {
13621366
fmt::from_fn(move |f| {
1363-
(|| {
1364-
self.values.iter().map(|input| {
1367+
self.values
1368+
.iter()
1369+
.map(|input| {
13651370
fmt::from_fn(|f| {
13661371
write!(f, "{}: ", input.name)?;
13671372
input.type_.print(cx).fmt(f)
13681373
})
13691374
})
1370-
})
1371-
.join(", ")
1372-
.fmt(f)
1375+
.joined(", ", f)
13731376
})
13741377
}
13751378
}

src/librustdoc/html/render/print_item.rs

Lines changed: 13 additions & 11 deletions
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, Write};
44

5-
use itertools::Itertools;
65
use rinja::Template;
76
use rustc_abi::VariantIdx;
87
use rustc_data_structures::captures::Captures;
@@ -37,7 +36,7 @@ use crate::html::format::{
3736
use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
3837
use crate::html::render::{document_full, document_item_info};
3938
use crate::html::url_parts_builder::UrlPartsBuilder;
40-
use crate::join::Join as _;
39+
use crate::join::Joined as _;
4140

4241
/// Generates a Rinja template struct for rendering items with common methods.
4342
///
@@ -499,11 +498,11 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
499498
class = myitem.type_(),
500499
unsafety_flag = unsafety_flag,
501500
href = item_path(myitem.type_(), myitem.name.unwrap().as_str()),
502-
title = [myitem.type_().to_string(), full_path(cx, myitem)]
501+
title = fmt::from_fn(|f| [myitem.type_().to_string(), full_path(cx, myitem)]
503502
.iter()
504503
.filter_map(|s| if !s.is_empty() { Some(s.as_str()) } else { None })
505504
.collect::<Vec<_>>()
506-
.join(" "),
505+
.joined(" ", f)),
507506
);
508507
}
509508
}
@@ -883,7 +882,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
883882
write!(
884883
w,
885884
"<div class=\"stab must_implement\">At least one of the `{}` methods is required.</div>",
886-
list.iter().join("`, `")
885+
fmt::from_fn(|f| list.iter().joined("`, `", f))
887886
);
888887
}
889888

@@ -1433,17 +1432,15 @@ fn print_tuple_struct_fields<'a, 'cx: 'a>(
14331432
return f.write_str("<span class=\"comment\">/* private fields */</span>");
14341433
}
14351434

1436-
(|| {
1437-
s.iter().map(|ty| {
1435+
s.iter()
1436+
.map(|ty| {
14381437
fmt::from_fn(|f| match ty.kind {
14391438
clean::StrippedItem(box clean::StructFieldItem(_)) => f.write_str("_"),
14401439
clean::StructFieldItem(ref ty) => write!(f, "{}", ty.print(cx)),
14411440
_ => unreachable!(),
14421441
})
14431442
})
1444-
})
1445-
.join(", ")
1446-
.fmt(f)
1443+
.joined(", ", f)
14471444
})
14481445
}
14491446

@@ -2066,7 +2063,12 @@ fn bounds(t_bounds: &[clean::GenericBound], trait_alias: bool, cx: &Context<'_>)
20662063
bounds.push_str(": ");
20672064
}
20682065
}
2069-
write!(bounds, "{}", (|| t_bounds.iter().map(|p| p.print(cx))).join(inter_str)).unwrap();
2066+
write!(
2067+
bounds,
2068+
"{}",
2069+
fmt::from_fn(|f| t_bounds.iter().map(|p| p.print(cx)).joined(inter_str, f))
2070+
)
2071+
.unwrap();
20702072
bounds
20712073
}
20722074

src/librustdoc/join.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
1-
use std::fmt::{self, Display};
1+
use std::fmt::{self, Display, Formatter};
22

3-
pub(crate) trait Join {
4-
fn join<'f, 'sep: 'f>(self, sep: &'sep str) -> impl Display + 'f
5-
where
6-
Self: 'f;
3+
pub(crate) trait Joined {
4+
fn joined(self, sep: &str, f: &mut Formatter<'_>) -> fmt::Result;
75
}
86

9-
impl<F, I, T> Join for F
7+
impl<I, T> Joined for I
108
where
11-
F: Fn() -> I,
12-
I: Iterator<Item = T>,
9+
I: IntoIterator<Item = T>,
1310
T: Display,
1411
{
15-
fn join<'f, 'sep: 'f>(self, sep: &'sep str) -> impl Display + 'f
16-
where
17-
Self: 'f,
18-
{
19-
fmt::from_fn(move |f| {
20-
let mut iter = self();
21-
let Some(first) = iter.next() else { return Ok(()) };
22-
first.fmt(f)?;
23-
while let Some(item) = iter.next() {
24-
f.write_str(sep)?;
25-
item.fmt(f)?;
26-
}
27-
Ok(())
28-
})
12+
fn joined(self, sep: &str, f: &mut Formatter<'_>) -> fmt::Result {
13+
let mut iter = self.into_iter();
14+
let Some(first) = iter.next() else { return Ok(()) };
15+
first.fmt(f)?;
16+
while let Some(item) = iter.next() {
17+
f.write_str(sep)?;
18+
item.fmt(f)?;
19+
}
20+
Ok(())
2921
}
3022
}

0 commit comments

Comments
 (0)