More requested changes
This commit is contained in:
@@ -44,9 +44,9 @@ pub type ExternalPaths = FxHashMap<DefId, (Vec<String>, clean::TypeKind)>;
|
|||||||
pub struct DocContext<'tcx> {
|
pub struct DocContext<'tcx> {
|
||||||
pub tcx: TyCtxt<'tcx>,
|
pub tcx: TyCtxt<'tcx>,
|
||||||
pub resolver: Rc<RefCell<interface::BoxedResolver>>,
|
pub resolver: Rc<RefCell<interface::BoxedResolver>>,
|
||||||
/// Later on moved into `formats::cache::CACHE_KEY`
|
/// Later on moved into `CACHE_KEY`
|
||||||
pub renderinfo: RefCell<RenderInfo>,
|
pub renderinfo: RefCell<RenderInfo>,
|
||||||
/// Later on moved through `clean::Crate` into `formats::cache::CACHE_KEY`
|
/// Later on moved through `clean::Crate` into `CACHE_KEY`
|
||||||
pub external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
|
pub external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
|
||||||
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
|
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
|
||||||
/// the same time.
|
/// the same time.
|
||||||
|
|||||||
@@ -148,29 +148,19 @@ impl Cache {
|
|||||||
external_paths.into_iter().map(|(k, (v, t))| (k, (v, ItemType::from(t)))).collect();
|
external_paths.into_iter().map(|(k, (v, t))| (k, (v, ItemType::from(t)))).collect();
|
||||||
|
|
||||||
let mut cache = Cache {
|
let mut cache = Cache {
|
||||||
impls: Default::default(),
|
|
||||||
external_paths,
|
external_paths,
|
||||||
exact_paths,
|
exact_paths,
|
||||||
paths: Default::default(),
|
|
||||||
implementors: Default::default(),
|
|
||||||
stack: Vec::new(),
|
|
||||||
parent_stack: Vec::new(),
|
|
||||||
search_index: Vec::new(),
|
|
||||||
parent_is_trait_impl: false,
|
parent_is_trait_impl: false,
|
||||||
extern_locations: Default::default(),
|
|
||||||
primitive_locations: Default::default(),
|
|
||||||
stripped_mod: false,
|
stripped_mod: false,
|
||||||
access_levels,
|
access_levels,
|
||||||
crate_version: krate.version.take(),
|
crate_version: krate.version.take(),
|
||||||
document_private,
|
document_private,
|
||||||
orphan_impl_items: Vec::new(),
|
|
||||||
orphan_trait_impls: Vec::new(),
|
|
||||||
traits: krate.external_traits.replace(Default::default()),
|
traits: krate.external_traits.replace(Default::default()),
|
||||||
deref_trait_did,
|
deref_trait_did,
|
||||||
deref_mut_trait_did,
|
deref_mut_trait_did,
|
||||||
owned_box_did,
|
owned_box_did,
|
||||||
masked_crates: mem::take(&mut krate.masked_crates),
|
masked_crates: mem::take(&mut krate.masked_crates),
|
||||||
aliases: Default::default(),
|
..Cache::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Cache where all our extern crates are located
|
// Cache where all our extern crates are located
|
||||||
@@ -211,7 +201,7 @@ impl Cache {
|
|||||||
for (trait_did, dids, impl_) in cache.orphan_trait_impls.drain(..) {
|
for (trait_did, dids, impl_) in cache.orphan_trait_impls.drain(..) {
|
||||||
if cache.traits.contains_key(&trait_did) {
|
if cache.traits.contains_key(&trait_did) {
|
||||||
for did in dids {
|
for did in dids {
|
||||||
cache.impls.entry(did).or_insert(vec![]).push(impl_.clone());
|
cache.impls.entry(did).or_default().push(impl_.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ pub mod cache;
|
|||||||
pub mod item_type;
|
pub mod item_type;
|
||||||
pub mod renderer;
|
pub mod renderer;
|
||||||
|
|
||||||
pub use renderer::{FormatRenderer, Renderer};
|
pub use renderer::{run_format, FormatRenderer};
|
||||||
|
|
||||||
use rustc_span::def_id::DefId;
|
use rustc_span::def_id::DefId;
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use crate::config::{RenderInfo, RenderOptions};
|
|||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::formats::cache::{Cache, CACHE_KEY};
|
use crate::formats::cache::{Cache, CACHE_KEY};
|
||||||
|
|
||||||
/// Allows for different backends to rustdoc to be used with the `Renderer::run()` function. Each
|
/// Allows for different backends to rustdoc to be used with the `run_format()` function. Each
|
||||||
/// backend renderer has hooks for initialization, documenting an item, entering and exiting a
|
/// backend renderer has hooks for initialization, documenting an item, entering and exiting a
|
||||||
/// module, and cleanup/finalizing output.
|
/// module, and cleanup/finalizing output.
|
||||||
pub trait FormatRenderer: Clone {
|
pub trait FormatRenderer: Clone {
|
||||||
@@ -42,75 +42,65 @@ pub trait FormatRenderer: Clone {
|
|||||||
fn after_run(&mut self, diag: &rustc_errors::Handler) -> Result<(), Error>;
|
fn after_run(&mut self, diag: &rustc_errors::Handler) -> Result<(), Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
/// Main method for rendering a crate.
|
||||||
pub struct Renderer;
|
pub fn run_format<T: FormatRenderer>(
|
||||||
|
krate: clean::Crate,
|
||||||
|
options: RenderOptions,
|
||||||
|
render_info: RenderInfo,
|
||||||
|
diag: &rustc_errors::Handler,
|
||||||
|
edition: Edition,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
let (krate, mut cache) = Cache::from_krate(
|
||||||
|
render_info.clone(),
|
||||||
|
options.document_private,
|
||||||
|
&options.extern_html_root_urls,
|
||||||
|
&options.output,
|
||||||
|
krate,
|
||||||
|
);
|
||||||
|
|
||||||
impl Renderer {
|
let (mut format_renderer, mut krate) =
|
||||||
pub fn new() -> Renderer {
|
T::init(krate, options, render_info, edition, &mut cache)?;
|
||||||
Renderer
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Main method for rendering a crate.
|
let cache = Arc::new(cache);
|
||||||
pub fn run<T: FormatRenderer + Clone>(
|
// Freeze the cache now that the index has been built. Put an Arc into TLS for future
|
||||||
self,
|
// parallelization opportunities
|
||||||
krate: clean::Crate,
|
CACHE_KEY.with(|v| *v.borrow_mut() = cache.clone());
|
||||||
options: RenderOptions,
|
|
||||||
render_info: RenderInfo,
|
|
||||||
diag: &rustc_errors::Handler,
|
|
||||||
edition: Edition,
|
|
||||||
) -> Result<(), Error> {
|
|
||||||
let (krate, mut cache) = Cache::from_krate(
|
|
||||||
render_info.clone(),
|
|
||||||
options.document_private,
|
|
||||||
&options.extern_html_root_urls,
|
|
||||||
&options.output,
|
|
||||||
krate,
|
|
||||||
);
|
|
||||||
|
|
||||||
let (mut format_renderer, mut krate) =
|
let mut item = match krate.module.take() {
|
||||||
T::init(krate, options, render_info, edition, &mut cache)?;
|
Some(i) => i,
|
||||||
|
None => return Ok(()),
|
||||||
|
};
|
||||||
|
|
||||||
let cache = Arc::new(cache);
|
item.name = Some(krate.name.clone());
|
||||||
// Freeze the cache now that the index has been built. Put an Arc into TLS for future
|
|
||||||
// parallelization opportunities
|
|
||||||
CACHE_KEY.with(|v| *v.borrow_mut() = cache.clone());
|
|
||||||
|
|
||||||
let mut item = match krate.module.take() {
|
// Render the crate documentation
|
||||||
Some(i) => i,
|
let mut work = vec![(format_renderer.clone(), item)];
|
||||||
None => return Ok(()),
|
|
||||||
};
|
|
||||||
|
|
||||||
item.name = Some(krate.name.clone());
|
while let Some((mut cx, item)) = work.pop() {
|
||||||
|
if item.is_mod() {
|
||||||
// Render the crate documentation
|
// modules are special because they add a namespace. We also need to
|
||||||
let mut work = vec![(format_renderer.clone(), item)];
|
// recurse into the items of the module as well.
|
||||||
|
let name = item.name.as_ref().unwrap().to_string();
|
||||||
while let Some((mut cx, item)) = work.pop() {
|
if name.is_empty() {
|
||||||
if item.is_mod() {
|
panic!("Unexpected module with empty name");
|
||||||
// modules are special because they add a namespace. We also need to
|
|
||||||
// recurse into the items of the module as well.
|
|
||||||
let name = item.name.as_ref().unwrap().to_string();
|
|
||||||
if name.is_empty() {
|
|
||||||
panic!("Unexpected module with empty name");
|
|
||||||
}
|
|
||||||
|
|
||||||
cx.mod_item_in(&item, &name, &cache)?;
|
|
||||||
let module = match item.inner {
|
|
||||||
clean::StrippedItem(box clean::ModuleItem(m)) | clean::ModuleItem(m) => m,
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
for it in module.items {
|
|
||||||
debug!("Adding {:?} to worklist", it.name);
|
|
||||||
work.push((cx.clone(), it));
|
|
||||||
}
|
|
||||||
|
|
||||||
cx.mod_item_out(&name)?;
|
|
||||||
} else if item.name.is_some() {
|
|
||||||
cx.item(item, &cache)?;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
format_renderer.after_krate(&krate, &cache)?;
|
cx.mod_item_in(&item, &name, &cache)?;
|
||||||
format_renderer.after_run(diag)
|
let module = match item.inner {
|
||||||
|
clean::StrippedItem(box clean::ModuleItem(m)) | clean::ModuleItem(m) => m,
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
for it in module.items {
|
||||||
|
debug!("Adding {:?} to worklist", it.name);
|
||||||
|
work.push((cx.clone(), it));
|
||||||
|
}
|
||||||
|
|
||||||
|
cx.mod_item_out(&name)?;
|
||||||
|
} else if item.name.is_some() {
|
||||||
|
cx.item(item, &cache)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
format_renderer.after_krate(&krate, &cache)?;
|
||||||
|
format_renderer.after_run(diag)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -228,7 +228,7 @@ impl Serialize for IndexItem {
|
|||||||
|
|
||||||
/// A type used for the search index.
|
/// A type used for the search index.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct RenderType {
|
crate struct RenderType {
|
||||||
ty: Option<DefId>,
|
ty: Option<DefId>,
|
||||||
idx: Option<usize>,
|
idx: Option<usize>,
|
||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
@@ -259,7 +259,7 @@ impl Serialize for RenderType {
|
|||||||
|
|
||||||
/// A type used for the search index.
|
/// A type used for the search index.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Generic {
|
crate struct Generic {
|
||||||
name: String,
|
name: String,
|
||||||
defid: Option<DefId>,
|
defid: Option<DefId>,
|
||||||
idx: Option<usize>,
|
idx: Option<usize>,
|
||||||
@@ -313,7 +313,7 @@ impl Serialize for IndexItemFunctionType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct TypeWithKind {
|
crate struct TypeWithKind {
|
||||||
ty: RenderType,
|
ty: RenderType,
|
||||||
kind: TypeKind,
|
kind: TypeKind,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -502,9 +502,9 @@ fn main_options(options: config::Options) -> i32 {
|
|||||||
info!("going to format");
|
info!("going to format");
|
||||||
let (error_format, edition, debugging_options) = diag_opts;
|
let (error_format, edition, debugging_options) = diag_opts;
|
||||||
let diag = core::new_handler(error_format, None, &debugging_options);
|
let diag = core::new_handler(error_format, None, &debugging_options);
|
||||||
match formats::Renderer::new()
|
match formats::run_format::<html::render::Context>(
|
||||||
.run::<html::render::Context>(krate, renderopts, renderinfo, &diag, edition)
|
krate, renderopts, renderinfo, &diag, edition,
|
||||||
{
|
) {
|
||||||
Ok(_) => rustc_driver::EXIT_SUCCESS,
|
Ok(_) => rustc_driver::EXIT_SUCCESS,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
diag.struct_err(&format!("couldn't generate documentation: {}", e.error))
|
diag.struct_err(&format!("couldn't generate documentation: {}", e.error))
|
||||||
|
|||||||
Reference in New Issue
Block a user