Skip to content

Commit 9436b4a

Browse files
committed
librustdoc: make bounds formatting lazy
1 parent e4636e2 commit 9436b4a

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

src/librustdoc/html/render/print_item.rs

+22-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::cmp::Ordering;
22
use std::fmt;
3-
use std::fmt::{Display, Write};
3+
use std::fmt::Display;
44

55
use itertools::Itertools;
66
use rinja::Template;
@@ -27,7 +27,7 @@ use super::{
2727
};
2828
use crate::clean;
2929
use crate::config::ModuleSorting;
30-
use crate::display::Joined as _;
30+
use crate::display::{Joined as _, MaybeDisplay as _};
3131
use crate::formats::Impl;
3232
use crate::formats::item_type::ItemType;
3333
use crate::html::escape::{Escape, EscapeBodyTextWithWbr};
@@ -2123,27 +2123,26 @@ pub(super) fn item_path(ty: ItemType, name: &str) -> String {
21232123
}
21242124
}
21252125

2126-
fn bounds(t_bounds: &[clean::GenericBound], trait_alias: bool, cx: &Context<'_>) -> String {
2127-
let mut bounds = String::new();
2128-
if t_bounds.is_empty() {
2129-
return bounds;
2130-
}
2131-
let has_lots_of_bounds = t_bounds.len() > 2;
2132-
let inter_str = if has_lots_of_bounds { "\n + " } else { " + " };
2133-
if !trait_alias {
2134-
if has_lots_of_bounds {
2135-
bounds.push_str(":\n ");
2136-
} else {
2137-
bounds.push_str(": ");
2138-
}
2139-
}
2140-
write!(
2141-
bounds,
2142-
"{}",
2143-
fmt::from_fn(|f| t_bounds.iter().map(|p| p.print(cx)).joined(inter_str, f))
2144-
)
2145-
.unwrap();
2146-
bounds
2126+
fn bounds<'a, 'tcx>(
2127+
bounds: &'a [clean::GenericBound],
2128+
trait_alias: bool,
2129+
cx: &'a Context<'tcx>,
2130+
) -> impl Display + 'a + Captures<'tcx> {
2131+
(!bounds.is_empty())
2132+
.then_some(fmt::from_fn(move |f| {
2133+
let has_lots_of_bounds = bounds.len() > 2;
2134+
let inter_str = if has_lots_of_bounds { "\n + " } else { " + " };
2135+
if !trait_alias {
2136+
if has_lots_of_bounds {
2137+
f.write_str(":\n ")?;
2138+
} else {
2139+
f.write_str(": ")?;
2140+
}
2141+
}
2142+
2143+
bounds.iter().map(|p| p.print(cx)).joined(inter_str, f)
2144+
}))
2145+
.maybe_display()
21472146
}
21482147

21492148
fn wrap_item<W, F>(w: &mut W, f: F)

0 commit comments

Comments
 (0)