1
- use syn:: { spanned:: Spanned , FnArg , ImplItem , ItemImpl , Pat , PatIdent , Signature , Type } ;
1
+ use syn:: { spanned:: Spanned , FnArg , Generics , ImplItem , ItemImpl , Pat , PatIdent , Signature , Type } ;
2
2
3
3
use proc_macro2:: TokenStream as TokenStream2 ;
4
4
use quote:: { quote, ToTokens } ;
@@ -330,6 +330,7 @@ pub(crate) struct ExportArgs {
330
330
pub ( crate ) fn derive_methods ( item_impl : ItemImpl ) -> TokenStream2 {
331
331
let derived = crate :: automatically_derived ( ) ;
332
332
let ( impl_block, export) = impl_gdnative_expose ( item_impl) ;
333
+ let ( impl_generics, _, where_clause) = impl_block. generics . split_for_impl ( ) ;
333
334
334
335
let class_name = export. class_ty ;
335
336
@@ -390,7 +391,7 @@ pub(crate) fn derive_methods(item_impl: ItemImpl) -> TokenStream2 {
390
391
quote_spanned ! ( ret_span=>)
391
392
} ;
392
393
393
- let method = wrap_method ( & class_name, & export_method)
394
+ let method = wrap_method ( & class_name, & impl_block . generics , & export_method)
394
395
. unwrap_or_else ( |err| err. to_compile_error ( ) ) ;
395
396
396
397
quote_spanned ! ( sig_span=>
@@ -410,7 +411,7 @@ pub(crate) fn derive_methods(item_impl: ItemImpl) -> TokenStream2 {
410
411
#impl_block
411
412
412
413
#derived
413
- impl gdnative:: export:: NativeClassMethods for #class_name {
414
+ impl #impl_generics gdnative:: export:: NativeClassMethods for #class_name #where_clause {
414
415
fn nativeclass_register( #builder: & :: gdnative:: export:: ClassBuilder <Self >) {
415
416
use gdnative:: export:: * ;
416
417
@@ -767,11 +768,17 @@ pub(crate) fn expand_godot_wrap_method(
767
768
return Err ( errors) ;
768
769
}
769
770
770
- wrap_method ( & class_name, & export_method. expect ( "ExportMethod is valid" ) ) . map_err ( |e| vec ! [ e] )
771
+ wrap_method (
772
+ & class_name,
773
+ & Generics :: default ( ) ,
774
+ & export_method. expect ( "ExportMethod is valid" ) ,
775
+ )
776
+ . map_err ( |e| vec ! [ e] )
771
777
}
772
778
773
779
fn wrap_method (
774
780
class_name : & Type ,
781
+ generics : & Generics ,
775
782
export_method : & ExportMethod ,
776
783
) -> Result < TokenStream2 , syn:: Error > {
777
784
let ExportMethod {
@@ -783,6 +790,21 @@ fn wrap_method(
783
790
let gdnative_core = crate :: crate_gdnative_core ( ) ;
784
791
let automatically_derived = crate :: automatically_derived ( ) ;
785
792
793
+ let ( impl_generics, ty_generics, where_clause) = generics. split_for_impl ( ) ;
794
+ let turbofish_ty_generics = ty_generics. as_turbofish ( ) ;
795
+
796
+ let generic_marker_decl = if generics. params . is_empty ( ) {
797
+ quote ! ( ( ) )
798
+ } else {
799
+ quote ! ( core:: marker:: PhantomData #ty_generics)
800
+ } ;
801
+
802
+ let generic_marker_ctor = if generics. params . is_empty ( ) {
803
+ quote ! ( ( ) )
804
+ } else {
805
+ quote ! ( core:: marker:: PhantomData )
806
+ } ;
807
+
786
808
let sig_span = sig. ident . span ( ) ;
787
809
let ret_span = sig. output . span ( ) ;
788
810
@@ -875,8 +897,8 @@ fn wrap_method(
875
897
876
898
quote_spanned ! { sig_span =>
877
899
#automatically_derived
878
- impl #gdnative_async:: StaticArgsAsyncMethod <#class_name> for ThisMethod {
879
- type Args = Args ;
900
+ impl #impl_generics # gdnative_async:: StaticArgsAsyncMethod <#class_name> for ThisMethod #ty_generics #where_clause {
901
+ type Args = Args #ty_generics ;
880
902
881
903
fn spawn_with(
882
904
& self ,
@@ -885,7 +907,7 @@ fn wrap_method(
885
907
__spawner. spawn( move |__ctx, __this, __args| {
886
908
let __future = __this
887
909
. #map_method( move |__rust_val, __base| {
888
- let Args { #( #destructure_arg_list, ) * } = __args;
910
+ let Args { #( #destructure_arg_list, ) * __generic_marker } = __args;
889
911
890
912
#[ allow( unused_unsafe) ]
891
913
unsafe {
@@ -916,17 +938,19 @@ fn wrap_method(
916
938
}
917
939
}
918
940
919
- #gdnative_async:: Async :: new( #gdnative_async:: StaticArgs :: new( ThisMethod ) )
941
+ #gdnative_async:: Async :: new( #gdnative_async:: StaticArgs :: new( ThisMethod #turbofish_ty_generics {
942
+ _marker: #generic_marker_ctor,
943
+ } ) )
920
944
}
921
945
} else {
922
946
quote_spanned ! { sig_span =>
923
947
#automatically_derived
924
- impl #gdnative_core:: export:: StaticArgsMethod <#class_name> for ThisMethod {
925
- type Args = Args ;
948
+ impl #impl_generics # gdnative_core:: export:: StaticArgsMethod <#class_name> for ThisMethod #ty_generics #where_clause {
949
+ type Args = Args #ty_generics ;
926
950
fn call(
927
951
& self ,
928
952
__this: TInstance <' _, #class_name, #gdnative_core:: object:: ownership:: Shared >,
929
- Args { #( #destructure_arg_list, ) * } : Args ,
953
+ Args { #( #destructure_arg_list, ) * __generic_marker } : Self :: Args ,
930
954
) -> #gdnative_core:: core_types:: Variant {
931
955
__this
932
956
. #map_method( |__rust_val, __base| {
@@ -950,23 +974,48 @@ fn wrap_method(
950
974
}
951
975
}
952
976
953
- #gdnative_core:: export:: StaticArgs :: new( ThisMethod )
977
+ #gdnative_core:: export:: StaticArgs :: new( ThisMethod #turbofish_ty_generics {
978
+ _marker: #generic_marker_ctor,
979
+ } )
954
980
}
955
981
} ;
956
982
983
+ // Necessary standard traits have to be implemented manually because the default derive isn't smart enough.
957
984
let output = quote_spanned ! { sig_span =>
958
985
{
959
- #[ derive( Copy , Clone , Default ) ]
960
- struct ThisMethod ;
986
+ struct ThisMethod #ty_generics #where_clause {
987
+ _marker: #generic_marker_decl,
988
+ }
989
+
990
+ impl #impl_generics Copy for ThisMethod #ty_generics #where_clause { }
991
+ impl #impl_generics Clone for ThisMethod #ty_generics #where_clause {
992
+ fn clone( & self ) -> Self {
993
+ * self
994
+ }
995
+ }
996
+
997
+ impl #impl_generics Default for ThisMethod #ty_generics #where_clause {
998
+ fn default ( ) -> Self {
999
+ Self {
1000
+ _marker: #generic_marker_ctor,
1001
+ }
1002
+ }
1003
+ }
1004
+
1005
+ unsafe impl #impl_generics Send for ThisMethod #ty_generics #where_clause { }
1006
+ unsafe impl #impl_generics Sync for ThisMethod #ty_generics #where_clause { }
961
1007
962
1008
use #gdnative_core:: export:: { NativeClass , OwnerArg } ;
963
1009
use #gdnative_core:: object:: { Instance , TInstance } ;
964
1010
use #gdnative_core:: derive:: FromVarargs ;
965
1011
966
1012
#[ derive( FromVarargs ) ]
967
1013
#automatically_derived
968
- struct Args {
1014
+ struct Args #ty_generics #where_clause {
969
1015
#( #declare_arg_list, ) *
1016
+
1017
+ #[ skip]
1018
+ __generic_marker: #generic_marker_decl,
970
1019
}
971
1020
972
1021
#impl_body
0 commit comments