@@ -32,24 +32,8 @@ pub struct JsonRenderer {
32
32
}
33
33
34
34
impl JsonRenderer {
35
- /// Inserts an item into the index. This should be used rather than directly calling insert on
36
- /// the hashmap because certain items (traits and types) need to have their mappings for trait
37
- /// implementations filled out before they're inserted.
38
- fn insert ( & self , item : clean:: Item , cache : & Cache ) {
39
- let id = item. def_id ;
40
- let mut new_item: types:: Item = item. into ( ) ;
41
- if let types:: ItemEnum :: TraitItem ( ref mut t) = new_item. inner {
42
- t. implementors = self . get_trait_implementors ( id, cache)
43
- } else if let types:: ItemEnum :: StructItem ( ref mut s) = new_item. inner {
44
- s. impls = self . get_impls ( id, cache)
45
- } else if let types:: ItemEnum :: EnumItem ( ref mut e) = new_item. inner {
46
- e. impls = self . get_impls ( id, cache)
47
- }
48
- self . index . borrow_mut ( ) . insert ( id. into ( ) , new_item) ;
49
- }
50
-
51
35
fn get_trait_implementors (
52
- & self ,
36
+ & mut self ,
53
37
id : rustc_span:: def_id:: DefId ,
54
38
cache : & Cache ,
55
39
) -> Vec < types:: Id > {
@@ -61,15 +45,15 @@ impl JsonRenderer {
61
45
. iter ( )
62
46
. map ( |i| {
63
47
let item = & i. impl_item ;
64
- self . insert ( item. clone ( ) , cache) ;
48
+ self . item ( item. clone ( ) , cache) . unwrap ( ) ;
65
49
item. def_id . into ( )
66
50
} )
67
51
. collect ( )
68
52
} )
69
53
. unwrap_or_default ( )
70
54
}
71
55
72
- fn get_impls ( & self , id : rustc_span:: def_id:: DefId , cache : & Cache ) -> Vec < types:: Id > {
56
+ fn get_impls ( & mut self , id : rustc_span:: def_id:: DefId , cache : & Cache ) -> Vec < types:: Id > {
73
57
cache
74
58
. impls
75
59
. get ( & id)
@@ -79,7 +63,7 @@ impl JsonRenderer {
79
63
. filter_map ( |i| {
80
64
let item = & i. impl_item ;
81
65
if item. def_id . is_local ( ) {
82
- self . insert ( item. clone ( ) , cache) ;
66
+ self . item ( item. clone ( ) , cache) . unwrap ( ) ;
83
67
Some ( item. def_id . into ( ) )
84
68
} else {
85
69
None
@@ -90,14 +74,14 @@ impl JsonRenderer {
90
74
. unwrap_or_default ( )
91
75
}
92
76
93
- fn get_trait_items ( & self , cache : & Cache ) -> Vec < ( types:: Id , types:: Item ) > {
77
+ fn get_trait_items ( & mut self , cache : & Cache ) -> Vec < ( types:: Id , types:: Item ) > {
94
78
cache
95
79
. traits
96
80
. iter ( )
97
81
. filter_map ( |( id, trait_item) | {
98
82
// only need to synthesize items for external traits
99
83
if !id. is_local ( ) {
100
- trait_item. items . clone ( ) . into_iter ( ) . for_each ( |i| self . insert ( i, cache) ) ;
84
+ trait_item. items . clone ( ) . into_iter ( ) . for_each ( |i| self . item ( i, cache) . unwrap ( ) ) ;
101
85
Some ( (
102
86
( * id) . into ( ) ,
103
87
types:: Item {
@@ -150,10 +134,24 @@ impl FormatRenderer for JsonRenderer {
150
134
) )
151
135
}
152
136
137
+ /// Inserts an item into the index. This should be used rather than directly calling insert on
138
+ /// the hashmap because certain items (traits and types) need to have their mappings for trait
139
+ /// implementations filled out before they're inserted.
153
140
fn item ( & mut self , item : clean:: Item , cache : & Cache ) -> Result < ( ) , Error > {
154
- // Flatten items that recursively store other items by inserting them into the index
141
+ // Flatten items that recursively store other items
155
142
item. inner . inner_items ( ) . for_each ( |i| self . item ( i. clone ( ) , cache) . unwrap ( ) ) ;
156
- self . insert ( item. clone ( ) , cache) ;
143
+
144
+ let id = item. def_id ;
145
+ let mut new_item: types:: Item = item. into ( ) ;
146
+ if let types:: ItemEnum :: TraitItem ( ref mut t) = new_item. inner {
147
+ t. implementors = self . get_trait_implementors ( id, cache)
148
+ } else if let types:: ItemEnum :: StructItem ( ref mut s) = new_item. inner {
149
+ s. impls = self . get_impls ( id, cache)
150
+ } else if let types:: ItemEnum :: EnumItem ( ref mut e) = new_item. inner {
151
+ e. impls = self . get_impls ( id, cache)
152
+ }
153
+
154
+ self . index . borrow_mut ( ) . insert ( id. into ( ) , new_item) ;
157
155
Ok ( ( ) )
158
156
}
159
157
@@ -166,15 +164,18 @@ impl FormatRenderer for JsonRenderer {
166
164
use clean:: types:: ItemEnum :: * ;
167
165
if let ModuleItem ( m) = & item. inner {
168
166
for item in & m. items {
169
- match item. inner {
167
+ match & item. inner {
170
168
// These don't have names so they don't get added to the output by default
171
- ImportItem ( _) => self . insert ( item. clone ( ) , cache) ,
172
- ExternCrateItem ( _, _) => self . insert ( item. clone ( ) , cache) ,
169
+ ImportItem ( _) => self . item ( item. clone ( ) , cache) . unwrap ( ) ,
170
+ ExternCrateItem ( _, _) => self . item ( item. clone ( ) , cache) . unwrap ( ) ,
171
+ ImplItem ( i) => {
172
+ i. items . iter ( ) . for_each ( |i| self . item ( i. clone ( ) , cache) . unwrap ( ) )
173
+ }
173
174
_ => { }
174
175
}
175
176
}
176
177
}
177
- self . insert ( item. clone ( ) , cache) ;
178
+ self . item ( item. clone ( ) , cache) . unwrap ( ) ;
178
179
Ok ( ( ) )
179
180
}
180
181
0 commit comments