Skip to content

Commit c92ac87

Browse files
authored
Rollup merge of #82963 - camelid:move-sharedcontext, r=GuillaumeGomez
Move `SharedContext` to `context.rs` It is tightly connected to `Context` and is primarily used as a field in `Context`. Thus, it should be next to `Context`.
2 parents 5c897d4 + 525646a commit c92ac87

File tree

2 files changed

+77
-83
lines changed

2 files changed

+77
-83
lines changed

src/librustdoc/html/render/context.rs

+72-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::cell::RefCell;
22
use std::collections::BTreeMap;
33
use std::io;
4-
use std::path::PathBuf;
4+
use std::path::{Path, PathBuf};
55
use std::rc::Rc;
6-
use std::sync::mpsc::channel;
6+
use std::sync::mpsc::{channel, Receiver};
77

8-
use rustc_data_structures::fx::FxHashMap;
8+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
99
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1010
use rustc_middle::ty::TyCtxt;
1111
use rustc_session::Session;
@@ -16,10 +16,7 @@ use rustc_span::{symbol::sym, Symbol};
1616
use super::cache::{build_index, ExternalLocation};
1717
use super::print_item::{full_path, item_path, print_item};
1818
use super::write_shared::write_shared;
19-
use super::{
20-
print_sidebar, settings, AllTypes, NameDoc, SharedContext, StylePath, BASIC_KEYWORDS,
21-
CURRENT_DEPTH,
22-
};
19+
use super::{print_sidebar, settings, AllTypes, NameDoc, StylePath, BASIC_KEYWORDS, CURRENT_DEPTH};
2320

2421
use crate::clean::{self, AttributesExt};
2522
use crate::config::RenderOptions;
@@ -78,6 +75,74 @@ crate struct Context<'tcx> {
7875
#[cfg(target_arch = "x86_64")]
7976
rustc_data_structures::static_assert_size!(Context<'_>, 152);
8077

78+
/// Shared mutable state used in [`Context`] and elsewhere.
79+
crate struct SharedContext<'tcx> {
80+
crate tcx: TyCtxt<'tcx>,
81+
/// The path to the crate root source minus the file name.
82+
/// Used for simplifying paths to the highlighted source code files.
83+
crate src_root: PathBuf,
84+
/// This describes the layout of each page, and is not modified after
85+
/// creation of the context (contains info like the favicon and added html).
86+
crate layout: layout::Layout,
87+
/// This flag indicates whether `[src]` links should be generated or not. If
88+
/// the source files are present in the html rendering, then this will be
89+
/// `true`.
90+
crate include_sources: bool,
91+
/// The local file sources we've emitted and their respective url-paths.
92+
crate local_sources: FxHashMap<PathBuf, String>,
93+
/// Whether the collapsed pass ran
94+
collapsed: bool,
95+
/// The base-URL of the issue tracker for when an item has been tagged with
96+
/// an issue number.
97+
pub(super) issue_tracker_base_url: Option<String>,
98+
/// The directories that have already been created in this doc run. Used to reduce the number
99+
/// of spurious `create_dir_all` calls.
100+
created_dirs: RefCell<FxHashSet<PathBuf>>,
101+
/// This flag indicates whether listings of modules (in the side bar and documentation itself)
102+
/// should be ordered alphabetically or in order of appearance (in the source code).
103+
pub(super) sort_modules_alphabetically: bool,
104+
/// Additional CSS files to be added to the generated docs.
105+
crate style_files: Vec<StylePath>,
106+
/// Suffix to be added on resource files (if suffix is "-v2" then "light.css" becomes
107+
/// "light-v2.css").
108+
crate resource_suffix: String,
109+
/// Optional path string to be used to load static files on output pages. If not set, uses
110+
/// combinations of `../` to reach the documentation root.
111+
crate static_root_path: Option<String>,
112+
/// The fs handle we are working with.
113+
crate fs: DocFS,
114+
/// The default edition used to parse doctests.
115+
crate edition: Edition,
116+
pub(super) codes: ErrorCodes,
117+
pub(super) playground: Option<markdown::Playground>,
118+
all: RefCell<AllTypes>,
119+
/// Storage for the errors produced while generating documentation so they
120+
/// can be printed together at the end.
121+
errors: Receiver<String>,
122+
/// `None` by default, depends on the `generate-redirect-map` option flag. If this field is set
123+
/// to `Some(...)`, it'll store redirections and then generate a JSON file at the top level of
124+
/// the crate.
125+
redirections: Option<RefCell<FxHashMap<String, String>>>,
126+
}
127+
128+
impl SharedContext<'_> {
129+
crate fn ensure_dir(&self, dst: &Path) -> Result<(), Error> {
130+
let mut dirs = self.created_dirs.borrow_mut();
131+
if !dirs.contains(dst) {
132+
try_err!(self.fs.create_dir_all(dst), dst);
133+
dirs.insert(dst.to_path_buf());
134+
}
135+
136+
Ok(())
137+
}
138+
139+
/// Based on whether the `collapse-docs` pass was run, return either the `doc_value` or the
140+
/// `collapsed_doc_value` of the given item.
141+
crate fn maybe_collapsed_doc_value<'a>(&self, item: &'a clean::Item) -> Option<String> {
142+
if self.collapsed { item.collapsed_doc_value() } else { item.doc_value() }
143+
}
144+
}
145+
81146
impl<'tcx> Context<'tcx> {
82147
pub(super) fn tcx(&self) -> TyCtxt<'tcx> {
83148
self.shared.tcx

src/librustdoc/html/render/mod.rs

+5-76
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,30 @@ mod write_shared;
3535
crate use context::*;
3636
crate use write_shared::FILES_UNVERSIONED;
3737

38-
use std::cell::{Cell, RefCell};
38+
use std::cell::Cell;
3939
use std::collections::VecDeque;
4040
use std::default::Default;
4141
use std::fmt;
42-
use std::path::{Path, PathBuf};
42+
use std::path::PathBuf;
4343
use std::str;
4444
use std::string::ToString;
45-
use std::sync::mpsc::Receiver;
4645

4746
use itertools::Itertools;
4847
use rustc_ast_pretty::pprust;
4948
use rustc_attr::{Deprecation, StabilityLevel};
50-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
49+
use rustc_data_structures::fx::FxHashSet;
5150
use rustc_hir as hir;
5251
use rustc_hir::def::CtorKind;
5352
use rustc_hir::def_id::DefId;
5453
use rustc_hir::Mutability;
5554
use rustc_middle::middle::stability;
5655
use rustc_middle::ty::TyCtxt;
57-
use rustc_span::edition::Edition;
5856
use rustc_span::symbol::{kw, sym, Symbol};
5957
use serde::ser::SerializeSeq;
6058
use serde::{Serialize, Serializer};
6159

6260
use crate::clean::{self, GetDefId, RenderedLink, SelfTy, TypeKind};
63-
use crate::docfs::{DocFS, PathError};
61+
use crate::docfs::PathError;
6462
use crate::error::Error;
6563
use crate::formats::cache::Cache;
6664
use crate::formats::item_type::ItemType;
@@ -70,8 +68,7 @@ use crate::html::format::{
7068
href, print_abi_with_space, print_default_space, print_generic_bounds, print_where_clause,
7169
Buffer, PrintWithSpace,
7270
};
73-
use crate::html::layout;
74-
use crate::html::markdown::{self, ErrorCodes, Markdown, MarkdownHtml, MarkdownSummaryLine};
71+
use crate::html::markdown::{Markdown, MarkdownHtml, MarkdownSummaryLine};
7572

7673
/// A pair of name and its optional document.
7774
crate type NameDoc = (String, Option<String>);
@@ -82,74 +79,6 @@ crate fn ensure_trailing_slash(v: &str) -> impl fmt::Display + '_ {
8279
})
8380
}
8481

