Skip to content

Commit 6a277dc

Browse files
committed
rustdoc: Strip implementations of private traits
Closes #5416
1 parent ca697d3 commit 6a277dc

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

Diff for: src/librustdoc/passes.rs

+44-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use std::num;
1212
use std::uint;
13+
use std::hashmap::HashSet;
1314

1415
use syntax::ast;
1516

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

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

100102
let i = if fastreturn {
103+
self.insert(i.id);
101104
return Some(i);
102105
} else {
103106
self.fold_item_recur(i)
@@ -109,15 +112,50 @@ pub fn strip_private(crate: clean::Crate) -> plugins::PluginResult {
109112
// emptied modules/impls have no need to exist
110113
clean::ModuleItem(ref m) if m.items.len() == 0 => None,
111114
clean::ImplItem(ref i) if i.methods.len() == 0 => None,
112-
_ => Some(i),
115+
_ => {
116+
self.insert(i.id);
117+
Some(i)
118+
}
113119
}
114120
}
115121
None => None,
116122
}
117123
}
118124
}
119-
let mut stripper = Stripper;
120-
let crate = stripper.fold_crate(crate);
125+
126+
// This stripper discards all private impls of traits
127+
struct ImplStripper<'self>(&'self HashSet<ast::NodeId>);
128+
impl<'self> fold::DocFolder for ImplStripper<'self> {
129+
fn fold_item(&mut self, i: Item) -> Option<Item> {
130+
match i.inner {
131+
clean::ImplItem(ref imp) => {
132+
match imp.trait_ {
133+
Some(clean::ResolvedPath{ id, _ }) => {
134+
if !self.contains(&id) {
135+
return None;
136+
}
137+
}
138+
Some(*) | None => {}
139+
}
140+
}
141+
_ => {}
142+
}
143+
self.fold_item_recur(i)
144+
}
145+
}
146+
147+
let mut retained = HashSet::new();
148+
// First, strip all private items
149+
{
150+
let mut stripper = Stripper(&mut retained);
151+
crate = stripper.fold_crate(crate);
152+
}
153+
154+
// Next, strip all private implementations of traits
155+
{
156+
let mut stripper = ImplStripper(&retained);
157+
crate = stripper.fold_crate(crate);
158+
}
121159
(crate, None)
122160
}
123161

0 commit comments

Comments
 (0)