@@ -15,7 +15,7 @@ use rustc_index::vec::IndexVec;
1515use rustc_span:: hygiene:: ExpnId ;
1616use rustc_span:: symbol:: { kw, sym, Symbol } ;
1717
18- use std:: fmt:: Write ;
18+ use std:: fmt:: { self , Write } ;
1919use std:: hash:: Hash ;
2020use tracing:: debug;
2121
@@ -155,6 +155,23 @@ pub struct DisambiguatedDefPathData {
155155 pub disambiguator : u32 ,
156156}
157157
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+
158175#[ derive( Clone , Debug , Encodable , Decodable ) ]
159176pub struct DefPath {
160177 /// The path leading from the crate root to the item.
@@ -202,35 +219,7 @@ impl DefPath {
202219 let mut s = String :: with_capacity ( self . data . len ( ) * 16 ) ;
203220
204221 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 ( ) ;
234223 }
235224
236225 s
@@ -246,13 +235,9 @@ impl DefPath {
246235 for component in & self . data {
247236 s. extend ( opt_delimiter) ;
248237 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 ( ) ;
255239 }
240+
256241 s
257242 }
258243}
@@ -465,11 +450,13 @@ impl DefPathData {
465450 ImplTrait => DefPathDataName :: Anon { namespace : sym:: opaque } ,
466451 }
467452 }
453+ }
468454
469- pub fn to_string ( & self ) -> String {
455+ impl fmt:: Display for DefPathData {
456+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
470457 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) ,
473460 }
474461 }
475462}
0 commit comments