Skip to content

Commit 1015ae9

Browse files
committed
make print_item return impl fmt::Display
1 parent 7872db9 commit 1015ae9

File tree

2 files changed

+129
-122
lines changed

2 files changed

+129
-122
lines changed

Diff for: src/librustdoc/html/render/context.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,7 @@ impl<'tcx> Context<'tcx> {
237237
};
238238

239239
if !render_redirect_pages {
240-
let mut page_buffer = String::new();
241-
print_item(self, it, &mut page_buffer);
240+
let content = print_item(self, it);
242241
let page = layout::Page {
243242
css_class: tyname_s,
244243
root_path: &self.root_path(),
@@ -254,7 +253,7 @@ impl<'tcx> Context<'tcx> {
254253
BufDisplay(|buf: &mut String| {
255254
print_sidebar(self, it, buf);
256255
}),
257-
page_buffer,
256+
content,
258257
&self.shared.style_files,
259258
)
260259
} else {

Diff for: src/librustdoc/html/render/print_item.rs

+127-119
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::formats::item_type::ItemType;
3232
use crate::html::escape::{Escape, EscapeBodyTextWithWbr};
3333
use crate::html::format::{
3434
Ending, PrintWithSpace, join_with_double_colon, print_abi_with_space,
35-
print_constness_with_space, print_where_clause, visibility_print_with_space, write_str,
35+
print_constness_with_space, print_where_clause, visibility_print_with_space,
3636
};
3737
use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
3838
use crate::html::render::{document_full, document_item_info};
@@ -162,131 +162,139 @@ struct ItemVars<'a> {
162162
src_href: Option<&'a str>,
163163
}
164164

165-
pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut String) {
165+
pub(super) fn print_item<'a, 'tcx>(
166+
cx: &'a Context<'tcx>,
167+
item: &'a clean::Item,
168+
) -> impl fmt::Display + 'a + Captures<'tcx> {
166169
debug_assert!(!item.is_stripped());
167-
let typ = match item.kind {
168-
clean::ModuleItem(_) => {
169-
if item.is_crate() {
170-
"Crate "
171-
} else {
172-
"Module "
170+
171+
fmt::from_fn(|buf| {
172+
let typ = match item.kind {
173+
clean::ModuleItem(_) => {
174+
if item.is_crate() {
175+
"Crate "
176+
} else {
177+
"Module "
178+
}
173179
}
174-
}
175-
clean::FunctionItem(..) | clean::ForeignFunctionItem(..) => "Function ",
176-
clean::TraitItem(..) => "Trait ",
177-
clean::StructItem(..) => "Struct ",
178-
clean::UnionItem(..) => "Union ",
179-
clean::EnumItem(..) => "Enum ",
180-
clean::TypeAliasItem(..) => "Type Alias ",
181-
clean::MacroItem(..) => "Macro ",
182-
clean::ProcMacroItem(ref mac) => match mac.kind {
183-
MacroKind::Bang => "Macro ",
184-
MacroKind::Attr => "Attribute Macro ",
185-
MacroKind::Derive => "Derive Macro ",
186-
},
187-
clean::PrimitiveItem(..) => "Primitive Type ",
188-
clean::StaticItem(..) | clean::ForeignStaticItem(..) => "Static ",
189-
clean::ConstantItem(..) => "Constant ",
190-
clean::ForeignTypeItem => "Foreign Type ",
191-
clean::KeywordItem => "Keyword ",
192-
clean::TraitAliasItem(..) => "Trait Alias ",
193-
_ => {
194-
// We don't generate pages for any other type.
195-
unreachable!();
196-
}
197-
};
198-
let stability_since_raw =
199-
render_stability_since_raw(item.stable_since(cx.tcx()), item.const_stability(cx.tcx()))
200-
.maybe_display()
201-
.to_string();
202-
203-
// Write source tag
204-
//
205-
// When this item is part of a `crate use` in a downstream crate, the
206-
// source link in the downstream documentation will actually come back to
207-
// this page, and this link will be auto-clicked. The `id` attribute is
208-
// used to find the link to auto-click.
209-
let src_href =
210-
if cx.info.include_sources && !item.is_primitive() { cx.src_href(item) } else { None };
211-
212-
let path_components = if item.is_primitive() || item.is_keyword() {
213-
vec![]
214-
} else {
215-
let cur = &cx.current;
216-
let amt = if item.is_mod() { cur.len() - 1 } else { cur.len() };
217-
cur.iter()
218-
.enumerate()
219-
.take(amt)
220-
.map(|(i, component)| PathComponent {
221-
path: "../".repeat(cur.len() - i - 1),
222-
name: *component,
223-
})
224-
.collect()
225-
};
180+
clean::FunctionItem(..) | clean::ForeignFunctionItem(..) => "Function ",
181+
clean::TraitItem(..) => "Trait ",
182+
clean::StructItem(..) => "Struct ",
183+
clean::UnionItem(..) => "Union ",
184+
clean::EnumItem(..) => "Enum ",
185+
clean::TypeAliasItem(..) => "Type Alias ",
186+
clean::MacroItem(..) => "Macro ",
187+
clean::ProcMacroItem(ref mac) => match mac.kind {
188+
MacroKind::Bang => "Macro ",
189+
MacroKind::Attr => "Attribute Macro ",
190+
MacroKind::Derive => "Derive Macro ",
191+
},
192+
clean::PrimitiveItem(..) => "Primitive Type ",
193+
clean::StaticItem(..) | clean::ForeignStaticItem(..) => "Static ",
194+
clean::ConstantItem(..) => "Constant ",
195+
clean::ForeignTypeItem => "Foreign Type ",
196+
clean::KeywordItem => "Keyword ",
197+
clean::TraitAliasItem(..) => "Trait Alias ",
198+
_ => {
199+
// We don't generate pages for any other type.
200+
unreachable!();
201+
}
202+
};
203+
let stability_since_raw =
204+
render_stability_since_raw(item.stable_since(cx.tcx()), item.const_stability(cx.tcx()))
205+
.maybe_display()
206+
.to_string();
226207

227-
let item_vars = ItemVars {
228-
typ,
229-
name: item.name.as_ref().unwrap().as_str(),
230-
item_type: &item.type_().to_string(),
231-
path_components,
232-
stability_since_raw: &stability_since_raw,
233-
src_href: src_href.as_deref(),
234-
};
208+
// Write source tag
209+
//
210+
// When this item is part of a `crate use` in a downstream crate, the
211+
// source link in the downstream documentation will actually come back to
212+
// this page, and this link will be auto-clicked. The `id` attribute is
213+
// used to find the link to auto-click.
214+
let src_href =
215+
if cx.info.include_sources && !item.is_primitive() { cx.src_href(item) } else { None };
216+
217+
let path_components = if item.is_primitive() || item.is_keyword() {
218+
vec![]
219+
} else {
220+
let cur = &cx.current;
221+
let amt = if item.is_mod() { cur.len() - 1 } else { cur.len() };
222+
cur.iter()
223+
.enumerate()
224+
.take(amt)
225+
.map(|(i, component)| PathComponent {
226+
path: "../".repeat(cur.len() - i - 1),
227+
name: *component,
228+
})
229+
.collect()
230+
};
235231

236-
item_vars.render_into(buf).unwrap();
232+
let item_vars = ItemVars {
233+
typ,
234+
name: item.name.as_ref().unwrap().as_str(),
235+
item_type: &item.type_().to_string(),
236+
path_components,
237+
stability_since_raw: &stability_since_raw,
238+
src_href: src_href.as_deref(),
239+
};
237240

238-
match &item.kind {
239-
clean::ModuleItem(ref m) => {
240-
write_str(buf, format_args!("{}", item_module(cx, item, &m.items)))
241-
}
242-
clean::FunctionItem(ref f) | clean::ForeignFunctionItem(ref f, _) => {
243-
write_str(buf, format_args!("{}", item_function(cx, item, f)))
244-
}
245-
clean::TraitItem(ref t) => write_str(buf, format_args!("{}", item_trait(cx, item, t))),
246-
clean::StructItem(ref s) => write_str(buf, format_args!("{}", item_struct(cx, item, s))),
247-
clean::UnionItem(ref s) => write_str(buf, format_args!("{}", item_union(cx, item, s))),
248-
clean::EnumItem(ref e) => write_str(buf, format_args!("{}", item_enum(cx, item, e))),
249-
clean::TypeAliasItem(ref t) => {
250-
write_str(buf, format_args!("{}", item_type_alias(cx, item, t)))
251-
}
252-
clean::MacroItem(ref m) => write_str(buf, format_args!("{}", item_macro(cx, item, m))),
253-
clean::ProcMacroItem(ref m) => {
254-
write_str(buf, format_args!("{}", item_proc_macro(cx, item, m)))
255-
}
256-
clean::PrimitiveItem(_) => write_str(buf, format_args!("{}", item_primitive(cx, item))),
257-
clean::StaticItem(ref i) => {
258-
write_str(buf, format_args!("{}", item_static(cx, item, i, None)))
259-
}
260-
clean::ForeignStaticItem(ref i, safety) => {
261-
write_str(buf, format_args!("{}", item_static(cx, item, i, Some(*safety))))
262-
}
263-
clean::ConstantItem(ci) => write_str(
264-
buf,
265-
format_args!("{}", item_constant(cx, item, &ci.generics, &ci.type_, &ci.kind)),
266-
),
267-
clean::ForeignTypeItem => write_str(buf, format_args!("{}", item_foreign_type(cx, item))),
268-
clean::KeywordItem => write_str(buf, format_args!("{}", item_keyword(cx, item))),
269-
clean::TraitAliasItem(ref ta) => {
270-
write_str(buf, format_args!("{}", item_trait_alias(cx, item, ta)))
271-
}
272-
_ => {
273-
// We don't generate pages for any other type.
274-
unreachable!();
275-
}
276-
}
241+
item_vars.render_into(buf).unwrap();
277242

278-
// Render notable-traits.js used for all methods in this module.
279-
let mut types_with_notable_traits = cx.types_with_notable_traits.borrow_mut();
280-
if !types_with_notable_traits.is_empty() {
281-
write_str(
282-
buf,
283-
format_args!(
243+
match &item.kind {
244+
clean::ModuleItem(ref m) => {
245+
write!(buf, "{}", item_module(cx, item, &m.items))
246+
}
247+
clean::FunctionItem(ref f) | clean::ForeignFunctionItem(ref f, _) => {
248+
write!(buf, "{}", item_function(cx, item, f))
249+
}
250+
clean::TraitItem(ref t) => write!(buf, "{}", item_trait(cx, item, t)),
251+
clean::StructItem(ref s) => {
252+
write!(buf, "{}", item_struct(cx, item, s))
253+
}
254+
clean::UnionItem(ref s) => write!(buf, "{}", item_union(cx, item, s)),
255+
clean::EnumItem(ref e) => write!(buf, "{}", item_enum(cx, item, e)),
256+
clean::TypeAliasItem(ref t) => {
257+
write!(buf, "{}", item_type_alias(cx, item, t))
258+
}
259+
clean::MacroItem(ref m) => write!(buf, "{}", item_macro(cx, item, m)),
260+
clean::ProcMacroItem(ref m) => {
261+
write!(buf, "{}", item_proc_macro(cx, item, m))
262+
}
263+
clean::PrimitiveItem(_) => write!(buf, "{}", item_primitive(cx, item)),
264+
clean::StaticItem(ref i) => {
265+
write!(buf, "{}", item_static(cx, item, i, None))
266+
}
267+
clean::ForeignStaticItem(ref i, safety) => {
268+
write!(buf, "{}", item_static(cx, item, i, Some(*safety)))
269+
}
270+
clean::ConstantItem(ci) => {
271+
write!(buf, "{}", item_constant(cx, item, &ci.generics, &ci.type_, &ci.kind))
272+
}
273+
clean::ForeignTypeItem => {
274+
write!(buf, "{}", item_foreign_type(cx, item))
275+
}
276+
clean::KeywordItem => write!(buf, "{}", item_keyword(cx, item)),
277+
clean::TraitAliasItem(ref ta) => {
278+
write!(buf, "{}", item_trait_alias(cx, item, ta))
279+
}
280+
_ => {
281+
// We don't generate pages for any other type.
282+
unreachable!();
283+
}
284+
}?;
285+
286+
// Render notable-traits.js used for all methods in this module.
287+
let mut types_with_notable_traits = cx.types_with_notable_traits.borrow_mut();
288+
if !types_with_notable_traits.is_empty() {
289+
write!(
290+
buf,
284291
r#"<script type="text/json" id="notable-traits-data">{}</script>"#,
285-
notable_traits_json(types_with_notable_traits.iter(), cx)
286-
),
287-
);
288-
types_with_notable_traits.clear();
289-
}
292+
notable_traits_json(types_with_notable_traits.iter(), cx),
293+
)?;
294+
types_with_notable_traits.clear();
295+
}
296+
Ok(())
297+
})
290298
}
291299

292300
/// For large structs, enums, unions, etc, determine whether to hide their fields

0 commit comments

Comments
 (0)