Skip to content

Commit 7732e62

Browse files
committed
Simplify logic around Context's root_path.
Was previously cached and maintained in the `Context`, which to me seems overkill.
1 parent c66c453 commit 7732e62

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/librustdoc/html/render.rs

+12-14
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),
@@ -1225,6 +1221,12 @@ impl<'a> Cache {
12251221
}
12261222

12271223
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+
12281230
/// Recurse in the directory structure and change the "root path" to make
12291231
/// sure it always points to the top (relatively)
12301232
fn recurse<T, F>(&mut self, s: String, f: F) -> T where
@@ -1235,7 +1237,6 @@ impl Context {
12351237
}
12361238
let prev = self.dst.clone();
12371239
self.dst.push(&s);
1238-
self.root_path.push_str("../");
12391240
self.current.push(s);
12401241

12411242
info!("Recursing into {}", self.dst.display());
@@ -1246,8 +1247,6 @@ impl Context {
12461247

12471248
// Go back to where we were at
12481249
self.dst = prev;
1249-
let len = self.root_path.len();
1250-
self.root_path.truncate(len - 3);
12511250
self.current.pop().unwrap();
12521251

12531252
return ret;
@@ -1310,7 +1309,7 @@ impl Context {
13101309
let keywords = make_item_keywords(it);
13111310
let page = layout::Page {
13121311
css_class: tyname,
1313-
root_path: &self.root_path,
1312+
root_path: &self.root_path(),
13141313
title: &title,
13151314
description: &desc,
13161315
keywords: &keywords,
@@ -1324,8 +1323,7 @@ impl Context {
13241323
&Item{ cx: self, item: it },
13251324
self.shared.css_file_extension.is_some())?;
13261325
} else {
1327-
let mut url = repeat("../").take(self.current.len())
1328-
.collect::<String>();
1326+
let mut url = self.root_path();
13291327
if let Some(&(ref names, ty)) = cache().paths.get(&it.def_id) {
13301328
for name in &names[..names.len() - 1] {
13311329
url.push_str(name);
@@ -1487,7 +1485,7 @@ impl<'a> Item<'a> {
14871485
}).map(|l| &l.1);
14881486
let root = match root {
14891487
Some(&Remote(ref s)) => s.to_string(),
1490-
Some(&Local) => self.cx.root_path.clone(),
1488+
Some(&Local) => self.cx.root_path(),
14911489
None | Some(&Unknown) => return None,
14921490
};
14931491
Some(format!("{root}/{krate}/macro.{name}.html?gotomacrosrc=1",
@@ -1502,7 +1500,7 @@ impl<'a> Item<'a> {
15021500
let path = PathBuf::from(&self.item.source.filename);
15031501
self.cx.shared.local_sources.get(&path).map(|path| {
15041502
format!("{root}src/{krate}/{path}#{href}",
1505-
root = self.cx.root_path,
1503+
root = self.cx.root_path(),
15061504
krate = self.cx.shared.layout.krate,
15071505
path = path,
15081506
href = href)
@@ -1526,7 +1524,7 @@ impl<'a> Item<'a> {
15261524
};
15271525
let mut path = match cache.extern_locations.get(&self.item.def_id.krate) {
15281526
Some(&(_, Remote(ref s))) => s.to_string(),
1529-
Some(&(_, Local)) => self.cx.root_path.clone(),
1527+
Some(&(_, Local)) => self.cx.root_path(),
15301528
Some(&(_, Unknown)) => return None,
15311529
None => return None,
15321530
};
@@ -2913,7 +2911,7 @@ impl<'a> fmt::Display for Sidebar<'a> {
29132911
write!(fmt, "::<wbr>")?;
29142912
}
29152913
write!(fmt, "<a href='{}index.html'>{}</a>",
2916-
&cx.root_path[..(cx.current.len() - i - 1) * 3],
2914+
&cx.root_path()[..(cx.current.len() - i - 1) * 3],
29172915
*name)?;
29182916
}
29192917
write!(fmt, "</p>")?;

0 commit comments

Comments
 (0)