Skip to content

Commit daccef1

Browse files
committed
make item_function return impl fmt::Display
1 parent 2672f0c commit daccef1

File tree

1 file changed

+50
-46
lines changed

1 file changed

+50
-46
lines changed

src/librustdoc/html/render/print_item.rs

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut String)
251251
write_str(buf, format_args!("{}", item_module(cx, item, &m.items)))
252252
}
253253
clean::FunctionItem(ref f) | clean::ForeignFunctionItem(ref f, _) => {
254-
item_function(buf, cx, item, f)
254+
write_str(buf, format_args!("{}", item_function(cx, item, f)))
255255
}
256256
clean::TraitItem(ref t) => item_trait(buf, cx, item, t),
257257
clean::StructItem(ref s) => item_struct(buf, cx, item, s),
@@ -587,44 +587,47 @@ fn extra_info_tags<'a, 'tcx: 'a>(
587587
})
588588
}
589589

590-
fn item_function(w: &mut String, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) {
591-
let tcx = cx.tcx();
592-
let header = it.fn_header(tcx).expect("printing a function which isn't a function");
593-
debug!(
594-
"item_function/const: {:?} {:?} {:?} {:?}",
595-
it.name,
596-
&header.constness,
597-
it.stable_since(tcx),
598-
it.const_stability(tcx),
599-
);
600-
let constness = print_constness_with_space(
601-
&header.constness,
602-
it.stable_since(tcx),
603-
it.const_stability(tcx),
604-
);
605-
let safety = header.safety.print_with_space();
606-
let abi = print_abi_with_space(header.abi).to_string();
607-
let asyncness = header.asyncness.print_with_space();
608-
let visibility = visibility_print_with_space(it, cx).to_string();
609-
let name = it.name.unwrap();
610-
611-
let generics_len = format!("{:#}", f.generics.print(cx)).len();
612-
let header_len = "fn ".len()
613-
+ visibility.len()
614-
+ constness.len()
615-
+ asyncness.len()
616-
+ safety.len()
617-
+ abi.len()
618-
+ name.as_str().len()
619-
+ generics_len;
620-
621-
let notable_traits = notable_traits_button(&f.decl.output, cx).maybe_display();
622-
623-
wrap_item(w, |w| {
624-
w.reserve(header_len);
625-
write_str(
626-
w,
627-
format_args!(
590+
fn item_function<'a, 'tcx>(
591+
cx: &'a Context<'tcx>,
592+
it: &'a clean::Item,
593+
f: &'a clean::Function,
594+
) -> impl fmt::Display + 'a + Captures<'tcx> {
595+
fmt::from_fn(|w| {
596+
let tcx = cx.tcx();
597+
let header = it.fn_header(tcx).expect("printing a function which isn't a function");
598+
debug!(
599+
"item_function/const: {:?} {:?} {:?} {:?}",
600+
it.name,
601+
&header.constness,
602+
it.stable_since(tcx),
603+
it.const_stability(tcx),
604+
);
605+
let constness = print_constness_with_space(
606+
&header.constness,
607+
it.stable_since(tcx),
608+
it.const_stability(tcx),
609+
);
610+
let safety = header.safety.print_with_space();
611+
let abi = print_abi_with_space(header.abi).to_string();
612+
let asyncness = header.asyncness.print_with_space();
613+
let visibility = visibility_print_with_space(it, cx).to_string();
614+
let name = it.name.unwrap();
615+
616+
let generics_len = format!("{:#}", f.generics.print(cx)).len();
617+
let header_len = "fn ".len()
618+
+ visibility.len()
619+
+ constness.len()
620+
+ asyncness.len()
621+
+ safety.len()
622+
+ abi.len()
623+
+ name.as_str().len()
624+
+ generics_len;
625+
626+
let notable_traits = notable_traits_button(&f.decl.output, cx).maybe_display();
627+
628+
wrap_item(w, |w| {
629+
write!(
630+
w,
628631
"{attrs}{vis}{constness}{asyncness}{safety}{abi}fn \
629632
{name}{generics}{decl}{notable_traits}{where_clause}",
630633
attrs = render_attributes_in_pre(it, "", cx),
@@ -637,10 +640,10 @@ fn item_function(w: &mut String, cx: &Context<'_>, it: &clean::Item, f: &clean::
637640
generics = f.generics.print(cx),
638641
where_clause = print_where_clause(&f.generics, cx, 0, Ending::Newline),
639642
decl = f.decl.full_print(header_len, 0, cx),
640-
),
641-
);
642-
});
643-
write_str(w, format_args!("{}", document(cx, it, None, HeadingOffset::H2)));
643+
)
644+
})?;
645+
write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
646+
})
644647
}
645648

646649
fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Trait) {
@@ -2249,14 +2252,15 @@ fn bounds<'a, 'tcx>(
22492252
.maybe_display()
22502253
}
22512254

2252-
fn wrap_item<W, F>(w: &mut W, f: F)
2255+
fn wrap_item<W, F, T>(w: &mut W, f: F) -> T
22532256
where
22542257
W: fmt::Write,
2255-
F: FnOnce(&mut W),
2258+
F: FnOnce(&mut W) -> T,
22562259
{
22572260
write!(w, r#"<pre class="rust item-decl"><code>"#).unwrap();
2258-
f(w);
2261+
let res = f(w);
22592262
write!(w, "</code></pre>").unwrap();
2263+
res
22602264
}
22612265

22622266
#[derive(PartialEq, Eq)]

0 commit comments

Comments
 (0)