Skip to content

Commit 97da91a

Browse files
committed
refactor
1 parent 623b64d commit 97da91a

File tree

3 files changed

+59
-53
lines changed

3 files changed

+59
-53
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
function on_test()
33
local my_type = types.TestResource;
4-
assert(my_type ~= nil, "Type t.TestResource is not available");
4+
assert(my_type ~= nil, "Type TestResource is not available in type cache");
55
assert(my_type:short_name() == "TestResource", "Type t.TestResource:short_name() is not correct: " .. my_type:short_name());
66
end

crates/bevy_mod_scripting_core/src/bindings/function/type_dependencies.rs

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,33 +57,29 @@ impl_get_type_dependencies!(
5757
#[get_type_dependencies(bms_core_path="crate")]
5858
struct HashMap<K,V> where
5959
K::Underlying: FromReflect + Eq + Hash + Typed,
60-
V::Underlying: FromReflect + Typed,
61-
{}
60+
V::Underlying: FromReflect + Typed {}
6261
);
6362

6463
impl_get_type_dependencies!(
6564
#[derive(GetTypeDependencies)]
6665
#[get_type_dependencies(bms_core_path="crate")]
6766
struct Result<T, E> where
6867
T::Underlying: FromReflect + Typed,
69-
E::Underlying: FromReflect + Typed,
70-
{}
68+
E::Underlying: FromReflect + Typed {}
7169
);
7270

7371
impl_get_type_dependencies!(
7472
#[derive(GetTypeDependencies)]
7573
#[get_type_dependencies(bms_core_path="crate")]
7674
struct Option<T> where
77-
T::Underlying: FromReflect + Typed,
78-
{}
75+
T::Underlying: FromReflect + Typed {}
7976
);
8077

8178
impl_get_type_dependencies!(
8279
#[derive(GetTypeDependencies)]
8380
#[get_type_dependencies(bms_core_path="crate")]
8481
struct Vec<T> where
85-
T::Underlying: FromReflect + Typed,
86-
{}
82+
T::Underlying: FromReflect + Typed {}
8783
);
8884

8985

@@ -92,47 +88,37 @@ impl_get_type_dependencies!(
9288
#[get_type_dependencies(bms_core_path="crate", underlying="Result<T1::Underlying,T2::Underlying>")]
9389
struct Union<T1, T2> where
9490
T1::Underlying: FromReflect + Typed,
95-
T2::Underlying: FromReflect + Typed,
96-
{}
91+
T2::Underlying: FromReflect + Typed {}
9792
);
9893

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;
102-
103-
fn register_type_dependencies(registry: &mut TypeRegistry) {
104-
registry.register::<T>();
105-
}
106-
}
107-
108-
impl <T: GetTypeRegistration> GetTypeDependencies for Ref<'_, T> {
109-
type Underlying = T;
110-
111-
fn register_type_dependencies(registry: &mut TypeRegistry) {
112-
registry.register::<T>();
113-
}
114-
}
94+
impl_get_type_dependencies!(
95+
#[derive(GetTypeDependencies)]
96+
#[get_type_dependencies(bms_core_path="crate", underlying="T", dont_recurse)]
97+
struct Val<T> {}
98+
);
11599

116-
impl <T: GetTypeRegistration> GetTypeDependencies for Mut<'_, T> {
117-
type Underlying = T;
100+
impl_get_type_dependencies!(
101+
#[derive(GetTypeDependencies)]
102+
#[get_type_dependencies(bms_core_path="crate", underlying="T", dont_recurse)]
103+
struct Ref<'a, T> {}
104+
);
118105

119-
fn register_type_dependencies(registry: &mut TypeRegistry) {
120-
registry.register::<T>();
121-
}
122-
}
106+
impl_get_type_dependencies!(
107+
#[derive(GetTypeDependencies)]
108+
#[get_type_dependencies(bms_core_path="crate", underlying="T", dont_recurse)]
109+
struct Mut<'a, T> {}
110+
);
123111

124112
impl_get_type_dependencies!(
125113
#[derive(GetTypeDependencies)]
126114
#[get_type_dependencies(bms_core_path="crate")]
127-
struct ReflectReference where
128-
{}
115+
struct ReflectReference where {}
129116
);
130117

131118
impl_get_type_dependencies!(
132119
#[derive(GetTypeDependencies)]
133120
#[get_type_dependencies(bms_core_path="crate")]
134-
struct FunctionCallContext where
135-
{}
121+
struct FunctionCallContext where {}
136122
);
137123

