Skip to content

Commit 2e5837a

Browse files
authored
Rollup merge of rust-lang#36872 - frewsxcv:rustdoc, r=GuillaumeGomez
A couple refactorings in librustdoc. None
2 parents 3821811 + 7732e62 commit 2e5837a

File tree

2 files changed

+47
-49
lines changed

2 files changed

+47
-49
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -283,34 +283,34 @@ impl Item {
283283
}
284284
}
285285
pub fn is_mod(&self) -> bool {
286-
ItemType::from(self) == ItemType::Module
286+
self.type_() == ItemType::Module
287287
}
288288
pub fn is_trait(&self) -> bool {
289-
ItemType::from(self) == ItemType::Trait
289+
self.type_() == ItemType::Trait
290290
}
291291
pub fn is_struct(&self) -> bool {
292-
ItemType::from(self) == ItemType::Struct
292+
self.type_() == ItemType::Struct
293293
}
294294
pub fn is_enum(&self) -> bool {
295-
ItemType::from(self) == ItemType::Module
295+
self.type_() == ItemType::Module
296296
}
297297
pub fn is_fn(&self) -> bool {
298-
ItemType::from(self) == ItemType::Function
298+
self.type_() == ItemType::Function
299299
}
300300
pub fn is_associated_type(&self) -> bool {
301-
ItemType::from(self) == ItemType::AssociatedType
301+
self.type_() == ItemType::AssociatedType
302302
}
303303
pub fn is_associated_const(&self) -> bool {
304-
ItemType::from(self) == ItemType::AssociatedConst
304+
self.type_() == ItemType::AssociatedConst
305305
}
306306
pub fn is_method(&self) -> bool {
307-
ItemType::from(self) == ItemType::Method
307+
self.type_() == ItemType::Method
308308
}
309309
pub fn is_ty_method(&self) -> bool {
310-
ItemType::from(self) == ItemType::TyMethod
310+
self.type_() == ItemType::TyMethod
311311
}
312312
pub fn is_primitive(&self) -> bool {
313-
ItemType::from(self) == ItemType::Primitive
313+
self.type_() == ItemType::Primitive
314314
}
315315
pub fn is_stripped(&self) -> bool {
316316
match self.inner { StrippedItem(..) => true, _ => false }
@@ -342,6 +342,11 @@ impl Item {
342342
pub fn stable_since(&self) -> Option<&str> {
343343
self.stability.as_ref().map(|s| &s.since[..])
344344
}
345+
346+
/// Returns a documentation-level item type from the item.
347+
pub fn type_(&self) -> ItemType {
348+
ItemType::from(self)
349+
}
345350
}
346351

347352
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]

