Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass Context as &mut in rustdoc #91468

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@ use crate::html::render::cache::ExternalLocation;
use crate::html::render::Context;

crate trait Print {
fn print(self, buffer: &mut Buffer);
fn print(self, buffer: &mut Buffer, cx: &mut Context<'_>);
}

impl<F> Print for F
where
F: FnOnce(&mut Buffer),
F: FnOnce(&mut Buffer, &mut Context<'_>),
{
fn print(self, buffer: &mut Buffer) {
(self)(buffer)
fn print(self, buffer: &mut Buffer, cx: &mut Context<'_>) {
(self)(buffer, cx)
}
}

impl Print for String {
fn print(self, buffer: &mut Buffer) {
fn print(self, buffer: &mut Buffer, _: &mut Context<'_>) {
buffer.write_str(&self);
}
}

impl Print for &'_ str {
fn print(self, buffer: &mut Buffer) {
fn print(self, buffer: &mut Buffer, _: &mut Context<'_>) {
buffer.write_str(self);
}
}
Expand Down Expand Up @@ -106,8 +106,8 @@ impl Buffer {
self.buffer.write_fmt(v).unwrap();
}

crate fn to_display<T: Print>(mut self, t: T) -> String {
t.print(&mut self);
crate fn to_display<T: Print>(mut self, t: T, cx: &mut Context<'_>) -> String {
t.print(&mut self, cx);
self.into_inner()
}

Expand Down
20 changes: 11 additions & 9 deletions src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::html::render::{ensure_trailing_slash, StylePath};

use serde::Serialize;

use super::render::{Context, SharedContext};

#[derive(Clone, Serialize)]
crate struct Layout {
crate logo: String,
Expand Down Expand Up @@ -58,36 +60,36 @@ struct PageLayout<'a> {
}

crate fn render<T: Print, S: Print>(
templates: &tera::Tera,
layout: &Layout,
cx: &mut Context<'_>,
shared: &SharedContext<'_>,
page: &Page<'_>,
sidebar: S,
t: T,
style_files: &[StylePath],
) -> String {
let static_root_path = page.get_static_root_path();
let krate_with_trailing_slash = ensure_trailing_slash(&layout.krate).to_string();
let mut themes: Vec<String> = style_files
let krate_with_trailing_slash = ensure_trailing_slash(&shared.layout.krate).to_string();
let mut themes: Vec<String> = shared
.style_files
.iter()
.map(StylePath::basename)
.collect::<Result<_, Error>>()
.unwrap_or_default();
themes.sort();
let rustdoc_version = rustc_interface::util::version_str().unwrap_or("unknown version");
let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
let sidebar = Buffer::html().to_display(sidebar);
let content = Buffer::html().to_display(t, cx); // Note: This must happen before making the sidebar.
let sidebar = Buffer::html().to_display(sidebar, cx);
let teractx = tera::Context::from_serialize(PageLayout {
static_root_path,
page,
layout,
layout: &shared.layout,
themes,
sidebar,
content,
krate_with_trailing_slash,
rustdoc_version,
})
.unwrap();
templates.render("page.html", &teractx).unwrap()
shared.templates.render("page.html", &teractx).unwrap()
}

crate fn redirect(url: &str) -> String {
Expand Down
61 changes: 30 additions & 31 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ crate struct Context<'tcx> {
pub(super) render_redirect_pages: bool,
/// Tracks section IDs for `Deref` targets so they match in both the main
/// body and the sidebar.
pub(super) deref_id_map: RefCell<FxHashMap<DefId, String>>,
pub(super) deref_id_map: FxHashMap<DefId, String>,
/// The map used to ensure all generated 'id=' attributes are unique.
pub(super) id_map: RefCell<IdMap>,
pub(super) id_map: IdMap,
/// Shared mutable state.
///
/// Issue for improving the situation: [#82381][]
Expand All @@ -73,7 +73,7 @@ crate struct Context<'tcx> {

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

/// Shared mutable state used in [`Context`] and elsewhere.
crate struct SharedContext<'tcx> {
Expand Down Expand Up @@ -166,9 +166,8 @@ impl<'tcx> Context<'tcx> {
self.shared.tcx.sess
}

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

/// String representation of how to get back to the root path of the 'doc/'
Expand All @@ -177,7 +176,7 @@ impl<'tcx> Context<'tcx> {
"../".repeat(self.current.len())
}

fn render_item(&self, it: &clean::Item, is_module: bool) -> String {
fn render_item(&mut self, it: &clean::Item, is_module: bool) -> String {
let mut title = String::new();
if !is_module {
title.push_str(&it.name.unwrap().as_str());
Expand Down Expand Up @@ -212,26 +211,28 @@ impl<'tcx> Context<'tcx> {
} else {
tyname.as_str()
};
let shared_clone = self.shared.clone();
let page = layout::Page {
css_class: tyname_s,
root_path: &self.root_path(),
static_root_path: self.shared.static_root_path.as_deref(),
static_root_path: (&*shared_clone).static_root_path.as_deref(),
title: &title,
description: &desc,
keywords: &keywords,
resource_suffix: &self.shared.resource_suffix,
resource_suffix: &*shared_clone.resource_suffix,
extra_scripts: &[],
static_extra_scripts: &[],
};

let shared_templates = &self.shared.clone().templates;
if !self.render_redirect_pages {
layout::render(
&self.shared.templates,
&self.shared.layout,
self,
&(&*self).shared,
&page,
|buf: &mut _| print_sidebar(self, it, buf),
|buf: &mut _| print_item(self, &self.shared.templates, it, buf, &page),
&self.shared.style_files,
|buf: &mut _, cx: &mut Context<'_>| print_sidebar(cx, it, buf),
|buf: &mut _, cx: &mut Context<'_>| {
print_item(cx, shared_templates, it, buf, &page)
},
)
} else {
if let Some(&(ref names, ty)) = self.cache().paths.get(&it.def_id.expect_def_id()) {
Expand Down Expand Up @@ -511,12 +512,12 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
let dst = output;
scx.ensure_dir(&dst)?;

let mut cx = Context {
let cx = &mut Context {
current: Vec::new(),
dst,
render_redirect_pages: false,
id_map: RefCell::new(id_map),
deref_id_map: RefCell::new(FxHashMap::default()),
id_map,
deref_id_map: FxHashMap::default(),
shared: Rc::new(scx),
include_sources,
};
Expand All @@ -530,18 +531,18 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {

// Write shared runs within a flock; disable thread dispatching of IO temporarily.
Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(true);
write_shared(&cx, &krate, index, &md_opts)?;
write_shared(cx, &krate, index, &md_opts)?;
Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(false);
Ok((cx, krate))
Ok((*cx, krate))
}

fn make_child_renderer(&self) -> Self {
Self {
current: self.current.clone(),
dst: self.dst.clone(),
render_redirect_pages: self.render_redirect_pages,
deref_id_map: RefCell::new(FxHashMap::default()),
id_map: RefCell::new(IdMap::new()),
deref_id_map: FxHashMap::default(),
id_map: IdMap::new(),
shared: Rc::clone(&self.shared),
include_sources: self.include_sources,
}
Expand Down Expand Up @@ -582,12 +583,11 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
};
let all = self.shared.all.replace(AllTypes::new());
let v = layout::render(
&self.shared.templates,
&self.shared.layout,
self,
&self.shared,
&page,
sidebar,
|buf: &mut Buffer| all.print(buf),
&self.shared.style_files,
|buf: &mut Buffer, _: &mut Context<'_>| all.print(buf),
);
self.shared.fs.write(final_file, v)?;

Expand All @@ -604,16 +604,15 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
.map(StylePath::basename)
.collect::<Result<_, Error>>()?;
let v = layout::render(
&self.shared.templates,
&self.shared.layout,
self,
&*self.shared.clone(),
&page,
sidebar,
settings(
self.shared.static_root_path.as_deref().unwrap_or("./"),
&self.shared.resource_suffix,
theme_names,
)?,
&self.shared.style_files,
);
self.shared.fs.write(settings_file, v)?;
if let Some(ref redirections) = self.shared.redirections {
Expand Down Expand Up @@ -648,7 +647,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
if !self.render_redirect_pages {
self.render_redirect_pages = item.is_stripped();
}
let scx = &self.shared;
let scx = self.shared.clone();
let item_name = item.name.as_ref().unwrap().to_string();
self.dst.push(&item_name);
self.current.push(item_name);
Expand All @@ -660,7 +659,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
if !buf.is_empty() {
self.shared.ensure_dir(&self.dst)?;
let joint_dst = self.dst.join("index.html");
scx.fs.write(joint_dst, buf)?;
scx.clone().fs.write(joint_dst, buf)?;
}

// Render sidebar-items.js used throughout this module.
Expand Down
Loading