1- //! Mostly copied from [bevy_utils ]
1+ //! Inspired by bevy's [disqualified ]
22//!
3- //! [bevy_utils ]: https://github.com/bevyengine/bevy /blob/main/crates/bevy_utils/ src/short_names .rs
3+ //! [disqualified ]: https://github.com/bevyengine/disqualified /blob/main/src/short_name .rs
44
55use std:: any:: type_name;
66
@@ -23,20 +23,24 @@ pub(crate) fn short_type_name<T: ?Sized>() -> String {
2323 while index < end_of_string {
2424 let rest_of_string = full_name. get ( index..end_of_string) . unwrap_or_default ( ) ;
2525
26- // Collapse everything up to the next special character,
27- // then skip over it
26+ // Collapse everything up to the next special character, then skip over it
2827 if let Some ( special_character_index) =
2928 rest_of_string. find ( |c : char | [ ' ' , '<' , '>' , '(' , ')' , '[' , ']' , ',' , ';' ] . contains ( & c) )
3029 {
3130 let segment_to_collapse = rest_of_string. get ( 0 ..special_character_index) . unwrap_or_default ( ) ;
3231 parsed_name += collapse_type_name ( segment_to_collapse) ;
3332 // Insert the special character
3433 let special_character = & rest_of_string[ special_character_index..=special_character_index] ;
35- parsed_name. push_str ( special_character) ;
34+ parsed_name += special_character;
35+
36+ // Remove lifetimes like <'_> or <'_, '_, ...>
37+ if parsed_name. ends_with ( "<'_>" ) || parsed_name. ends_with ( "<'_, " ) {
38+ _ = parsed_name. split_off ( parsed_name. len ( ) - 4 ) ;
39+ }
3640
3741 match special_character {
3842 ">" | ")" | "]" if rest_of_string[ special_character_index + 1 ..] . starts_with ( "::" ) => {
39- parsed_name. push_str ( "::" ) ;
43+ parsed_name += "::" ;
4044 // Move the index past the "::"
4145 index += special_character_index + 3 ;
4246 }
@@ -53,14 +57,18 @@ pub(crate) fn short_type_name<T: ?Sized>() -> String {
5357}
5458
5559#[ inline( always) ]
56- fn collapse_type_name ( string : & str ) -> & str {
57- string . rsplit ( "::" ) . next ( ) . unwrap ( )
60+ fn collapse_type_name ( segment : & str ) -> & str {
61+ segment . rsplit ( "::" ) . next ( ) . unwrap ( )
5862}
5963
6064#[ cfg( test) ]
6165mod tests {
6266 use super :: short_type_name;
6367 use std:: collections:: HashMap ;
68+ use std:: marker:: PhantomData ;
69+
70+ struct MyData < ' a , ' b > ( PhantomData < & ' a & ' b ( ) > ) ;
71+ struct MyDataT < ' a , T > ( PhantomData < & ' a T > ) ;
6472
6573 #[ test]
6674 fn tests ( ) {
@@ -73,5 +81,7 @@ mod tests {
7381 "HashMap<String, Option<[i32; 3]>>"
7482 ) ;
7583 assert_eq ! ( short_type_name:: <dyn Fn ( i32 ) -> i32 >( ) , "dyn Fn(i32) -> i32" ) ;
84+ assert_eq ! ( short_type_name:: <MyDataT <& str >>( ) , "MyDataT<&str>" ) ;
85+ assert_eq ! ( short_type_name:: <( & MyData , [ MyData ] ) >( ) , "(MyData, [MyData])" ) ;
7686 }
7787}
0 commit comments