@@ -15,7 +15,7 @@ use rustc_index::vec::IndexVec;
15
15
use rustc_span:: hygiene:: ExpnId ;
16
16
use rustc_span:: symbol:: { kw, sym, Symbol } ;
17
17
18
- use std:: fmt:: Write ;
18
+ use std:: fmt:: { self , Write } ;
19
19
use std:: hash:: Hash ;
20
20
use tracing:: debug;
21
21
@@ -155,6 +155,23 @@ pub struct DisambiguatedDefPathData {
155
155
pub disambiguator : u32 ,
156
156
}
157
157
158
+ impl fmt:: Display for DisambiguatedDefPathData {
159
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
160
+ match self . data . get_name ( ) {
161
+ DefPathDataName :: Named ( name) => {
162
+ if self . disambiguator == 0 {
163
+ f. write_str ( & name. as_str ( ) )
164
+ } else {
165
+ write ! ( f, "{}#{}" , name, self . disambiguator)
166
+ }
167
+ }
168
+ DefPathDataName :: Anon { namespace } => {
169
+ write ! ( f, "{{{}#{}}}" , namespace, self . disambiguator)
170
+ }
171
+ }
172
+ }
173
+ }
174
+
158
175
#[ derive( Clone , Debug , Encodable , Decodable ) ]
159
176
pub struct DefPath {
160
177
/// The path leading from the crate root to the item.
@@ -202,35 +219,7 @@ impl DefPath {
202
219
let mut s = String :: with_capacity ( self . data . len ( ) * 16 ) ;
203
220
204
221
for component in & self . data {
205
- match component. data . get_name ( ) {
206
- DefPathDataName :: Named ( name) => write ! ( s, "::{}" , name) . unwrap ( ) ,
207
- DefPathDataName :: Anon { namespace } => {
208
- write ! ( s, "::{{{}#{}}}" , namespace, component. disambiguator) . unwrap ( )
209
- }
210
- }
211
- }
212
-
213
- s
214
- }
215
-
216
- /// Returns a filename-friendly string for the `DefPath`, with the
217
- /// crate-prefix.
218
- pub fn to_string_friendly < F > ( & self , crate_imported_name : F ) -> String
219
- where
220
- F : FnOnce ( CrateNum ) -> Symbol ,
221
- {
222
- let crate_name_str = crate_imported_name ( self . krate ) . as_str ( ) ;
223
- let mut s = String :: with_capacity ( crate_name_str. len ( ) + self . data . len ( ) * 16 ) ;
224
-
225
- write ! ( s, "::{}" , crate_name_str) . unwrap ( ) ;
226
-
227
- for component in & self . data {
228
- match component. data . get_name ( ) {
229
- DefPathDataName :: Named ( name) => write ! ( s, "::{}" , name) . unwrap ( ) ,
230
- DefPathDataName :: Anon { namespace } => {
231
- write ! ( s, "{{{}#{}}}" , namespace, component. disambiguator) . unwrap ( )
232
- }
233
- }
222
+ write ! ( s, "::{}" , component) . unwrap ( ) ;
234
223
}
235
224
236
225
s
@@ -246,13 +235,9 @@ impl DefPath {
246
235
for component in & self . data {
247
236
s. extend ( opt_delimiter) ;
248
237
opt_delimiter = Some ( '-' ) ;
249
- match component. data . get_name ( ) {
250
- DefPathDataName :: Named ( name) => write ! ( s, "{}" , name) . unwrap ( ) ,
251
- DefPathDataName :: Anon { namespace } => {
252
- write ! ( s, "{{{}#{}}}" , namespace, component. disambiguator) . unwrap ( )
253
- }
254
- }
238
+ write ! ( s, "{}" , component) . unwrap ( ) ;
255
239
}
240
+
256
241
s
257
242
}
258
243
}
@@ -465,11 +450,13 @@ impl DefPathData {
465
450
ImplTrait => DefPathDataName :: Anon { namespace : sym:: opaque } ,
466
451
}
467
452
}
453
+ }
468
454
469
- pub fn to_string ( & self ) -> String {
455
+ impl fmt:: Display for DefPathData {
456
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
470
457
match self . get_name ( ) {
471
- DefPathDataName :: Named ( name) => name. to_string ( ) ,
472
- DefPathDataName :: Anon { namespace } => format ! ( "{{{{{}}}}}" , namespace) ,
458
+ DefPathDataName :: Named ( name) => f . write_str ( & name. as_str ( ) ) ,
459
+ DefPathDataName :: Anon { namespace } => write ! ( f , "{{{{{}}}}}" , namespace) ,
473
460
}
474
461
}
475
462
}
0 commit comments