Skip to content

Commit bea81a3

Browse files
committed
rustdoc: Move most shared fields to SharedContext
...and remove `Rc`s for the moved fields. The only shared one that I didn't move was `cache`; see the doc-comment I added to `cache` for details.
1 parent d2b38d6 commit bea81a3

File tree

1 file changed

+35
-25
lines changed
  • src/librustdoc/html/render

1 file changed

+35
-25
lines changed

src/librustdoc/html/render/mod.rs

+35-25
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,16 @@ crate struct Context<'tcx> {
111111
/// real location of an item. This is used to allow external links to
112112
/// publicly reused items to redirect to the right location.
113113
crate render_redirect_pages: bool,
114-
/// The map used to ensure all generated 'id=' attributes are unique.
115-
id_map: Rc<RefCell<IdMap>>,
116-
/// Tracks section IDs for `Deref` targets so they match in both the main
117-
/// body and the sidebar.
118-
deref_id_map: Rc<RefCell<FxHashMap<DefId, String>>>,
119114
crate shared: Arc<SharedContext<'tcx>>,
120-
all: Rc<RefCell<AllTypes>>,
121-
/// Storage for the errors produced while generating documentation so they
122-
/// can be printed together at the end.
123-
crate errors: Rc<Receiver<String>>,
115+
/// The [`Cache`] used during rendering.
116+
///
117+
/// Ideally the cache would be in [`SharedContext`], but it's mutated
118+
/// between when the `SharedContext` is created and when `Context`
119+
/// is created, so more refactoring would be needed.
120+
///
121+
/// It's immutable once in `Context`, so it's not as bad that it's not in
122+
/// `SharedContext`.
123+
// FIXME: move `cache` to `SharedContext`
124124
crate cache: Rc<Cache>,
125125
}
126126

@@ -163,6 +163,15 @@ crate struct SharedContext<'tcx> {
163163
crate edition: Edition,
164164
crate codes: ErrorCodes,
165165
playground: Option<markdown::Playground>,
166+
/// The map used to ensure all generated 'id=' attributes are unique.
167+
id_map: RefCell<IdMap>,
168+
/// Tracks section IDs for `Deref` targets so they match in both the main
169+
/// body and the sidebar.
170+
deref_id_map: RefCell<FxHashMap<DefId, String>>,
171+
all: RefCell<AllTypes>,
172+
/// Storage for the errors produced while generating documentation so they
173+
/// can be printed together at the end.
174+
crate errors: Receiver<String>,
166175
}
167176

168177
impl<'tcx> Context<'tcx> {
@@ -478,6 +487,10 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
478487
edition,
479488
codes: ErrorCodes::from(unstable_features.is_nightly_build()),
480489
playground,
490+
id_map: RefCell::new(id_map),
491+
deref_id_map: RefCell::new(FxHashMap::default()),
492+
all: RefCell::new(AllTypes::new()),
493+
errors: receiver,
481494
};
482495

483496
// Add the default themes to the `Vec` of stylepaths
@@ -504,11 +517,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
504517
current: Vec::new(),
505518
dst,
506519
render_redirect_pages: false,
507-
id_map: Rc::new(RefCell::new(id_map)),
508-
deref_id_map: Rc::new(RefCell::new(FxHashMap::default())),
509520
shared: Arc::new(scx),
510-
all: Rc::new(RefCell::new(AllTypes::new())),
511-
errors: Rc::new(receiver),
512521
cache: Rc::new(cache),
513522
};
514523

