X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibrustdoc%2Fjson%2Fmod.rs;h=7d4d5598c6b9b0ea5afeb3200312502c9ef9fd6a;hb=c689b97fba5d0f8832c72c3b64d200eee40d4704;hp=512c9124727efde4d836fa436d9a8d103de53775;hpb=fe6b3a97921eb59502dfca13505e3ef68f5289bb;p=rust.git diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index 512c9124727..7d4d5598c6b 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -5,7 +5,6 @@ //! docs for usage and details. mod conversions; -pub mod types; use std::cell::RefCell; use std::fs::File; @@ -17,12 +16,15 @@ use rustc_session::Session; use rustc_span::edition::Edition; +use json_types as types; + use crate::clean; use crate::config::{RenderInfo, RenderOptions}; use crate::error::Error; use crate::formats::cache::Cache; use crate::formats::FormatRenderer; use crate::html::render::cache::ExternalLocation; +use crate::json::conversions::from_def_id; #[derive(Clone)] crate struct JsonRenderer<'tcx> { @@ -32,6 +34,7 @@ index: Rc>>, /// The directory where the blob will be written to. out_path: PathBuf, + cache: Rc, } impl JsonRenderer<'_> { @@ -39,12 +42,8 @@ fn sess(&self) -> &Session { self.tcx.sess } - fn get_trait_implementors( - &mut self, - id: rustc_span::def_id::DefId, - cache: &Cache, - ) -> Vec { - cache + fn get_trait_implementors(&mut self, id: rustc_span::def_id::DefId) -> Vec { + Rc::clone(&self.cache) .implementors .get(&id) .map(|implementors| { @@ -52,16 +51,16 @@ fn get_trait_implementors( .iter() .map(|i| { let item = &i.impl_item; - self.item(item.clone(), cache).unwrap(); - item.def_id.into() + self.item(item.clone()).unwrap(); + from_def_id(item.def_id) }) .collect() }) .unwrap_or_default() } - fn get_impls(&mut self, id: rustc_span::def_id::DefId, cache: &Cache) -> Vec { - cache + fn get_impls(&mut self, id: rustc_span::def_id::DefId) -> Vec { + Rc::clone(&self.cache) .impls .get(&id) .map(|impls| { @@ -70,8 +69,8 @@ fn get_impls(&mut self, id: rustc_span::def_id::DefId, cache: &Cache) -> Vec Vec Vec<(types::Id, types::Item)> { - cache + fn get_trait_items(&mut self) -> Vec<(types::Id, types::Item)> { + Rc::clone(&self.cache) .traits .iter() .filter_map(|(&id, trait_item)| { // only need to synthesize items for external traits if !id.is_local() { - trait_item.items.clone().into_iter().for_each(|i| self.item(i, cache).unwrap()); + trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap()); Some(( - id.into(), + from_def_id(id), types::Item { - id: id.into(), + id: from_def_id(id), crate_id: id.krate.as_u32(), - name: cache + name: self + .cache .paths .get(&id) .unwrap_or_else(|| { - cache + self.cache .external_paths .get(&id) .expect("Trait should either be in local or external paths") @@ -134,7 +134,7 @@ fn init( options: RenderOptions, _render_info: RenderInfo, _edition: Edition, - _cache: &mut Cache, + cache: Cache, tcx: TyCtxt<'tcx>, ) -> Result<(Self, clean::Crate), Error> { debug!("Initializing json renderer"); @@ -143,6 +143,7 @@ fn init( tcx, index: Rc::new(RefCell::new(FxHashMap::default())), out_path: options.output, + cache: Rc::new(cache), }, krate, )) @@ -151,20 +152,20 @@ fn init( /// Inserts an item into the index. This should be used rather than directly calling insert on /// the hashmap because certain items (traits and types) need to have their mappings for trait /// implementations filled out before they're inserted. - fn item(&mut self, item: clean::Item, cache: &Cache) -> Result<(), Error> { + fn item(&mut self, item: clean::Item) -> Result<(), Error> { // Flatten items that recursively store other items - item.kind.inner_items().for_each(|i| self.item(i.clone(), cache).unwrap()); + item.kind.inner_items().for_each(|i| self.item(i.clone()).unwrap()); let id = item.def_id; if let Some(mut new_item) = self.convert_item(item) { if let types::ItemEnum::TraitItem(ref mut t) = new_item.inner { - t.implementors = self.get_trait_implementors(id, cache) + t.implementors = self.get_trait_implementors(id) } else if let types::ItemEnum::StructItem(ref mut s) = new_item.inner { - s.impls = self.get_impls(id, cache) + s.impls = self.get_impls(id) } else if let types::ItemEnum::EnumItem(ref mut e) = new_item.inner { - e.impls = self.get_impls(id, cache) + e.impls = self.get_impls(id) } - let removed = self.index.borrow_mut().insert(id.into(), new_item.clone()); + let removed = self.index.borrow_mut().insert(from_def_id(id), new_item.clone()); // FIXME(adotinthevoid): Currently, the index is duplicated. This is a sanity check // to make sure the items are unique. if let Some(old_item) = removed { @@ -175,27 +176,20 @@ fn item(&mut self, item: clean::Item, cache: &Cache) -> Result<(), Error> { Ok(()) } - fn mod_item_in( - &mut self, - item: &clean::Item, - _module_name: &str, - cache: &Cache, - ) -> Result<(), Error> { + fn mod_item_in(&mut self, item: &clean::Item, _module_name: &str) -> Result<(), Error> { use clean::types::ItemKind::*; if let ModuleItem(m) = &*item.kind { for item in &m.items { match &*item.kind { // These don't have names so they don't get added to the output by default - ImportItem(_) => self.item(item.clone(), cache).unwrap(), - ExternCrateItem(_, _) => self.item(item.clone(), cache).unwrap(), - ImplItem(i) => { - i.items.iter().for_each(|i| self.item(i.clone(), cache).unwrap()) - } + ImportItem(_) => self.item(item.clone()).unwrap(), + ExternCrateItem(_, _) => self.item(item.clone()).unwrap(), + ImplItem(i) => i.items.iter().for_each(|i| self.item(i.clone()).unwrap()), _ => {} } } } - self.item(item.clone(), cache).unwrap(); + self.item(item.clone()).unwrap(); Ok(()) } @@ -206,30 +200,38 @@ fn mod_item_out(&mut self, _item_name: &str) -> Result<(), Error> { fn after_krate( &mut self, krate: &clean::Crate, - cache: &Cache, _diag: &rustc_errors::Handler, ) -> Result<(), Error> { debug!("Done with crate"); let mut index = (*self.index).clone().into_inner(); - index.extend(self.get_trait_items(cache)); + index.extend(self.get_trait_items()); + let len = index.len(); let output = types::Crate { root: types::Id(String::from("0:0")), crate_version: krate.version.clone(), - includes_private: cache.document_private, - index, - paths: cache + includes_private: self.cache.document_private, + index: index.into_iter().fold( + std::collections::HashMap::with_capacity(len), + |mut acc, (key, val)| { + acc.insert(key, val); + acc + }, + ), + paths: self + .cache .paths .clone() .into_iter() - .chain(cache.external_paths.clone().into_iter()) + .chain(self.cache.external_paths.clone().into_iter()) .map(|(k, (path, kind))| { ( - k.into(), + from_def_id(k), types::ItemSummary { crate_id: k.krate.as_u32(), path, kind: kind.into() }, ) }) .collect(), - external_crates: cache + external_crates: self + .cache .extern_locations .iter() .map(|(k, v)| { @@ -254,4 +256,8 @@ fn after_krate( serde_json::ser::to_writer(&file, &output).unwrap(); Ok(()) } + + fn cache(&self) -> &Cache { + &self.cache + } }