85-
/// Shared mutable state used in [`Context`] and elsewhere.
86-
crate struct SharedContext<'tcx> {
87-
crate tcx: TyCtxt<'tcx>,
88-
/// The path to the crate root source minus the file name.
89-
/// Used for simplifying paths to the highlighted source code files.
90-
crate src_root: PathBuf,
91-
/// This describes the layout of each page, and is not modified after
92-
/// creation of the context (contains info like the favicon and added html).
93-
crate layout: layout::Layout,
94-
/// This flag indicates whether `[src]` links should be generated or not. If
95-
/// the source files are present in the html rendering, then this will be
96-
/// `true`.
97-
crate include_sources: bool,
98-
/// The local file sources we've emitted and their respective url-paths.
99-
crate local_sources: FxHashMap<PathBuf, String>,
100-
/// Whether the collapsed pass ran
101-
collapsed: bool,
102-
/// The base-URL of the issue tracker for when an item has been tagged with
103-
/// an issue number.
104-
issue_tracker_base_url: Option<String>,
105-
/// The directories that have already been created in this doc run. Used to reduce the number
106-
/// of spurious `create_dir_all` calls.
107-
created_dirs: RefCell<FxHashSet<PathBuf>>,
108-
/// This flag indicates whether listings of modules (in the side bar and documentation itself)
109-
/// should be ordered alphabetically or in order of appearance (in the source code).
110-
sort_modules_alphabetically: bool,
111-
/// Additional CSS files to be added to the generated docs.
112-
crate style_files: Vec<StylePath>,
113-
/// Suffix to be added on resource files (if suffix is "-v2" then "light.css" becomes
114-
/// "light-v2.css").
115-
crate resource_suffix: String,
116-
/// Optional path string to be used to load static files on output pages. If not set, uses
117-
/// combinations of `../` to reach the documentation root.
118-
crate static_root_path: Option<String>,
119-
/// The fs handle we are working with.
120-
crate fs: DocFS,
121-
/// The default edition used to parse doctests.
122-
crate edition: Edition,
123-
codes: ErrorCodes,
124-
playground: Option<markdown::Playground>,
125-
all: RefCell<AllTypes>,
126-
/// Storage for the errors produced while generating documentation so they
127-
/// can be printed together at the end.
128-
errors: Receiver<String>,
129-
/// `None` by default, depends on the `generate-redirect-map` option flag. If this field is set
130-
/// to `Some(...)`, it'll store redirections and then generate a JSON file at the top level of
131-
/// the crate.
132-
redirections: Option<RefCell<FxHashMap<String, String>>>,
133-
}
134-
135-
impl SharedContext<'_> {
136-
crate fn ensure_dir(&self, dst: &Path) -> Result<(), Error> {
137-
let mut dirs = self.created_dirs.borrow_mut();
138-
if !dirs.contains(dst) {
139-
try_err!(self.fs.create_dir_all(dst), dst);
140-
dirs.insert(dst.to_path_buf());
141-
}
142-
143-
Ok(())
144-
}
145-
146-
/// Based on whether the `collapse-docs` pass was run, return either the `doc_value` or the
147-
/// `collapsed_doc_value` of the given item.
148-
crate fn maybe_collapsed_doc_value<'a>(&self, item: &'a clean::Item) -> Option<String> {
149-
if self.collapsed { item.collapsed_doc_value() } else { item.doc_value() }
150-
}
151-
}
152-
15382
// Helper structs for rendering items/sidebars and carrying along contextual
15483
// information
15584

0 commit comments

Comments
 (0)