@@ -558,7 +567,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
558567
} else {
559568
String::new()
560569
};
561-
let all = self.all.replace(AllTypes::new());
570+
let all = self.shared.all.replace(AllTypes::new());
562571
let v = layout::render(
563572
&self.shared.layout,
564573
&page,
@@ -591,7 +600,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
591600

592601
// Flush pending errors.
593602
Arc::get_mut(&mut self.shared).unwrap().fs.close();
594-
let nb_errors = self.errors.iter().map(|err| diag.struct_err(&err).emit()).count();
603+
let nb_errors = self.shared.errors.iter().map(|err| diag.struct_err(&err).emit()).count();
595604
if nb_errors > 0 {
596605
Err(Error::new(io::Error::new(io::ErrorKind::Other, "I/O error"), ""))
597606
} else {
@@ -670,7 +679,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
670679
self.shared.fs.write(&joint_dst, buf.as_bytes())?;
671680

672681
if !self.render_redirect_pages {
673-
self.all.borrow_mut().append(full_path(self, &item), &item_type);
682+
self.shared.all.borrow_mut().append(full_path(self, &item), &item_type);
674683
}
675684
// If the item is a macro, redirect from the old macro URL (with !)
676685
// to the new one (without).
@@ -1517,7 +1526,7 @@ fn settings(root_path: &str, suffix: &str, themes: &[StylePath]) -> Result<Strin
15171526

15181527
impl Context<'_> {
15191528
fn derive_id(&self, id: String) -> String {
1520-
let mut map = self.id_map.borrow_mut();
1529+
let mut map = self.shared.id_map.borrow_mut();
15211530
map.derive(id)
15221531
}
15231532

@@ -1572,8 +1581,8 @@ impl Context<'_> {
15721581
};
15731582

15741583
{
1575-
self.id_map.borrow_mut().reset();
1576-
self.id_map.borrow_mut().populate(&INITIAL_IDS);
1584+
self.shared.id_map.borrow_mut().reset();
1585+
self.shared.id_map.borrow_mut().populate(&INITIAL_IDS);
15771586
}
15781587

15791588
if !self.render_redirect_pages {
@@ -1841,7 +1850,7 @@ fn render_markdown(
18411850
prefix: &str,
18421851
is_hidden: bool,
18431852
) {
1844-
let mut ids = cx.id_map.borrow_mut();
1853+
let mut ids = cx.shared.id_map.borrow_mut();
18451854
write!(
18461855
w,
18471856
"<div class=\"docblock{}\">{}{}</div>",
@@ -2319,7 +2328,7 @@ fn short_item_info(
23192328

23202329
if let Some(note) = note {
23212330
let note = note.as_str();
2322-
let mut ids = cx.id_map.borrow_mut();
2331+
let mut ids = cx.shared.id_map.borrow_mut();
23232332
let html = MarkdownHtml(
23242333
&note,
23252334
&mut ids,
@@ -2358,7 +2367,7 @@ fn short_item_info(
23582367
message.push_str(&format!(" ({})", feature));
23592368

23602369
if let Some(unstable_reason) = reason {
2361-
let mut ids = cx.id_map.borrow_mut();
2370+
let mut ids = cx.shared.id_map.borrow_mut();
23622371
message = format!(
23632372
"<details><summary>{}</summary>{}</details>",
23642373
message,
@@ -3513,7 +3522,8 @@ fn render_assoc_items(
35133522
type_.print(cx.cache())
35143523
)));
35153524
debug!("Adding {} to deref id map", type_.print(cx.cache()));
3516-
cx.deref_id_map
3525+
cx.shared
3526+
.deref_id_map
35173527
.borrow_mut()
35183528
.insert(type_.def_id_full(cx.cache()).unwrap(), id.clone());
35193529
write!(
@@ -3819,7 +3829,7 @@ fn render_impl(
38193829
}
38203830

38213831
if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) {
3822-
let mut ids = cx.id_map.borrow_mut();
3832+
let mut ids = cx.shared.id_map.borrow_mut();
38233833
write!(
38243834
w,
38253835
"<div class=\"docblock\">{}</div>",
@@ -4448,7 +4458,7 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V
44484458
.flat_map(|i| get_methods(i.inner_impl(), true, &mut used_links, deref_mut, c))
44494459
.collect::<Vec<_>>();
44504460
if !ret.is_empty() {
4451-
let deref_id_map = cx.deref_id_map.borrow();
4461+
let deref_id_map = cx.shared.deref_id_map.borrow();
44524462
let id = deref_id_map
44534463
.get(&real_target.def_id_full(cx.cache()).unwrap())
44544464
.expect("Deref section without derived id");

0 commit comments

Comments
 (0)