Skip to content

Commit 6ab8edb

Browse files
Pass Context as a &mut to allow to remove RefCell fields
1 parent 1ab9893 commit 6ab8edb

File tree

5 files changed

+128
-111
lines changed

5 files changed

+128
-111
lines changed

src/librustdoc/html/render/context.rs

+43-38
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ pub(crate) struct Context<'tcx> {
5555
pub(super) render_redirect_pages: bool,
5656
/// Tracks section IDs for `Deref` targets so they match in both the main
5757
/// body and the sidebar.
58-
pub(super) deref_id_map: RefCell<FxHashMap<DefId, String>>,
58+
pub(super) deref_id_map: FxHashMap<DefId, String>,
5959
/// The map used to ensure all generated 'id=' attributes are unique.
60-
pub(super) id_map: RefCell<IdMap>,
60+
pub(super) id_map: IdMap,
6161
/// Shared mutable state.
6262
///
6363
/// Issue for improving the situation: [#82381][]
@@ -72,7 +72,7 @@ pub(crate) struct Context<'tcx> {
7272

7373
// `Context` is cloned a lot, so we don't want the size to grow unexpectedly.
7474
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
75-
rustc_data_structures::static_assert_size!(Context<'_>, 144);
75+
rustc_data_structures::static_assert_size!(Context<'_>, 128);
7676

7777
/// Shared mutable state used in [`Context`] and elsewhere.
7878
pub(crate) struct SharedContext<'tcx> {
@@ -155,9 +155,8 @@ impl<'tcx> Context<'tcx> {
155155
self.shared.tcx.sess
156156
}
157157

158-
pub(super) fn derive_id(&self, id: String) -> String {
159-
let mut map = self.id_map.borrow_mut();
160-
map.derive(id)
158+
pub(super) fn derive_id(&mut self, id: String) -> String {
159+
self.id_map.derive(id)
161160
}
162161

163162
/// String representation of how to get back to the root path of the 'doc/'
@@ -166,7 +165,7 @@ impl<'tcx> Context<'tcx> {
166165
"../".repeat(self.current.len())
167166
}
168167

169-
fn render_item(&self, it: &clean::Item, is_module: bool) -> String {
168+
fn render_item(&mut self, it: &clean::Item, is_module: bool) -> String {
170169
let mut title = String::new();
171170
if !is_module {
172171
title.push_str(it.name.unwrap().as_str());
@@ -203,23 +202,26 @@ impl<'tcx> Context<'tcx> {
203202
};
204203

205204
if !self.render_redirect_pages {
205+
let clone_shared = Rc::clone(&self.shared);
206206
let page = layout::Page {
207207
css_class: tyname_s,
208208
root_path: &self.root_path(),
209-
static_root_path: self.shared.static_root_path.as_deref(),
209+
static_root_path: clone_shared.static_root_path.as_deref(),
210210
title: &title,
211211
description: &desc,
212212
keywords: &keywords,
213-
resource_suffix: &self.shared.resource_suffix,
213+
resource_suffix: &clone_shared.resource_suffix,
214214
extra_scripts: &[],
215215
static_extra_scripts: &[],
216216
};
217+
let mut page_buffer = Buffer::html();
218+
print_item(self, it, &mut page_buffer, &page);
217219
layout::render(
218-
&self.shared.layout,
220+
&clone_shared.layout,
219221
&page,
220222
|buf: &mut _| print_sidebar(self, it, buf),
221-
|buf: &mut _| print_item(self, it, buf, &page),
222-
&self.shared.style_files,
223+
move |buf: &mut Buffer| buf.push_buffer(page_buffer),
224+
&clone_shared.style_files,
223225
)
224226
} else {
225227
if let Some(&(ref names, ty)) = self.cache().paths.get(&it.item_id.expect_def_id()) {
@@ -512,8 +514,8 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
512514
current: Vec::new(),
513515
dst,
514516
render_redirect_pages: false,
515-
id_map: RefCell::new(id_map),
516-
deref_id_map: RefCell::new(FxHashMap::default()),
517+
id_map,
518+
deref_id_map: FxHashMap::default(),
517519
shared: Rc::new(scx),
518520
include_sources,
519521
};
@@ -528,7 +530,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
528530

529531
// Write shared runs within a flock; disable thread dispatching of IO temporarily.
530532
Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(true);
531-
write_shared(&cx, &krate, index, &md_opts)?;
533+
write_shared(&mut cx, &krate, index, &md_opts)?;
532534
Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(false);
533535
}
534536

@@ -540,8 +542,8 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
540542
current: self.current.clone(),
541543
dst: self.dst.clone(),
542544
render_redirect_pages: self.render_redirect_pages,
543-
deref_id_map: RefCell::new(FxHashMap::default()),
544-
id_map: RefCell::new(IdMap::new()),
545+
deref_id_map: FxHashMap::default(),
546+
id_map: IdMap::new(),
545547
shared: Rc::clone(&self.shared),
546548
include_sources: self.include_sources,
547549
}
@@ -557,31 +559,32 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
557559
if !root_path.ends_with('/') {
558560
root_path.push('/');
559561
}
562+
let shared = Rc::clone(&self.shared);
560563
let mut page = layout::Page {
561564
title: "List of all items in this crate",
562565
css_class: "mod",
563566
root_path: "../",
564-
static_root_path: self.shared.static_root_path.as_deref(),
567+
static_root_path: shared.static_root_path.as_deref(),
565568
description: "List of all items in this crate",
566569
keywords: BASIC_KEYWORDS,
567-
resource_suffix: &self.shared.resource_suffix,
570+
resource_suffix: &shared.resource_suffix,
568571
extra_scripts: &[],
569572
static_extra_scripts: &[],
570573
};
571-
let sidebar = if self.shared.cache.crate_version.is_some() {
574+
let sidebar = if shared.cache.crate_version.is_some() {
572575
format!("<h2 class=\"location\">Crate {}</h2>", crate_name)
573576
} else {
574577
String::new()
575578
};
576-
let all = self.shared.all.replace(AllTypes::new());
579+
let all = shared.all.replace(AllTypes::new());
577580
let v = layout::render(
578-
&self.shared.layout,
581+
&shared.layout,
579582
&page,
580583
sidebar,
581584
|buf: &mut Buffer| all.print(buf),
582-
&self.shared.style_files,
585+
&shared.style_files,
583586
);
584-
self.shared.fs.write(final_file, v)?;
587+
shared.fs.write(final_file, v)?;
585588

586589
// Generating settings page.
587590
page.title = "Rustdoc settings";
@@ -590,7 +593,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
590593

591594
let sidebar = "<h2 class=\"location\">Settings</h2><div class=\"sidebar-elems\"></div>";
592595
let v = layout::render(
593-
&self.shared.layout,
596+
&shared.layout,
594597
&page,
595598
sidebar,
596599
|buf: &mut Buffer| {
@@ -613,33 +616,36 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
613616
suffix = page.resource_suffix,
614617
)
615618
},
616-
&self.shared.style_files,
619+
&shared.style_files,
617620
);
618-
self.shared.fs.write(settings_file, v)?;
621+
shared.fs.write(settings_file, v)?;
619622

620-
if self.shared.layout.scrape_examples_extension {
623+
if shared.layout.scrape_examples_extension {
621624
page.title = "About scraped examples";
622625
page.description = "How the scraped examples feature works in Rustdoc";
623626
let v = layout::render(
624-
&self.shared.layout,
627+
&shared.layout,
625628
&page,
626629
"",
627-
scrape_examples_help(&*self.shared),
628-
&self.shared.style_files,
630+
scrape_examples_help(&*shared),
631+
&shared.style_files,
629632
);
630-
self.shared.fs.write(scrape_examples_help_file, v)?;
633+
shared.fs.write(scrape_examples_help_file, v)?;
631634
}
632635

633-
if let Some(ref redirections) = self.shared.redirections {
636+
if let Some(ref redirections) = shared.redirections {
634637
if !redirections.borrow().is_empty() {
635638
let redirect_map_path =
636639
self.dst.join(crate_name.as_str()).join("redirect-map.json");
637640
let paths = serde_json::to_string(&*redirections.borrow()).unwrap();
638-
self.shared.ensure_dir(&self.dst.join(crate_name.as_str()))?;
639-
self.shared.fs.write(redirect_map_path, paths)?;
641+
shared.ensure_dir(&self.dst.join(crate_name.as_str()))?;
642+
shared.fs.write(redirect_map_path, paths)?;
640643
}
641644
}
642645

646+
// No need for it anymore.
647+
drop(shared);
648+
643649
// Flush pending errors.
644650
Rc::get_mut(&mut self.shared).unwrap().fs.close();
645651
let nb_errors =
@@ -662,7 +668,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
662668
if !self.render_redirect_pages {
663669
self.render_redirect_pages = item.is_stripped();
664670
}
665-
let scx = &self.shared;
666671
let item_name = item.name.unwrap();
667672
self.dst.push(&*item_name.as_str());
668673
self.current.push(item_name);
@@ -674,7 +679,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
674679
if !buf.is_empty() {
675680
self.shared.ensure_dir(&self.dst)?;
676681
let joint_dst = self.dst.join("index.html");
677-
scx.fs.write(joint_dst, buf)?;
682+
self.shared.fs.write(joint_dst, buf)?;
678683
}
679684

680685
// Render sidebar-items.js used throughout this module.
@@ -684,7 +689,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
684689
let items = self.build_sidebar_items(module);
685690
let js_dst = self.dst.join(&format!("sidebar-items{}.js", self.shared.resource_suffix));
686691
let v = format!("initSidebarItems({});", serde_json::to_string(&items).unwrap());
687-
scx.fs.write(js_dst, v)?;
692+
self.shared.fs.write(js_dst, v)?;
688693
}
689694
Ok(())
690695
}

0 commit comments

Comments
 (0)