Skip to content

Commit 1b4cf02

Browse files
authored
bevy_reflect: streamline generated FromReflect::from_reflect (#19906)
# Objective Generated `from_reflect` methods use closures in a weird way, e.g.: ```rust x: (|| { <f32 as ::bevy::reflect::FromReflect>::from_reflect( ::bevy::reflect::Struct::field(__ref_struct, "x")?, ) })()?, ``` The reason for this is because when `#[reflect(Default)]` is used, you instead get stuff like this: ```rust if let ::core::option::Option::Some(__field) = (|| { <f32 as ::bevy::reflect::FromReflect>::from_reflect( ::bevy::reflect::Struct::field(__ref_struct, "x")?, ) })() { __this.x = __field; } ``` and the closure is necessary to contain the scope of the `?`. But the first case is more common. Helps with #19873. ## Solution Avoid the closure in the common case. ## Testing I used cargo expand to confirm the closures are no longer produced in the common case. `-Zmacro-stats` output tells me this reduces the size of the `Reflect` code produced for `bevy_ui` by 0.5%.
1 parent f95f42b commit 1b4cf02

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

crates/bevy_reflect/derive/src/from_reflect.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ fn impl_struct_internal(
146146
quote! {
147147
let mut #__this = <#reflect_ty as #FQDefault>::default();
148148
#(
149-
if let #fqoption::Some(__field) = #active_values() {
149+
// The closure catches any failing `?` within `active_values`.
150+
if let #fqoption::Some(__field) = (|| #active_values)() {
150151
// Iff field exists -> use its value
151152
#__this.#active_members = __field;
152153
}
@@ -158,7 +159,7 @@ fn impl_struct_internal(
158159

159160
quote! {
160161
let #__this = #constructor {
161-
#(#active_members: #active_values()?,)*
162+
#(#active_members: #active_values?,)*
162163
#(#ignored_members: #ignored_values,)*
163164
};
164165
#FQOption::Some(#retval)
@@ -274,35 +275,31 @@ fn get_active_fields(
274275
<#ty as #bevy_reflect_path::FromReflect>::from_reflect(field)
275276
});
276277
quote! {
277-
(||
278-
if let #FQOption::Some(field) = #get_field {
279-
#value
280-
} else {
281-
#FQOption::Some(#path())
282-
}
283-
)
278+
if let #FQOption::Some(field) = #get_field {
279+
#value
280+
} else {
281+
#FQOption::Some(#path())
282+
}
284283
}
285284
}
286285
DefaultBehavior::Default => {
287286
let value = into_remote(quote! {
288287
<#ty as #bevy_reflect_path::FromReflect>::from_reflect(field)
289288
});
290289
quote! {
291-
(||
292-
if let #FQOption::Some(field) = #get_field {
293-
#value
294-
} else {
295-
#FQOption::Some(#FQDefault::default())
296-
}
297-
)
290+
if let #FQOption::Some(field) = #get_field {
291+
#value
292+
} else {
293+
#FQOption::Some(#FQDefault::default())
294+
}
298295
}
299296
}
300297
DefaultBehavior::Required => {
301298
let value = into_remote(quote! {
302299
<#ty as #bevy_reflect_path::FromReflect>::from_reflect(#get_field?)
303300
});
304301
quote! {
305-
(|| #value)
302+
#value
306303
}
307304
}
308305
};

0 commit comments

Comments
 (0)