Skip to content

Commit 44eb1de

Browse files
committed
librustdoc: make bounds formatting lazy
1 parent a46db03 commit 44eb1de

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

src/librustdoc/html/render/print_item.rs

Lines changed: 22 additions & 23 deletions
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};
@@ -2050,27 +2050,26 @@ pub(super) fn item_path(ty: ItemType, name: &str) -> String {
20502050
}
20512051
}
20522052

2053-
fn bounds(t_bounds: &[clean::GenericBound], trait_alias: bool, cx: &Context<'_>) -> String {
2054-
let mut bounds = String::new();
2055-
if t_bounds.is_empty() {
2056-
return bounds;
2057-
}
2058-
let has_lots_of_bounds = t_bounds.len() > 2;
2059-
let inter_str = if has_lots_of_bounds { "\n + " } else { " + " };
2060-
if !trait_alias {
2061-
if has_lots_of_bounds {
2062-
bounds.push_str(":\n ");
2063-
} else {
2064-
bounds.push_str(": ");
2065-
}
2066-
}
2067-
write!(
2068-
bounds,
2069-
"{}",
2070-
fmt::from_fn(|f| t_bounds.iter().map(|p| p.print(cx)).joined(inter_str, f))
2071-
)
2072-
.unwrap();
2073-
bounds
2053+
fn bounds<'a, 'tcx>(
2054+
bounds: &'a [clean::GenericBound],
2055+
trait_alias: bool,
2056+
cx: &'a Context<'tcx>,
2057+
) -> impl Display + 'a + Captures<'tcx> {
2058+
(!bounds.is_empty())
2059+
.then_some(fmt::from_fn(move |f| {
2060+
let has_lots_of_bounds = bounds.len() > 2;
2061+
let inter_str = if has_lots_of_bounds { "\n + " } else { " + " };
2062+
if !trait_alias {
2063+
if has_lots_of_bounds {
2064+
f.write_str(":\n ")?;
2065+
} else {
2066+
f.write_str(": ")?;
2067+
}
2068+
}
2069+
2070+
bounds.iter().map(|p| p.print(cx)).joined(inter_str, f)
2071+
}))
2072+
.maybe_display()
20742073
}
20752074

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

0 commit comments

Comments
 (0)