-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Reflect Struct QOL #22708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Reflect Struct QOL #22708
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,6 +115,23 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> proc_macro2::TokenS | |
| } | ||
| } | ||
|
|
||
| fn name_of(&self, field: &dyn #bevy_reflect_path::PartialReflect) -> #FQOption<&str> { | ||
| #(if #bevy_reflect_path::PartialReflect::reflect_partial_eq(#fields_ref, field).unwrap_or(false) { return #fqoption::Some(#field_names) })* | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned in this comment, I'm still not fully sold on this behavior. To summarize my thoughts, I believe this behavior (i.e. where a field from one struct returns I'm on board with I'm willing to change my mind if people feel strongly that this is useful/necessary, though.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One idea for getting around the limitations I mention here, is to instead make But I don't love that idea as it still is super limiting (just not as much).
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another option that I think would work better is to provide a low-level API for working with struct field pointers and addresses directly. Then you could do something like: let api = TheApi::from(&my_struct);
let field: NamedField = api.field_at_addr(&my_struct.field).unwrap();
let name = field.name();In fact, renaming |
||
| #FQOption::None | ||
| } | ||
|
|
||
| fn index_of(&self, field: &dyn #bevy_reflect_path::PartialReflect) -> #FQOption<usize> { | ||
| #(if #bevy_reflect_path::PartialReflect::reflect_partial_eq(#fields_ref, field).unwrap_or(false) { return #fqoption::Some(#field_indices) })* | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above. |
||
| #FQOption::None | ||
| } | ||
|
|
||
| fn index_of_name(&self, name: &str) -> #FQOption<usize> { | ||
| match name { | ||
| #(#field_names => #fqoption::Some(#field_indices),)* | ||
| _ => #FQOption::None, | ||
| } | ||
| } | ||
|
|
||
| fn field_len(&self) -> usize { | ||
| #field_count | ||
| } | ||
|
|
@@ -144,8 +161,7 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> proc_macro2::TokenS | |
| ) -> #FQResult<(), #bevy_reflect_path::ApplyError> { | ||
| if let #bevy_reflect_path::ReflectRef::Struct(struct_value) | ||
| = #bevy_reflect_path::PartialReflect::reflect_ref(value) { | ||
| for (i, value) in ::core::iter::Iterator::enumerate(#bevy_reflect_path::structs::Struct::iter_fields(struct_value)) { | ||
| let name = #bevy_reflect_path::structs::Struct::name_at(struct_value, i).unwrap(); | ||
| for (name, value) in #bevy_reflect_path::structs::Struct::iter_fields(struct_value) { | ||
| if let #FQOption::Some(v) = #bevy_reflect_path::structs::Struct::field_mut(self, name) { | ||
| #bevy_reflect_path::PartialReflect::try_apply(v, value)?; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: you can probably clean this up by using ? out of the reflect_partial_eq.