src/librustdoc/html/render.rs

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ pub struct Context {
8989
/// Current hierarchy of components leading down to what's currently being
9090
/// rendered
9191
pub current: Vec<String>,
92-
/// String representation of how to get back to the root path of the 'doc/'
93-
/// folder in terms of a relative URL.
94-
pub root_path: String,
9592
/// The current destination folder of where HTML artifacts should be placed.
9693
/// This changes as the context descends into the module hierarchy.
9794
pub dst: PathBuf,
@@ -496,7 +493,6 @@ pub fn run(mut krate: clean::Crate,
496493
krate = render_sources(&dst, &mut scx, krate)?;
497494
let cx = Context {
498495
current: Vec::new(),
499-
root_path: String::new(),
500496
dst: dst,
501497
render_redirect_pages: false,
502498
shared: Arc::new(scx),
@@ -591,7 +587,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
591587
for &(did, ref item) in orphan_impl_items {
592588
if let Some(&(ref fqp, _)) = paths.get(&did) {
593589
search_index.push(IndexItem {
594-
ty: item_type(item),
590+
ty: item.type_(),
595591
name: item.name.clone().unwrap(),
596592
path: fqp[..fqp.len() - 1].join("::"),
597593
desc: Escape(&shorter(item.doc_value())).to_string(),
@@ -835,11 +831,6 @@ fn mkdir(path: &Path) -> io::Result<()> {
835831
}
836832
}
837833

838-
/// Returns a documentation-level item type from the item.
839-
fn item_type(item: &clean::Item) -> ItemType {
840-
ItemType::from(item)
841-
}
842-
843834
/// Takes a path to a source file and cleans the path to it. This canonicalizes
844835
/// things like ".." to components which preserve the "top down" hierarchy of a
845836
/// static HTML tree. Each component in the cleaned path will be passed as an
@@ -1075,7 +1066,7 @@ impl DocFolder for Cache {
10751066
// inserted later on when serializing the search-index.
10761067
if item.def_id.index != CRATE_DEF_INDEX {
10771068
self.search_index.push(IndexItem {
1078-
ty: item_type(&item),
1069+
ty: item.type_(),
10791070
name: s.to_string(),
10801071
path: path.join("::").to_string(),
10811072
desc: Escape(&shorter(item.doc_value())).to_string(),
@@ -1122,7 +1113,7 @@ impl DocFolder for Cache {
11221113
self.access_levels.is_public(item.def_id)
11231114
{
11241115
self.paths.insert(item.def_id,
1125-
(self.stack.clone(), item_type(&item)));
1116+
(self.stack.clone(), item.type_()));
11261117
}
11271118
}
11281119
// link variants to their parent enum because pages aren't emitted
@@ -1135,7 +1126,7 @@ impl DocFolder for Cache {
11351126

11361127
clean::PrimitiveItem(..) if item.visibility.is_some() => {
11371128
self.paths.insert(item.def_id, (self.stack.clone(),
1138-
item_type(&item)));
1129+
item.type_()));
11391130
}
11401131

11411132
_ => {}
@@ -1230,6 +1221,12 @@ impl<'a> Cache {
12301221
}
12311222

12321223
impl Context {
1224+
/// String representation of how to get back to the root path of the 'doc/'
1225+
/// folder in terms of a relative URL.
1226+
fn root_path(&self) -> String {
1227+
repeat("../").take(self.current.len()).collect::<String>()
1228+
}
1229+
12331230
/// Recurse in the directory structure and change the "root path" to make
12341231
/// sure it always points to the top (relatively)
12351232
fn recurse<T, F>(&mut self, s: String, f: F) -> T where
@@ -1240,7 +1237,6 @@ impl Context {
12401237
}
12411238
let prev = self.dst.clone();
12421239
self.dst.push(&s);
1243-
self.root_path.push_str("../");
12441240
self.current.push(s);
12451241

12461242
info!("Recursing into {}", self.dst.display());
@@ -1251,8 +1247,6 @@ impl Context {
12511247

12521248
// Go back to where we were at
12531249
self.dst = prev;
1254-
let len = self.root_path.len();
1255-
self.root_path.truncate(len - 3);
12561250
self.current.pop().unwrap();
12571251

12581252
return ret;
@@ -1304,7 +1298,7 @@ impl Context {
13041298
title.push_str(it.name.as_ref().unwrap());
13051299
}
13061300
title.push_str(" - Rust");
1307-
let tyname = item_type(it).css_class();
1301+
let tyname = it.type_().css_class();
13081302
let desc = if it.is_crate() {
13091303
format!("API documentation for the Rust `{}` crate.",
13101304
self.shared.layout.krate)
@@ -1315,7 +1309,7 @@ impl Context {
13151309
let keywords = make_item_keywords(it);
13161310
let page = layout::Page {
13171311
css_class: tyname,
1318-
root_path: &self.root_path,
1312+
root_path: &self.root_path(),
13191313
title: &title,
13201314
description: &desc,
13211315
keywords: &keywords,
@@ -1329,8 +1323,7 @@ impl Context {
13291323
&Item{ cx: self, item: it },
13301324
self.shared.css_file_extension.is_some())?;
13311325
} else {
1332-
let mut url = repeat("../").take(self.current.len())
1333-
.collect::<String>();
1326+
let mut url = self.root_path();
13341327
if let Some(&(ref names, ty)) = cache().paths.get(&it.def_id) {
13351328
for name in &names[..names.len() - 1] {
13361329
url.push_str(name);
@@ -1407,7 +1400,7 @@ impl Context {
14071400
// buf will be empty if the item is stripped and there is no redirect for it
14081401
if !buf.is_empty() {
14091402
let name = item.name.as_ref().unwrap();
1410-
let item_type = item_type(&item);
1403+
let item_type = item.type_();
14111404
let file_name = &item_path(item_type, name);
14121405
let joint_dst = self.dst.join(file_name);
14131406
try_err!(fs::create_dir_all(&self.dst), &self.dst);
@@ -1444,7 +1437,7 @@ impl Context {
14441437
for item in &m.items {
14451438
if maybe_ignore_item(item) { continue }
14461439

1447-
let short = item_type(item).css_class();
1440+
let short = item.type_().css_class();
14481441
let myname = match item.name {
14491442
None => continue,
14501443
Some(ref s) => s.to_string(),
@@ -1492,7 +1485,7 @@ impl<'a> Item<'a> {
14921485
}).map(|l| &l.1);
14931486
let root = match root {
14941487
Some(&Remote(ref s)) => s.to_string(),
1495-
Some(&Local) => self.cx.root_path.clone(),
1488+
Some(&Local) => self.cx.root_path(),
14961489
None | Some(&Unknown) => return None,
14971490
};
14981491
Some(format!("{root}/{krate}/macro.{name}.html?gotomacrosrc=1",
@@ -1507,7 +1500,7 @@ impl<'a> Item<'a> {
15071500
let path = PathBuf::from(&self.item.source.filename);
15081501
self.cx.shared.local_sources.get(&path).map(|path| {
15091502
format!("{root}src/{krate}/{path}#{href}",
1510-
root = self.cx.root_path,
1503+
root = self.cx.root_path(),
15111504
krate = self.cx.shared.layout.krate,
15121505
path = path,
15131506
href = href)
@@ -1531,7 +1524,7 @@ impl<'a> Item<'a> {
15311524
};
15321525
let mut path = match cache.extern_locations.get(&self.item.def_id.krate) {
15331526
Some(&(_, Remote(ref s))) => s.to_string(),
1534-
Some(&(_, Local)) => self.cx.root_path.clone(),
1527+
Some(&(_, Local)) => self.cx.root_path(),
15351528
Some(&(_, Unknown)) => return None,
15361529
None => return None,
15371530
};
@@ -1541,7 +1534,7 @@ impl<'a> Item<'a> {
15411534
}
15421535
Some(format!("{path}{file}?gotosrc={goto}",
15431536
path = path,
1544-
file = item_path(item_type(self.item), external_path.last().unwrap()),
1537+
file = item_path(self.item.type_(), external_path.last().unwrap()),
15451538
goto = self.item.def_id.index.as_usize()))
15461539
}
15471540
}
@@ -1586,7 +1579,7 @@ impl<'a> fmt::Display for Item<'a> {
15861579
}
15871580
}
15881581
write!(fmt, "<a class='{}' href=''>{}</a>",
1589-
item_type(self.item), self.item.name.as_ref().unwrap())?;
1582+
self.item.type_(), self.item.name.as_ref().unwrap())?;
15901583

15911584
write!(fmt, "</span>")?; // in-band
15921585
write!(fmt, "<span class='out-of-band'>")?;
@@ -1739,8 +1732,8 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
17391732
}
17401733

17411734
fn cmp(i1: &clean::Item, i2: &clean::Item, idx1: usize, idx2: usize) -> Ordering {
1742-
let ty1 = item_type(i1);
1743-
let ty2 = item_type(i2);
1735+
let ty1 = i1.type_();
1736+
let ty2 = i2.type_();
17441737
if ty1 != ty2 {
17451738
return (reorder(ty1), idx1).cmp(&(reorder(ty2), idx2))
17461739
}
@@ -1764,7 +1757,7 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
17641757
continue;
17651758
}
17661759

1767-
let myty = Some(item_type(myitem));
1760+
let myty = Some(myitem.type_());
17681761
if curty == Some(ItemType::ExternCrate) && myty == Some(ItemType::Import) {
17691762
// Put `extern crate` and `use` re-exports in the same section.
17701763
curty = myty;
@@ -1851,9 +1844,9 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
18511844
name = *myitem.name.as_ref().unwrap(),
18521845
stab_docs = stab_docs,
18531846
docs = shorter(Some(&Markdown(doc_value).to_string())),
1854-
class = item_type(myitem),
1847+
class = myitem.type_(),
18551848
stab = myitem.stability_class(),
1856-
href = item_path(item_type(myitem), myitem.name.as_ref().unwrap()),
1849+
href = item_path(myitem.type_(), myitem.name.as_ref().unwrap()),
18571850
title = full_path(cx, myitem))?;
18581851
}
18591852
}
@@ -2059,7 +2052,7 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
20592052
fn trait_item(w: &mut fmt::Formatter, cx: &Context, m: &clean::Item, t: &clean::Item)
20602053
-> fmt::Result {
20612054
let name = m.name.as_ref().unwrap();
2062-
let item_type = item_type(m);
2055+
let item_type = m.type_();
20632056
let id = derive_id(format!("{}.{}", item_type, name));
20642057
let ns_id = derive_id(format!("{}.{}", name, item_type.name_space()));
20652058
write!(w, "<h3 id='{id}' class='method stab {stab}'>\
@@ -2145,7 +2138,7 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
21452138
let (ref path, _) = cache.external_paths[&it.def_id];
21462139
path[..path.len() - 1].join("/")
21472140
},
2148-
ty = item_type(it).css_class(),
2141+
ty = it.type_().css_class(),
21492142
name = *it.name.as_ref().unwrap())?;
21502143
Ok(())
21512144
}
@@ -2154,7 +2147,7 @@ fn naive_assoc_href(it: &clean::Item, link: AssocItemLink) -> String {
21542147
use html::item_type::ItemType::*;
21552148

21562149
let name = it.name.as_ref().unwrap();
2157-
let ty = match item_type(it) {
2150+
let ty = match it.type_() {
21582151
Typedef | AssociatedType => AssociatedType,
21592152
s@_ => s,
21602153
};
@@ -2232,7 +2225,7 @@ fn render_assoc_item(w: &mut fmt::Formatter,
22322225
link: AssocItemLink)
22332226
-> fmt::Result {
22342227
let name = meth.name.as_ref().unwrap();
2235-
let anchor = format!("#{}.{}", item_type(meth), name);
2228+
let anchor = format!("#{}.{}", meth.type_(), name);
22362229
let href = match link {
22372230
AssocItemLink::Anchor(Some(ref id)) => format!("#{}", id),
22382231
AssocItemLink::Anchor(None) => anchor,
@@ -2740,7 +2733,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
27402733
link: AssocItemLink, render_mode: RenderMode,
27412734
is_default_item: bool, outer_version: Option<&str>,
27422735
trait_: Option<&clean::Trait>) -> fmt::Result {
2743-
let item_type = item_type(item);
2736+
let item_type = item.type_();
27442737
let name = item.name.as_ref().unwrap();
27452738

27462739
let render_method_item: bool = match render_mode {
@@ -2918,7 +2911,7 @@ impl<'a> fmt::Display for Sidebar<'a> {
29182911
write!(fmt, "::<wbr>")?;
29192912
}
29202913
write!(fmt, "<a href='{}index.html'>{}</a>",
2921-
&cx.root_path[..(cx.current.len() - i - 1) * 3],
2914+
&cx.root_path()[..(cx.current.len() - i - 1) * 3],
29222915
*name)?;
29232916
}
29242917
write!(fmt, "</p>")?;
@@ -2932,7 +2925,7 @@ impl<'a> fmt::Display for Sidebar<'a> {
29322925
relpath: '{path}'\
29332926
}};</script>",
29342927
name = it.name.as_ref().map(|x| &x[..]).unwrap_or(""),
2935-
ty = item_type(it).css_class(),
2928+
ty = it.type_().css_class(),
29362929
path = relpath)?;
29372930
if parentlen == 0 {
29382931
// there is no sidebar-items.js beyond the crate root path

0 commit comments

Comments
 (0)