138124

crates/bevy_mod_scripting_derive/src/derive/get_type_dependencies.rs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,7 @@ fn get_type_dependencies_from_input(derive_input: DeriveInput) -> TokenStream {
4545

4646
let (impl_generics, type_generics, impl_where) = derive_input.generics.split_for_impl();
4747

48-
let mut impl_where: WhereClause = impl_where.cloned().unwrap_or_else(|| parse_quote!{where});
49-
for param in derive_input.generics.type_params() {
50-
let param_name = &param.ident;
51-
impl_where.predicates.push(
52-
parse_quote_spanned!(param.ident.span()=> #param_name: GetTypeDependencies),
53-
);
54-
impl_where.predicates.push(
55-
parse_quote_spanned!(param.ident.span()=> #param_name::Underlying: bevy::reflect::GetTypeRegistration),
56-
);
57-
}
58-
48+
5949
let name = &derive_input.ident;
6050

6151

@@ -76,15 +66,40 @@ fn get_type_dependencies_from_input(derive_input: DeriveInput) -> TokenStream {
7666
#name #type_generics_underlying
7767
}
7868
};
69+
70+
let mut impl_where: WhereClause = impl_where.cloned().unwrap_or_else(|| parse_quote!{where});
71+
let mut recursive_registrations = Vec::default();
72+
for param in derive_input.generics.type_params() {
73+
let param_name = &param.ident;
74+
if !args.dont_recurse {
75+
impl_where.predicates.push(
76+
parse_quote_spanned!(param.ident.span()=> #param_name: GetTypeDependencies),
77+
);
78+
recursive_registrations.push(
79+
quote_spanned! {param.ident.span()=>
80+
<#param_name as GetTypeDependencies>::register_type_dependencies(registry);
81+
}
82+
);
83+
84+
impl_where.predicates.push(
85+
parse_quote_spanned!(param.ident.span()=> #param_name::Underlying: bevy::reflect::GetTypeRegistration),
86+
);
87+
} else {
88+
impl_where.predicates.push(
89+
parse_quote_spanned!(param.ident.span()=> #param_name: bevy::reflect::GetTypeRegistration),
90+
)
91+
}
92+
}
93+
7994

8095
quote_spanned! {derive_input.ident.span()=>
96+
#[automatically_derived]
97+
#[allow(clippy::needless_lifetimes)]
8198
impl #impl_generics #bms_core::bindings::GetTypeDependencies for #name #type_generics #impl_where
8299
{
83100
type Underlying = #underlying;
84101
fn register_type_dependencies(registry: &mut bevy::reflect::TypeRegistry) {
85-
#(
86-
<#generic_names as #bms_core::bindings::GetTypeDependencies>::register_type_dependencies(registry);
87-
)*
102+
#(#recursive_registrations)*
88103

89104
registry.register::<#underlying>();
90105
}
@@ -106,14 +121,16 @@ pub fn get_type_dependencies(input: TokenStream) -> TokenStream {
106121

107122
struct Args {
108123
bms_core_path: syn::Path,
109-
underlying: Option<syn::Type>
124+
underlying: Option<syn::Type>,
125+
dont_recurse: bool,
110126
// bounds: Vec<syn::TypeParamBound>,
111127
}
112128

113129
impl Args {
114130
fn parse(attrs: &[syn::Attribute]) -> syn::Result<Self> {
115131
let mut bms_core_path = parse_quote!(bevy_mod_scripting_core);
116132
let mut underlying = None;
133+
let mut dont_recurse = false;
117134

118135
for attr in attrs {
119136
// find attr with name `get_type_dependencies`
@@ -130,13 +147,16 @@ impl Args {
130147
let string: syn::LitStr = value.parse()?;
131148
underlying = Some(string.parse()?);
132149
Ok(())
150+
} else if meta.path.is_ident("dont_recurse") {
151+
dont_recurse = true;
152+
Ok(())
133153
} else {
134154
Err(syn::Error::new_spanned(meta.path, "unknown attribute, allowed: bms_core_path, underlying"))
135155
}
136156
})?;
137157
}
138158
}
139159

140-
Ok(Self { bms_core_path, underlying })
160+
Ok(Self { bms_core_path, underlying, dont_recurse })
141161
}
142162
}

0 commit comments

Comments
 (0)