Skip to content

rustdoc: Fix generating documentation from json #32698

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix de/serializing rustdoc documentation
`DefId` needs to be converted to a `String` so that we can use it as
a key for a `Json::Object`.
  • Loading branch information
mitaa committed Apr 3, 2016
commit 8c88cda38b1be75bfe09d3c6322b5c006acadb4e
53 changes: 49 additions & 4 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,22 @@ use rustc::middle::stability;

use rustc_front::hir;

use serialize::{Encoder, Decoder, Encodable, Decodable};

use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::rc::Rc;
use std::u32;
use std::env::current_dir;
use std::str::FromStr;

use core::DocContext;
use doctree;
use visit_ast;

/// A stable identifier to the particular version of JSON output.
/// Increment this when the `Crate` and related structures change.
pub const SCHEMA_VERSION: &'static str = "0.8.3";
pub const SCHEMA_VERSION: &'static str = "0.8.4";

mod inline;
mod simplify;
Expand Down Expand Up @@ -116,14 +119,54 @@ impl<T: Clean<U>, U> Clean<Vec<U>> for P<[T]> {
}
}

#[derive(Clone, Debug)]
pub struct ExternalTraits {
pub map: HashMap<DefId, Trait>,
}

impl Encodable for ExternalTraits {
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_map(self.map.len(), |e| {
for (i, (did, val)) in self.map.iter().enumerate() {
let key = format!("{:?}@{:?}", did.krate, did.index.as_u32());
e.emit_map_elt_key(i, |e| key.encode(e))?;
e.emit_map_elt_val(i, |e| val.encode(e))?;
}
Ok(())
})
}
}

impl Decodable for ExternalTraits {
fn decode<D: Decoder>(d: &mut D) -> Result<ExternalTraits, D::Error> {
d.read_map(|d, len| {
let state = Default::default();
let mut map = HashMap::with_capacity_and_hasher(len, state);
for i in 0..len {
let key: String = d.read_map_elt_key(i, |d| Decodable::decode(d))?;
let val = d.read_map_elt_val(i, |d| Decodable::decode(d))?;

let mut fragments = key.split('@').map(|f| u32::from_str(f).unwrap());
let krate = fragments.next().unwrap();
let index = DefIndex::from_u32(fragments.next().unwrap());
assert!(fragments.next().is_none());

let did = DefId { krate: krate, index: index };
map.insert(did, val);
}
Ok(ExternalTraits { map: map })
})
}
}

#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct Crate {
pub name: String,
pub src: PathBuf,
pub module: Option<Item>,
pub externs: Vec<(ast::CrateNum, ExternalCrate)>,
pub primitives: Vec<PrimitiveType>,
pub external_traits: HashMap<DefId, Trait>,
pub external_traits: ExternalTraits,
}

struct CrateNum(ast::CrateNum);
Expand Down Expand Up @@ -214,8 +257,10 @@ impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
module: Some(module),
externs: externs,
primitives: primitives,
external_traits: cx.external_traits.borrow_mut().take()
.unwrap_or(HashMap::new()),
external_traits: ExternalTraits {
map: cx.external_traits.borrow_mut().take()
.unwrap_or(HashMap::new()),
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub trait DocFolder : Sized {
c.module = c.module.and_then(|module| {
self.fold_item(module)
});
c.external_traits = c.external_traits.into_iter().map(|(k, mut v)| {
c.external_traits.map = c.external_traits.map.into_iter().map(|(k, mut v)| {
v.items = v.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
(k, v)
}).collect();
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ pub fn run(mut krate: clean::Crate,
privmod: false,
access_levels: access_levels,
orphan_methods: Vec::new(),
traits: mem::replace(&mut krate.external_traits, HashMap::new()),
traits: mem::replace(&mut krate.external_traits.map, HashMap::new()),
deref_trait_did: analysis.as_ref().and_then(|a| a.deref_trait_did),
typarams: analysis.as_ref().map(|a| {
a.external_typarams.borrow_mut().take().unwrap()
Expand Down