@@ -249,6 +249,40 @@ impl<'tcx> ImplOrTraitItem<'tcx> {
249249 _ => None ,
250250 }
251251 }
252+
253+ pub fn signature < ' a > ( & self , tcx : TyCtxt < ' a , ' tcx , ' tcx > ) -> String {
254+ match * self {
255+ MethodTraitItem ( ref item) => {
256+ match tcx. map . get_if_local ( item. def_id ) {
257+ Some ( node) => {
258+ match node. span ( ) {
259+ Some ( span) => match tcx. sess . codemap ( ) . span_to_oneline_snippet ( span) {
260+ Ok ( snippet) => snippet,
261+ Err ( _) => item. signature ( ) ,
262+ } ,
263+ None => item. signature ( ) ,
264+ }
265+ }
266+ None => item. signature ( ) ,
267+ }
268+ }
269+ TypeTraitItem ( ref item) => item. signature ( ) ,
270+ ConstTraitItem ( ref item) => {
271+ match tcx. map . get_if_local ( item. def_id ) {
272+ Some ( node) => {
273+ match node. span ( ) {
274+ Some ( span) => match tcx. sess . codemap ( ) . span_to_oneline_snippet ( span) {
275+ Ok ( snippet) => snippet,
276+ Err ( _) => item. signature ( ) ,
277+ } ,
278+ None => item. signature ( ) ,
279+ }
280+ }
281+ None => item. signature ( ) ,
282+ }
283+ }
284+ }
285+ }
252286}
253287
254288#[ derive( Clone , Copy , Debug ) ]
@@ -380,6 +414,34 @@ impl<'tcx> Method<'tcx> {
380414 ImplContainer ( id) => id,
381415 }
382416 }
417+
418+ pub fn signature ( & self ) -> String {
419+ let name = self . name . to_string ( ) ;
420+ let unsafety = match self . fty . unsafety {
421+ hir:: Unsafety :: Unsafe => "unsafe " ,
422+ hir:: Unsafety :: Normal => "" ,
423+ } ;
424+ let has_gen_types = !self . generics . types . is_empty ( ) ;
425+ let type_args = if has_gen_types {
426+ format ! ( "<{}>" , self . generics. types. clone( ) . into_iter( )
427+ . map( |t| t. name. as_str( ) . to_string( ) )
428+ . collect:: <Vec <String >>( )
429+ . join( ", " ) )
430+ } else {
431+ String :: new ( )
432+ } ;
433+ let args = self . fty . sig . inputs ( ) . 0 . iter ( )
434+ . map ( |t| format ! ( "{:?}" , t) ) . collect :: < Vec < _ > > ( ) . join ( ", " ) ;
435+ let return_type = format ! ( "{:?}" , self . fty. sig. output( ) . 0 ) ;
436+ let return_signature = if & return_type == "()" {
437+ "" . to_string ( )
438+ } else {
439+ format ! ( " -> {}" , return_type)
440+ } ;
441+
442+ // unsafe fn name<'a, T>(args) -> ReturnType
443+ format ! ( "{}fn {}{}({}){};" , unsafety, name, type_args, args, return_signature)
444+ }
383445}
384446
385447impl < ' tcx > PartialEq for Method < ' tcx > {
@@ -407,6 +469,18 @@ pub struct AssociatedConst<'tcx> {
407469 pub has_value : bool
408470}
409471
472+ impl < ' tcx > AssociatedConst < ' tcx > {
473+ pub fn signature ( & self ) -> String {
474+ // const FOO: Type = DEFAULT;
475+ let value = if self . has_value {
476+ " = <DEFAULT>"
477+ } else {
478+ ""
479+ } ;
480+ format ! ( "const {}: {:?}{};" , self . name. to_string( ) , self . ty, value)
481+ }
482+ }
483+
410484#[ derive( Clone , Copy , Debug ) ]
411485pub struct AssociatedType < ' tcx > {
412486 pub name : Name ,
@@ -417,6 +491,13 @@ pub struct AssociatedType<'tcx> {
417491 pub container : ImplOrTraitItemContainer ,
418492}
419493
494+ impl < ' tcx > AssociatedType < ' tcx > {
495+ pub fn signature ( & self ) -> String {
496+ //// type Type;
497+ format ! ( "type {};" , self . name. to_string( ) )
498+ }
499+ }
500+
420501#[ derive( Clone , PartialEq , RustcDecodable , RustcEncodable , Copy ) ]
421502pub enum Variance {
422503 Covariant , // T<A> <: T<B> iff A <: B -- e.g., function return type
0 commit comments