@@ -4,38 +4,48 @@ use quote::{format_ident, quote};
44use syn:: { Data , DataEnum , DataStruct , DeriveInput , Error , Fields , Result } ;
55
66pub fn derive ( input : & DeriveInput ) -> Result < TokenStream > {
7- match & input. data {
7+ let impls = match & input. data {
88 Data :: Struct ( data) => impl_struct ( input, data) ,
99 Data :: Enum ( data) => impl_enum ( input, data) ,
1010 Data :: Union ( _) => Err ( Error :: new_spanned ( input, "Unions are not supported" ) ) ,
11- }
11+ } ?;
12+
13+ let helpers = specialization ( ) ;
14+ let dummy_const = format_ident ! ( "_DERIVE_Display_FOR_{}" , input. ident) ;
15+ Ok ( quote ! {
16+ #[ allow( non_upper_case_globals, unused_attributes, unused_qualifications) ]
17+ const #dummy_const: ( ) = {
18+ #helpers
19+ #impls
20+ } ;
21+ } )
1222}
1323
1424#[ cfg( feature = "std" ) ]
1525fn specialization ( ) -> TokenStream {
1626 quote ! {
1727 trait DisplayToDisplayDoc {
18- fn get_display ( & self ) -> Self ;
28+ fn __displaydoc_display ( & self ) -> & Self ;
1929 }
2030
21- impl <T : core:: fmt:: Display > DisplayToDisplayDoc for & T {
22- fn get_display ( & self ) -> Self {
31+ impl <T : core:: fmt:: Display > DisplayToDisplayDoc for T {
32+ fn __displaydoc_display ( & self ) -> & Self {
2333 self
2434 }
2535 }
2636
2737 trait PathToDisplayDoc {
28- fn get_display ( & self ) -> std:: path:: Display <' _>;
38+ fn __displaydoc_display ( & self ) -> std:: path:: Display <' _>;
2939 }
3040
3141 impl PathToDisplayDoc for std:: path:: Path {
32- fn get_display ( & self ) -> std:: path:: Display <' _> {
42+ fn __displaydoc_display ( & self ) -> std:: path:: Display <' _> {
3343 self . display( )
3444 }
3545 }
3646
3747 impl PathToDisplayDoc for std:: path:: PathBuf {
38- fn get_display ( & self ) -> std:: path:: Display <' _> {
48+ fn __displaydoc_display ( & self ) -> std:: path:: Display <' _> {
3949 self . display( )
4050 }
4151 }
@@ -74,13 +84,7 @@ fn impl_struct(input: &DeriveInput, data: &DataStruct) -> Result<TokenStream> {
7484 }
7585 } ) ;
7686
77- let needed_traits = specialization ( ) ;
78-
79- Ok ( quote ! {
80- #needed_traits
81-
82- #display
83- } )
87+ Ok ( quote ! { #display } )
8488}
8589
8690fn impl_enum ( input : & DeriveInput , data : & DataEnum ) -> Result < TokenStream > {
@@ -93,7 +97,7 @@ fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
9397 . map ( |variant| attr:: display ( & variant. attrs ) )
9498 . collect :: < Result < Vec < _ > > > ( ) ?;
9599
96- let display = if displays. iter ( ) . any ( Option :: is_some) {
100+ if displays. iter ( ) . any ( Option :: is_some) {
97101 let arms = data
98102 . variants
99103 . iter ( )
@@ -115,7 +119,7 @@ fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
115119 } )
116120 } )
117121 . collect :: < Result < Vec < _ > > > ( ) ?;
118- Some ( quote ! {
122+ Ok ( quote ! {
119123 impl #impl_generics core:: fmt:: Display for #ty #ty_generics #where_clause {
120124 fn fmt( & self , formatter: & mut core:: fmt:: Formatter ) -> core:: fmt:: Result {
121125 #[ allow( unused_variables) ]
@@ -126,14 +130,6 @@ fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
126130 }
127131 } )
128132 } else {
129- return Err ( Error :: new_spanned ( input, "Missing doc comments" ) ) ;
130- } ;
131-
132- let needed_traits = specialization ( ) ;
133-
134- Ok ( quote ! {
135- #needed_traits
136-
137- #display
138- } )
133+ Err ( Error :: new_spanned ( input, "Missing doc comments" ) )
134+ }
139135}
0 commit comments