10
10
11
11
use std:: num;
12
12
use std:: uint;
13
+ use std:: hashmap:: HashSet ;
13
14
14
15
use syntax:: ast;
15
16
@@ -50,9 +51,10 @@ pub fn strip_hidden(crate: clean::Crate) -> plugins::PluginResult {
50
51
51
52
/// Strip private items from the point of view of a crate or externally from a
52
53
/// 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 > {
56
58
fn fold_item ( & mut self , i : Item ) -> Option < Item > {
57
59
match i. inner {
58
60
// These items can all get re-exported
@@ -98,6 +100,7 @@ pub fn strip_private(crate: clean::Crate) -> plugins::PluginResult {
98
100
} ;
99
101
100
102
let i = if fastreturn {
103
+ self . insert ( i. id ) ;
101
104
return Some ( i) ;
102
105
} else {
103
106
self . fold_item_recur ( i)
@@ -109,15 +112,50 @@ pub fn strip_private(crate: clean::Crate) -> plugins::PluginResult {
109
112
// emptied modules/impls have no need to exist
110
113
clean:: ModuleItem ( ref m) if m. items . len ( ) == 0 => None ,
111
114
clean:: ImplItem ( ref i) if i. methods . len ( ) == 0 => None ,
112
- _ => Some ( i) ,
115
+ _ => {
116
+ self . insert ( i. id ) ;
117
+ Some ( i)
118
+ }
113
119
}
114
120
}
115
121
None => None ,
116
122
}
117
123
}
118
124
}
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
+ }
121
159
( crate , None )
122
160
}
123
161
0 commit comments