Skip to content

Commit 9badf69

Browse files
authored
Rollup merge of rust-lang#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 4fa76a4 + e022936 commit 9badf69

File tree

2 files changed

+78
-82
lines changed

2 files changed

+78
-82
lines changed

src/librustdoc/html/render/context.rs

Lines changed: 73 additions & 5 deletions
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;
@@ -17,8 +17,8 @@ use super::cache::{build_index, ExternalLocation};
1717
use super::print_item::{full_path, item_path, print_item};
1818
use super::write_shared::write_shared;
1919
use super::{
20-
print_sidebar, settings, AllTypes, NameDoc, SharedContext, StylePath, BASIC_KEYWORDS,
21-
CURRENT_DEPTH, INITIAL_IDS,
20+
print_sidebar, settings, AllTypes, NameDoc, StylePath, BASIC_KEYWORDS, CURRENT_DEPTH,
21+
INITIAL_IDS,
2222
};
2323

2424
use crate::clean::{self, AttributesExt};
@@ -78,6 +78,74 @@ crate struct Context<'tcx> {
7878
#[cfg(target_arch = "x86_64")]
7979
rustc_data_structures::static_assert_size!(Context<'_>, 152);
8080

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

src/librustdoc/html/render/mod.rs

Lines changed: 5 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,29 @@ 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;
56-
use rustc_middle::ty::TyCtxt;
57-
use rustc_span::edition::Edition;
5855
use rustc_span::symbol::{kw, sym, Symbol};
5956
use serde::ser::SerializeSeq;
6057
use serde::{Serialize, Serializer};
6158

6259
use crate::clean::{self, GetDefId, RenderedLink, SelfTy, TypeKind};
63-
use crate::docfs::{DocFS, PathError};
60+
use crate::docfs::PathError;
6461
use crate::error::Error;
6562
use crate::formats::cache::Cache;
6663
use crate::formats::item_type::ItemType;
@@ -70,8 +67,7 @@ use crate::html::format::{
7067
href, print_abi_with_space, print_default_space, print_generic_bounds, print_where_clause,
7168
Buffer, PrintWithSpace,
7269
};
73-
use crate::html::layout;
74-
use crate::html::markdown::{self, ErrorCodes, Markdown, MarkdownHtml, MarkdownSummaryLine};
70+
use crate::html::markdown::{Markdown, MarkdownHtml, MarkdownSummaryLine};
7571

7672
/// A pair of name and its optional document.
7773
crate type NameDoc = (String, Option<String>);
@@ -82,74 +78,6 @@ crate fn ensure_trailing_slash(v: &str) -> impl fmt::Display + '_ {
8278
})
8379
}
8480

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-
15381
// Helper structs for rendering items/sidebars and carrying along contextual
15482
// information
15583

0 commit comments

Comments
 (0)