@@ -232,25 +232,31 @@ impl Clean<ExternalCrate> for CrateNum {
232
232
impl Clean<ItemEnum> for hir::ItemKind<'_> {
233
233
fn clean(&self, _cx: &DocContext<'_>) -> ItemEnum {
234
234
match self {
235
- ExternalCrate(name) =>
235
+ ExternalCrate(name) =>
236
236
}
237
237
}
238
238
}
239
239
*/
240
240
241
- impl Clean < Item > for hir:: Item < ' _ > {
242
- fn clean ( & self , cx : & DocContext < ' _ > ) -> Item {
241
+ enum CleanedModule {
242
+ Module ( ItemKind ) ,
243
+ Inlined ( Vec < Item > ) ,
244
+ }
245
+
246
+ impl Clean < Vec < Item > > for hir:: Item < ' _ > {
247
+ fn clean ( & self , cx : & DocContext < ' _ > ) -> Vec < Item > {
243
248
use hir:: ItemKind ;
244
249
245
250
let def_id = cx. tcx . hir ( ) . local_def_id ( self . hir_id ) . to_def_id ( ) ;
246
251
let name = cx. tcx . item_name ( def_id) . clean ( cx) ;
247
- let inner = match self . kind {
252
+ let kind = match self . kind {
248
253
// TODO: should store Symbol, not String
249
- ItemKind :: ExternCrate ( renamed) => ExternCrateItem ( name. clone ( ) , renamed. clean ( cx) ) ,
250
- ItemKind :: Use ( path, kind) => {
251
- unimplemented ! ( )
252
- }
253
- /*
254
+ ItemKind :: ExternCrate ( renamed) => match clean_extern_crate ( self , renamed, cx) {
255
+ CleanedModule :: Module ( inner) => inner,
256
+ CleanedModule :: Inlined ( items) => return items,
257
+ } ,
258
+ ItemKind :: Use ( path, kind) => unimplemented ! ( ) ,
259
+ /*
254
260
ImportItem(Import {
255
261
kind: kind.clean(cx),
256
262
source: path.clean(cx),
@@ -259,16 +265,16 @@ impl Clean<Item> for hir::Item<'_> {
259
265
_ => unimplemented ! ( ) ,
260
266
} ;
261
267
262
- Item {
268
+ vec ! [ Item {
263
269
def_id,
264
- inner ,
270
+ kind ,
265
271
name: Some ( name) ,
266
272
source: cx. tcx. def_span( def_id) . clean( cx) ,
267
273
attrs: self . attrs. clean( cx) , // should this use tcx.attrs instead?
268
274
visibility: self . vis. clean( cx) , // TODO: use tcx.visibility once #78077 lands
269
275
stability: cx. tcx. lookup_stability( def_id) . copied( ) ,
270
276
deprecation: cx. tcx. lookup_deprecation( def_id) . clean( cx) ,
271
- }
277
+ } ]
272
278
}
273
279
}
274
280
@@ -279,17 +285,22 @@ impl Clean<Item> for hir::Crate<'_> {
279
285
let attrs = self . item . attrs . clean ( cx) ;
280
286
281
287
// Get _all_ the items!
282
- let mut items = self . items . clean ( cx) ;
283
- items. extend ( self . exported_macros . clean ( cx) ) ;
284
- items. extend ( self . trait_items . clean ( cx) ) ;
285
- items. extend ( self . impl_items . clean ( cx) ) ;
288
+ let items = self . items . clean ( cx) . into_iter ( ) . flatten ( ) ;
289
+ let items = items . chain ( self . exported_macros . clean ( cx) ) ;
290
+ let items = items . chain ( self . trait_items . clean ( cx) ) ;
291
+ let items = items . chain ( self . impl_items . clean ( cx) ) ;
286
292
// NOTE: bodies intentionally skipped
287
293
288
- items. extend ( self . trait_impls . iter ( ) . flat_map ( |( _trait, impls) | {
289
- impls. into_iter ( ) . map ( |& impl_| cx. tcx . hir ( ) . item ( impl_) . clean ( cx) )
290
- } ) ) ;
291
- items. extend ( self . modules . clean ( cx) . into_iter ( ) . flatten ( ) ) ;
292
- items. extend ( self . proc_macros . iter ( ) . map ( |hir_id| {
294
+ let items = items. chain (
295
+ self . trait_impls
296
+ . iter ( )
297
+ . flat_map ( |( _trait, impls) | {
298
+ impls. into_iter ( ) . map ( |& impl_| cx. tcx . hir ( ) . item ( impl_) . clean ( cx) )
299
+ } )
300
+ . flatten ( ) ,
301
+ ) ;
302
+ let items = items. chain ( self . modules . clean ( cx) . into_iter ( ) . flatten ( ) ) ;
303
+ let items = items. chain ( self . proc_macros . iter ( ) . map ( |hir_id| {
293
304
let _def_id = hir_id. owner . local_def_index ;
294
305
// TODO: look how `rustc_metadata::rmeta::encoder` does this
295
306
unimplemented ! ( )
@@ -320,7 +331,7 @@ impl Clean<Item> for hir::Crate<'_> {
320
331
stability : cx. stability ( id) ,
321
332
deprecation : cx. deprecation ( id) . clean ( cx) ,
322
333
def_id : cx. tcx . hir ( ) . local_def_id ( id) . to_def_id ( ) ,
323
- kind : ModuleItem ( Module { is_crate : true , items } ) ,
334
+ kind : ModuleItem ( Module { is_crate : true , items : items . collect ( ) } ) ,
324
335
}
325
336
}
326
337
}
@@ -2226,45 +2237,40 @@ impl Clean<Vec<Item>> for doctree::Impl<'_> {
2226
2237
}
2227
2238
}
2228
2239
2229
- impl Clean < Vec < Item > > for doctree:: ExternCrate < ' _ > {
2230
- fn clean ( & self , cx : & DocContext < ' _ > ) -> Vec < Item > {
2231
- let please_inline = self . vis . node . is_pub ( )
2232
- && self . attrs . iter ( ) . any ( |a| {
2233
- a. has_name ( sym:: doc)
2234
- && match a. meta_item_list ( ) {
2235
- Some ( l) => attr:: list_contains_name ( & l, sym:: inline) ,
2236
- None => false ,
2237
- }
2238
- } ) ;
2240
+ fn clean_extern_crate (
2241
+ item : & hir:: Item < ' _ > ,
2242
+ renamed : Option < Symbol > ,
2243
+ cx : & DocContext < ' _ > ,
2244
+ ) -> CleanedModule {
2245
+ let please_inline = item. vis . node . is_pub ( )
2246
+ && item. attrs . iter ( ) . any ( |a| {
2247
+ a. has_name ( sym:: doc)
2248
+ && match a. meta_item_list ( ) {
2249
+ Some ( l) => attr:: list_contains_name ( & l, sym:: inline) ,
2250
+ None => false ,
2251
+ }
2252
+ } ) ;
2253
+ let def_id = cx. tcx . hir ( ) . local_def_id ( item. hir_id ) . to_def_id ( ) ;
2254
+ let name = cx. tcx . item_name ( def_id) ;
2239
2255
2240
- if please_inline {
2241
- let mut visited = FxHashSet :: default ( ) ;
2256
+ if please_inline {
2257
+ let mut visited = FxHashSet :: default ( ) ;
2242
2258
2243
- let res = Res :: Def ( DefKind :: Mod , DefId { krate : self . cnum , index : CRATE_DEF_INDEX } ) ;
2259
+ let res = Res :: Def ( DefKind :: Mod , def_id ) ;
2244
2260
2245
- if let Some ( items) = inline:: try_inline (
2246
- cx,
2247
- cx. tcx . parent_module ( self . hir_id ) . to_def_id ( ) ,
2248
- res,
2249
- self . name ,
2250
- Some ( self . attrs ) ,
2251
- & mut visited,
2252
- ) {
2253
- return items;
2254
- }
2261
+ if let Some ( items) = inline:: try_inline (
2262
+ cx,
2263
+ cx. tcx . parent_module ( item. hir_id ) . to_def_id ( ) ,
2264
+ res,
2265
+ name,
2266
+ Some ( item. attrs ) ,
2267
+ & mut visited,
2268
+ ) {
2269
+ return CleanedModule :: Inlined ( items) ;
2255
2270
}
2256
-
2257
- vec ! [ Item {
2258
- name: None ,
2259
- attrs: self . attrs. clean( cx) ,
2260
- source: self . span. clean( cx) ,
2261
- def_id: DefId { krate: self . cnum, index: CRATE_DEF_INDEX } ,
2262
- visibility: self . vis. clean( cx) ,
2263
- stability: None ,
2264
- deprecation: None ,
2265
- kind: ExternCrateItem ( self . name. clean( cx) , self . path. clone( ) ) ,
2266
- } ]
2267
2271
}
2272
+
2273
+ CleanedModule :: Module ( ExternCrateItem ( name. clean ( cx) , renamed. clean ( cx) ) )
2268
2274
}
2269
2275
2270
2276
impl Clean < Vec < Item > > for doctree:: Import < ' _ > {
0 commit comments