1
1
//! This module contains the [`GetTypeDependencies`] trait and its implementations for various types.
2
2
3
3
use super :: {
4
- from:: { Mut , Ref , Union , Val } ,
5
- script_function:: FunctionCallContext ,
4
+ from:: { Union , Val } , script_function:: FunctionCallContext , DynamicScriptFunction , DynamicScriptFunctionMut , Mut , Ref
6
5
} ;
7
- use crate :: {
8
- bindings:: { ReflectReference , WorldGuard } ,
9
- error:: InteropError , private:: { no_type_dependencies, self_type_dependency_only} ,
10
- } ;
11
- use bevy:: reflect:: { FromReflect , GetTypeRegistration , TypeRegistration , TypeRegistry , Typed } ;
12
- use bevy_mod_scripting_derive:: { impl_get_type_dependencies, GetTypeDependencies } ;
13
- use std:: { collections:: HashMap , hash:: Hash } ;
6
+ use crate :: { bindings:: { ReflectReference , ScriptValue } , error:: InteropError } ;
7
+ use bevy:: reflect:: { FromReflect , GetTypeRegistration , TypeRegistry , Typed } ;
8
+ use bevy_mod_scripting_derive:: impl_get_type_dependencies;
9
+ use std:: { collections:: HashMap , ffi:: OsString , hash:: Hash , path:: PathBuf } ;
14
10
15
11
/// Functionally identical to [`GetTypeRegistration`] but without the 'static bound
16
12
pub trait GetTypeDependencies {
@@ -23,90 +19,153 @@ pub trait GetTypeDependencies {
23
19
}
24
20
25
21
26
- macro_rules! register_tuple_dependencies {
27
- ( $( $ty: ident ) ,* ) => {
28
- impl <$ ( $ty : GetTypeRegistration + Typed ) , * > GetTypeDependencies for ( $ ( $ty , ) * ) {
29
- fn register_type_dependencies ( registry : & mut TypeRegistry ) {
30
- $ (
31
- registry . register :: <$ty> ( ) ;
32
- ) *
33
- }
34
- }
22
+ macro_rules! impl_get_type_dependencies_primitives {
23
+ ( $( $ty: ty ) ,* ) => {
24
+ $ (
25
+ impl_get_type_dependencies! (
26
+ # [ derive ( GetTypeDependencies ) ]
27
+ # [ get_type_dependencies ( bms_core_path= "crate" ) ]
28
+ struct $ty { }
29
+ ) ;
30
+ ) *
35
31
} ;
36
32
}
37
33
34
+ impl_get_type_dependencies_primitives ! (
35
+ String , char , PathBuf , OsString ,
36
+ i8 , i16 , i32 , i64 , i128 , u8 , u16 , u32 , u64 , u128 , usize , isize , f32 , f64 , bool ,
37
+ DynamicScriptFunctionMut , DynamicScriptFunction ,
38
+ ScriptValue , InteropError
39
+ ) ;
38
40
39
- impl GetTypeDependencies for InteropError {
40
- type Underlying = Self ;
41
+ impl GetTypeDependencies for & ' static str {
42
+ type Underlying = & ' static str ;
43
+ fn register_type_dependencies ( registry : & mut TypeRegistry ) {
44
+ registry. register :: < & ' static str > ( ) ;
45
+ }
46
+ }
41
47
48
+ impl GetTypeDependencies for ( ) {
49
+ type Underlying = ( ) ;
42
50
fn register_type_dependencies ( registry : & mut TypeRegistry ) {
43
- todo ! ( )
51
+ registry . register :: < ( ) > ( ) ;
44
52
}
45
53
}
46
54
55
+ impl_get_type_dependencies ! (
56
+ #[ derive( GetTypeDependencies ) ]
57
+ #[ get_type_dependencies( bms_core_path="crate" ) ]
58
+ struct HashMap <K , V > where
59
+ K :: Underlying : FromReflect + Eq + Hash + Typed ,
60
+ V :: Underlying : FromReflect + Typed ,
61
+ { }
62
+ ) ;
47
63
64
+ impl_get_type_dependencies ! (
65
+ #[ derive( GetTypeDependencies ) ]
66
+ #[ get_type_dependencies( bms_core_path="crate" ) ]
67
+ struct Result <T , E > where
68
+ T :: Underlying : FromReflect + Typed ,
69
+ E :: Underlying : FromReflect + Typed ,
70
+ { }
71
+ ) ;
48
72
49
73
impl_get_type_dependencies ! (
50
74
#[ derive( GetTypeDependencies ) ]
51
- #[ get_type_dependencies( bms_core_path=crate ) ]
52
- struct HashMap < K , V > {
53
-
54
- }
75
+ #[ get_type_dependencies( bms_core_path=" crate" ) ]
76
+ struct Option < T > where
77
+ T :: Underlying : FromReflect + Typed ,
78
+ { }
55
79
) ;
56
80
81
+ impl_get_type_dependencies ! (
82
+ #[ derive( GetTypeDependencies ) ]
83
+ #[ get_type_dependencies( bms_core_path="crate" ) ]
84
+ struct Vec <T > where
85
+ T :: Underlying : FromReflect + Typed ,
86
+ { }
87
+ ) ;
57
88
58
- no_type_dependencies ! ( InteropError ) ;
59
- no_type_dependencies ! ( WorldGuard <' static >) ;
60
- self_type_dependency_only ! ( FunctionCallContext , ReflectReference ) ;
61
89
62
- recursive_type_dependencies ! (
63
- ( Val <T > where T : GetTypeRegistration ) ,
64
- ( Ref <' _, T > where T : GetTypeRegistration ) ,
65
- ( Mut <' _, T > where T : GetTypeRegistration ) ,
66
- // (Result<T, InteropError> where T: GetTypeRegistration),
67
- ( [ T ; N ] where T : GetTypeRegistration ; Typed , , const N : usize => with Self ) ,
68
- // (Option<T> where T: GetTypeRegistration;FromReflect;Typed => with Self),
69
- ( Vec <T > where T : GetTypeRegistration ; FromReflect ; Typed => with Self )
70
- // (HashMap<K,V> where K: GetTypeRegistration;FromReflect;Typed;Hash;Eq, V: GetTypeRegistration;FromReflect;Typed => with Self)
90
+ impl_get_type_dependencies ! (
91
+ #[ derive( GetTypeDependencies ) ]
92
+ #[ get_type_dependencies( bms_core_path="crate" , underlying="Result<T1::Underlying,T2::Underlying>" ) ]
93
+ struct Union <T1 , T2 > where
94
+ T1 :: Underlying : FromReflect + Typed ,
95
+ T2 :: Underlying : FromReflect + Typed ,
96
+ { }
71
97
) ;
72
98
99
+ // the below 3 types are "leaf" types, i.e. they cannot contain nested type dependencies
100
+ impl < T : GetTypeRegistration > GetTypeDependencies for Val < T > {
101
+ type Underlying = T ;
73
102
74
- impl < K > GetTypeDependencies for Option < K >
75
- where
76
- K : GetTypeDependencies
77
- {
78
103
fn register_type_dependencies ( registry : & mut TypeRegistry ) {
79
- K :: register_type_dependencies ( registry ) ;
104
+ registry . register :: < T > ( ) ;
80
105
}
81
106
}
82
107
83
- impl < Uk , Uv , K , V > GetTypeDependencies < HashMap < Uk , Uv > > for HashMap < K , V >
84
- where
85
- K : GetTypeDependencies < Uk > ,
86
- V : GetTypeDependencies < Uv > ,
87
- Uk : GetTypeRegistration + FromReflect + Eq + Hash + Typed ,
88
- Uv : GetTypeRegistration + FromReflect + Eq + Hash + Typed ,
89
- {
108
+ impl < T : GetTypeRegistration > GetTypeDependencies for Ref < ' _ , T > {
109
+ type Underlying = T ;
110
+
90
111
fn register_type_dependencies ( registry : & mut TypeRegistry ) {
91
- K :: register_type_dependencies ( registry) ;
92
- V :: register_type_dependencies ( registry) ;
93
- TypeRegistration :: of :: < HashMap < Uk , Uv > > ( ) ;
112
+ registry. register :: < T > ( ) ;
94
113
}
95
114
}
96
115
97
- impl < T : GetTypeDependencies > GetTypeDependencies for Result < T , InteropError > {
116
+ impl < T : GetTypeRegistration > GetTypeDependencies for Mut < ' _ , T > {
117
+ type Underlying = T ;
118
+
98
119
fn register_type_dependencies ( registry : & mut TypeRegistry ) {
99
- T :: register_type_dependencies ( registry ) ;
120
+ registry . register :: < T > ( ) ;
100
121
}
101
122
}
102
123
103
- impl < T1 : GetTypeDependencies , T2 : GetTypeDependencies > GetTypeDependencies for Union < T1 , T2 > {
124
+ impl_get_type_dependencies ! (
125
+ #[ derive( GetTypeDependencies ) ]
126
+ #[ get_type_dependencies( bms_core_path="crate" ) ]
127
+ struct ReflectReference where
128
+ { }
129
+ ) ;
130
+
131
+ impl_get_type_dependencies ! (
132
+ #[ derive( GetTypeDependencies ) ]
133
+ #[ get_type_dependencies( bms_core_path="crate" ) ]
134
+ struct FunctionCallContext where
135
+ { }
136
+ ) ;
137
+
138
+
139
+ impl < T , const N : usize > GetTypeDependencies for [ T ; N ] where
140
+ T : GetTypeDependencies ,
141
+ T :: Underlying : FromReflect + Typed ,
142
+ {
143
+ type Underlying = [ T :: Underlying ; N ] ;
104
144
fn register_type_dependencies ( registry : & mut TypeRegistry ) {
105
- T1 :: register_type_dependencies ( registry) ;
106
- T2 :: register_type_dependencies ( registry) ;
145
+ T :: register_type_dependencies ( registry) ;
107
146
}
108
147
}
109
148
149
+ macro_rules! register_tuple_dependencies {
150
+ ( $( $param: ident) ,* ) => {
151
+ impl <$( $param) ,* > $crate:: bindings:: GetTypeDependencies for ( $( $param, ) * ) where
152
+ $(
153
+ $param: GetTypeDependencies ,
154
+ <$param>:: Underlying : FromReflect + Typed + GetTypeRegistration ,
155
+ ) *
156
+ {
157
+ type Underlying = ( $( <$param as GetTypeDependencies >:: Underlying , ) * ) ;
158
+ fn register_type_dependencies( registry: & mut TypeRegistry ) {
159
+ $(
160
+ registry. register:: <<$param>:: Underlying >( ) ;
161
+ <$param>:: register_type_dependencies( registry) ;
162
+ ) *
163
+ }
164
+ }
165
+ } ;
166
+ }
167
+
168
+
110
169
bevy:: utils:: all_tuples!( register_tuple_dependencies, 1 , 14 , T ) ;
111
170
112
171
/// A trait collecting type dependency information for a whole function. Used to register everything used by a function with the type registry
@@ -117,8 +176,12 @@ pub trait GetFunctionTypeDependencies<Marker> {
117
176
118
177
macro_rules! impl_script_function_type_dependencies{
119
178
( $( $param: ident ) ,* ) => {
120
- impl <F , $( $param: GetTypeDependencies , ) * O : GetTypeDependencies > GetFunctionTypeDependencies <fn ( $( $param) ,* ) -> O > for F
121
- where F : Fn ( $( $param ) ,* ) -> O
179
+ impl <F , $( $param, ) * O > GetFunctionTypeDependencies <fn ( $( $param) ,* ) -> O > for F where
180
+ O : GetTypeDependencies ,
181
+ F : Fn ( $( $param ) ,* ) -> O ,
182
+ $(
183
+ $param: GetTypeDependencies ,
184
+ ) *
122
185
{
123
186
fn register_type_dependencies( registry: & mut TypeRegistry ) {
124
187
$(
0 commit comments