Skip to content

Commit

Permalink
rustdoc: Strip implementations of private traits
Browse files Browse the repository at this point in the history
Closes #5416
  • Loading branch information
alexcrichton committed Sep 26, 2013
1 parent ca697d3 commit 6a277dc
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions src/librustdoc/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use std::num;
use std::uint;
use std::hashmap::HashSet;

use syntax::ast;

Expand Down Expand Up @@ -50,9 +51,10 @@ pub fn strip_hidden(crate: clean::Crate) -> plugins::PluginResult {

/// Strip private items from the point of view of a crate or externally from a
/// crate, specified by the `xcrate` flag.
pub fn strip_private(crate: clean::Crate) -> plugins::PluginResult {
struct Stripper;
impl fold::DocFolder for Stripper {
pub fn strip_private(mut crate: clean::Crate) -> plugins::PluginResult {
// This stripper collects all *retained* nodes.
struct Stripper<'self>(&'self mut HashSet<ast::NodeId>);
impl<'self> fold::DocFolder for Stripper<'self> {
fn fold_item(&mut self, i: Item) -> Option<Item> {
match i.inner {
// These items can all get re-exported
Expand Down Expand Up @@ -98,6 +100,7 @@ pub fn strip_private(crate: clean::Crate) -> plugins::PluginResult {
};

let i = if fastreturn {
self.insert(i.id);
return Some(i);
} else {
self.fold_item_recur(i)
Expand All @@ -109,15 +112,50 @@ pub fn strip_private(crate: clean::Crate) -> plugins::PluginResult {
// emptied modules/impls have no need to exist
clean::ModuleItem(ref m) if m.items.len() == 0 => None,
clean::ImplItem(ref i) if i.methods.len() == 0 => None,
_ => Some(i),
_ => {
self.insert(i.id);
Some(i)
}
}
}
None => None,
}
}
}
let mut stripper = Stripper;
let crate = stripper.fold_crate(crate);

// This stripper discards all private impls of traits
struct ImplStripper<'self>(&'self HashSet<ast::NodeId>);
impl<'self> fold::DocFolder for ImplStripper<'self> {
fn fold_item(&mut self, i: Item) -> Option<Item> {
match i.inner {
clean::ImplItem(ref imp) => {
match imp.trait_ {
Some(clean::ResolvedPath{ id, _ }) => {
if !self.contains(&id) {
return None;
}
}
Some(*) | None => {}
}
}
_ => {}
}
self.fold_item_recur(i)
}
}

let mut retained = HashSet::new();
// First, strip all private items
{
let mut stripper = Stripper(&mut retained);
crate = stripper.fold_crate(crate);
}

// Next, strip all private implementations of traits
{
let mut stripper = ImplStripper(&retained);
crate = stripper.fold_crate(crate);
}
(crate, None)
}

Expand Down

0 comments on commit 6a277dc

Please sign in to comment.