Skip to content

Commit c1c1e86

Browse files
Item to function
1 parent a502e85 commit c1c1e86

File tree

1 file changed

+79
-84
lines changed

1 file changed

+79
-84
lines changed

src/librustdoc/html/render.rs

+79-84
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,6 @@ pub struct RenderInfo {
381381
// Helper structs for rendering items/sidebars and carrying along contextual
382382
// information
383383

384-
#[derive(Copy, Clone)]
385-
struct Item<'a> {
386-
cx: &'a Context,
387-
item: &'a clean::Item,
388-
}
389-
390384
/// Struct representing one entry in the JS search index. These are all emitted
391385
/// by hand to a large JS file at the end of cache-creation.
392386
#[derive(Debug)]
@@ -1974,7 +1968,7 @@ impl Context {
19741968
if !self.render_redirect_pages {
19751969
layout::render(&self.shared.layout, &page,
19761970
|buf: &mut _| print_sidebar(self, it, buf),
1977-
|buf: &mut Buffer| buf.from_display(Item { cx: self, item: it }),
1971+
|buf: &mut _| print_item(self, it, buf),
19781972
&self.shared.themes)
19791973
} else {
19801974
let mut url = self.root_path();
@@ -2115,7 +2109,7 @@ impl Context {
21152109
}
21162110
}
21172111

2118-
impl<'a> Item<'a> {
2112+
impl Context {
21192113
/// Generates a url appropriate for an `href` attribute back to the source of
21202114
/// this item.
21212115
///
@@ -2125,26 +2119,26 @@ impl<'a> Item<'a> {
21252119
/// If `None` is returned, then a source link couldn't be generated. This
21262120
/// may happen, for example, with externally inlined items where the source
21272121
/// of their crate documentation isn't known.
2128-
fn src_href(&self) -> Option<String> {
2129-
let mut root = self.cx.root_path();
2122+
fn src_href(&self, item: &clean::Item) -> Option<String> {
2123+
let mut root = self.root_path();
21302124

21312125
let cache = cache();
21322126
let mut path = String::new();
21332127

21342128
// We can safely ignore macros from other libraries
2135-
let file = match self.item.source.filename {
2129+
let file = match item.source.filename {
21362130
FileName::Real(ref path) => path,
21372131
_ => return None,
21382132
};
21392133

2140-
let (krate, path) = if self.item.def_id.is_local() {
2141-
if let Some(path) = self.cx.shared.local_sources.get(file) {
2142-
(&self.cx.shared.layout.krate, path)
2134+
let (krate, path) = if item.def_id.is_local() {
2135+
if let Some(path) = self.shared.local_sources.get(file) {
2136+
(&self.shared.layout.krate, path)
21432137
} else {
21442138
return None;
21452139
}
21462140
} else {
2147-
let (krate, src_root) = match *cache.extern_locations.get(&self.item.def_id.krate)? {
2141+
let (krate, src_root) = match *cache.extern_locations.get(&item.def_id.krate)? {
21482142
(ref name, ref src, Local) => (name, src),
21492143
(ref name, ref src, Remote(ref s)) => {
21502144
root = s.to_string();
@@ -2164,10 +2158,10 @@ impl<'a> Item<'a> {
21642158
(krate, &path)
21652159
};
21662160

2167-
let lines = if self.item.source.loline == self.item.source.hiline {
2168-
self.item.source.loline.to_string()
2161+
let lines = if item.source.loline == item.source.hiline {
2162+
item.source.loline.to_string()
21692163
} else {
2170-
format!("{}-{}", self.item.source.loline, self.item.source.hiline)
2164+
format!("{}-{}", item.source.loline, item.source.hiline)
21712165
};
21722166
Some(format!("{root}src/{krate}/{path}#{lines}",
21732167
root = Escape(&root),
@@ -2185,108 +2179,109 @@ where F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result {
21852179
write!(w, "</div>")
21862180
}
21872181

2188-
impl<'a> fmt::Display for Item<'a> {
2189-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2190-
debug_assert!(!self.item.is_stripped());
2182+
fn print_item(cx: &Context, item: &clean::Item, buf: &mut Buffer) {
2183+
debug_assert!(!item.is_stripped());
21912184
// Write the breadcrumb trail header for the top
2192-
write!(fmt, "<h1 class='fqn'><span class='out-of-band'>")?;
2193-
if let Some(version) = self.item.stable_since() {
2194-
write!(fmt, "<span class='since' title='Stable since Rust version {0}'>{0}</span>",
2195-
version)?;
2185+
write!(buf, "<h1 class='fqn'><span class='out-of-band'>");
2186+
if let Some(version) = item.stable_since() {
2187+
write!(buf, "<span class='since' title='Stable since Rust version {0}'>{0}</span>",
2188+
version);
21962189
}
2197-
write!(fmt,
2190+
write!(buf,
21982191
"<span id='render-detail'>\
21992192
<a id=\"toggle-all-docs\" href=\"javascript:void(0)\" \
22002193
title=\"collapse all docs\">\
22012194
[<span class='inner'>&#x2212;</span>]\
22022195
</a>\
2203-
</span>")?;
2196+
</span>");
22042197

22052198
// Write `src` tag
22062199
//
22072200
// When this item is part of a `pub use` in a downstream crate, the
22082201
// [src] link in the downstream documentation will actually come back to
22092202
// this page, and this link will be auto-clicked. The `id` attribute is
22102203
// used to find the link to auto-click.
2211-
if self.cx.shared.include_sources && !self.item.is_primitive() {
2212-
if let Some(l) = self.src_href() {
2213-
write!(fmt, "<a class='srclink' href='{}' title='{}'>[src]</a>",
2214-
l, "goto source code")?;
2204+
if cx.shared.include_sources && !item.is_primitive() {
2205+
if let Some(l) = cx.src_href(item) {
2206+
write!(buf, "<a class='srclink' href='{}' title='{}'>[src]</a>",
2207+
l, "goto source code");
22152208
}
22162209
}
22172210

2218-
write!(fmt, "</span>")?; // out-of-band
2219-
write!(fmt, "<span class='in-band'>")?;
2220-
match self.item.inner {
2211+
write!(buf, "</span>"); // out-of-band
2212+
write!(buf, "<span class='in-band'>");
2213+
let name = match item.inner {
22212214
clean::ModuleItem(ref m) => if m.is_crate {
2222-
write!(fmt, "Crate ")?;
2215+
"Crate "
22232216
} else {
2224-
write!(fmt, "Module ")?;
2217+
"Module "
22252218
},
2226-
clean::FunctionItem(..) | clean::ForeignFunctionItem(..) => write!(fmt, "Function ")?,
2227-
clean::TraitItem(..) => write!(fmt, "Trait ")?,
2228-
clean::StructItem(..) => write!(fmt, "Struct ")?,
2229-
clean::UnionItem(..) => write!(fmt, "Union ")?,
2230-
clean::EnumItem(..) => write!(fmt, "Enum ")?,
2231-
clean::TypedefItem(..) => write!(fmt, "Type Definition ")?,
2232-
clean::MacroItem(..) => write!(fmt, "Macro ")?,
2219+
clean::FunctionItem(..) | clean::ForeignFunctionItem(..) => "Function ",
2220+
clean::TraitItem(..) => "Trait ",
2221+
clean::StructItem(..) => "Struct ",
2222+
clean::UnionItem(..) => "Union ",
2223+
clean::EnumItem(..) => "Enum ",
2224+
clean::TypedefItem(..) => "Type Definition ",
2225+
clean::MacroItem(..) => "Macro ",
22332226
clean::ProcMacroItem(ref mac) => match mac.kind {
2234-
MacroKind::Bang => write!(fmt, "Macro ")?,
2235-
MacroKind::Attr => write!(fmt, "Attribute Macro ")?,
2236-
MacroKind::Derive => write!(fmt, "Derive Macro ")?,
2237-
}
2238-
clean::PrimitiveItem(..) => write!(fmt, "Primitive Type ")?,
2239-
clean::StaticItem(..) | clean::ForeignStaticItem(..) => write!(fmt, "Static ")?,
2240-
clean::ConstantItem(..) => write!(fmt, "Constant ")?,
2241-
clean::ForeignTypeItem => write!(fmt, "Foreign Type ")?,
2242-
clean::KeywordItem(..) => write!(fmt, "Keyword ")?,
2243-
clean::OpaqueTyItem(..) => write!(fmt, "Opaque Type ")?,
2244-
clean::TraitAliasItem(..) => write!(fmt, "Trait Alias ")?,
2227+
MacroKind::Bang => "Macro ",
2228+
MacroKind::Attr => "Attribute Macro ",
2229+
MacroKind::Derive => "Derive Macro ",
2230+
}
2231+
clean::PrimitiveItem(..) => "Primitive Type ",
2232+
clean::StaticItem(..) | clean::ForeignStaticItem(..) => "Static ",
2233+
clean::ConstantItem(..) => "Constant ",
2234+
clean::ForeignTypeItem => "Foreign Type ",
2235+
clean::KeywordItem(..) => "Keyword ",
2236+
clean::OpaqueTyItem(..) => "Opaque Type ",
2237+
clean::TraitAliasItem(..) => "Trait Alias ",
22452238
_ => {
22462239
// We don't generate pages for any other type.
22472240
unreachable!();
22482241
}
2249-
}
2250-
if !self.item.is_primitive() && !self.item.is_keyword() {
2251-
let cur = &self.cx.current;
2252-
let amt = if self.item.is_mod() { cur.len() - 1 } else { cur.len() };
2242+
};
2243+
buf.write_str(name);
2244+
if !item.is_primitive() && !item.is_keyword() {
2245+
let cur = &cx.current;
2246+
let amt = if item.is_mod() { cur.len() - 1 } else { cur.len() };
22532247
for (i, component) in cur.iter().enumerate().take(amt) {
2254-
write!(fmt, "<a href='{}index.html'>{}</a>::<wbr>",
2248+
write!(buf, "<a href='{}index.html'>{}</a>::<wbr>",
22552249
"../".repeat(cur.len() - i - 1),
2256-
component)?;
2250+
component);
22572251
}
22582252
}
2259-
write!(fmt, "<a class=\"{}\" href=''>{}</a>",
2260-
self.item.type_(), self.item.name.as_ref().unwrap())?;
2253+
write!(buf, "<a class=\"{}\" href=''>{}</a>",
2254+
item.type_(), item.name.as_ref().unwrap());
22612255

2262-
write!(fmt, "</span></h1>")?; // in-band
2256+
write!(buf, "</span></h1>"); // in-band
22632257

2264-
match self.item.inner {
2258+
buf.with_formatter(|fmt| {
2259+
match item.inner {
22652260
clean::ModuleItem(ref m) =>
2266-
item_module(fmt, self.cx, self.item, &m.items),
2261+
item_module(fmt, cx, item, &m.items),
22672262
clean::FunctionItem(ref f) | clean::ForeignFunctionItem(ref f) =>
2268-
item_function(fmt, self.cx, self.item, f),
2269-
clean::TraitItem(ref t) => item_trait(fmt, self.cx, self.item, t),
2270-
clean::StructItem(ref s) => item_struct(fmt, self.cx, self.item, s),
2271-
clean::UnionItem(ref s) => item_union(fmt, self.cx, self.item, s),
2272-
clean::EnumItem(ref e) => item_enum(fmt, self.cx, self.item, e),
2273-
clean::TypedefItem(ref t, _) => item_typedef(fmt, self.cx, self.item, t),
2274-
clean::MacroItem(ref m) => item_macro(fmt, self.cx, self.item, m),
2275-
clean::ProcMacroItem(ref m) => item_proc_macro(fmt, self.cx, self.item, m),
2276-
clean::PrimitiveItem(ref p) => item_primitive(fmt, self.cx, self.item, p),
2263+
item_function(fmt, cx, item, f),
2264+
clean::TraitItem(ref t) => item_trait(fmt, cx, item, t),
2265+
clean::StructItem(ref s) => item_struct(fmt, cx, item, s),
2266+
clean::UnionItem(ref s) => item_union(fmt, cx, item, s),
2267+
clean::EnumItem(ref e) => item_enum(fmt, cx, item, e),
2268+
clean::TypedefItem(ref t, _) => item_typedef(fmt, cx, item, t),
2269+
clean::MacroItem(ref m) => item_macro(fmt, cx, item, m),
2270+
clean::ProcMacroItem(ref m) => item_proc_macro(fmt, cx, item, m),
2271+
clean::PrimitiveItem(ref p) => item_primitive(fmt, cx, item, p),
22772272
clean::StaticItem(ref i) | clean::ForeignStaticItem(ref i) =>
2278-
item_static(fmt, self.cx, self.item, i),
2279-
clean::ConstantItem(ref c) => item_constant(fmt, self.cx, self.item, c),
2280-
clean::ForeignTypeItem => item_foreign_type(fmt, self.cx, self.item),
2281-
clean::KeywordItem(ref k) => item_keyword(fmt, self.cx, self.item, k),
2282-
clean::OpaqueTyItem(ref e, _) => item_opaque_ty(fmt, self.cx, self.item, e),
2283-
clean::TraitAliasItem(ref ta) => item_trait_alias(fmt, self.cx, self.item, ta),
2273+
item_static(fmt, cx, item, i),
2274+
clean::ConstantItem(ref c) => item_constant(fmt, cx, item, c),
2275+
clean::ForeignTypeItem => item_foreign_type(fmt, cx, item),
2276+
clean::KeywordItem(ref k) => item_keyword(fmt, cx, item, k),
2277+
clean::OpaqueTyItem(ref e, _) => item_opaque_ty(fmt, cx, item, e),
2278+
clean::TraitAliasItem(ref ta) => item_trait_alias(fmt, cx, item, ta),
22842279
_ => {
22852280
// We don't generate pages for any other type.
22862281
unreachable!();
22872282
}
22882283
}
2289-
}
2284+
})
22902285
}
22912286

22922287
fn item_path(ty: ItemType, name: &str) -> String {
@@ -4004,7 +3999,7 @@ fn render_impl(w: &mut fmt::Formatter<'_>, cx: &Context, i: &Impl, link: AssocIt
40043999
write!(w, "<a href='#{}' class='anchor'></a>", id)?;
40054000
let since = i.impl_item.stability.as_ref().map(|s| &s.since[..]);
40064001
render_stability_since_raw(w, since, outer_version)?;
4007-
if let Some(l) = (Item { item: &i.impl_item, cx: cx }).src_href() {
4002+
if let Some(l) = cx.src_href(&i.impl_item) {
40084003
write!(w, "<a class='srclink' href='{}' title='{}'>[src]</a>",
40094004
l, "goto source code")?;
40104005
}
@@ -4050,7 +4045,7 @@ fn render_impl(w: &mut fmt::Formatter<'_>, cx: &Context, i: &Impl, link: AssocIt
40504045
render_assoc_item(w, item, link.anchor(&id), ItemType::Impl)?;
40514046
write!(w, "</code>")?;
40524047
render_stability_since_raw(w, item.stable_since(), outer_version)?;
4053-
if let Some(l) = (Item { cx, item }).src_href() {
4048+
if let Some(l) = cx.src_href(item) {
40544049
write!(w, "<a class='srclink' href='{}' title='{}'>[src]</a>",
40554050
l, "goto source code")?;
40564051
}
@@ -4073,7 +4068,7 @@ fn render_impl(w: &mut fmt::Formatter<'_>, cx: &Context, i: &Impl, link: AssocIt
40734068
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id), "")?;
40744069
write!(w, "</code>")?;
40754070
render_stability_since_raw(w, item.stable_since(), outer_version)?;
4076-
if let Some(l) = (Item { cx, item }).src_href() {
4071+
if let Some(l) = cx.src_href(item) {
40774072
write!(w, "<a class='srclink' href='{}' title='{}'>[src]</a>",
40784073
l, "goto source code")?;
40794074
}

0 commit comments

Comments
 (0)