Repository: bevyengine/bevy
File: crates/bevy_reflect/derive/src/impls/structs.rs (Rust)

Code before:
                }
            }

            fn field_at(&self, index: usize) -> #FQOption<&dyn #bevy_reflect_path::PartialReflect> {
                match index {
                    #(#field_indices => #fqoption::Some(#fields_ref),)*
                    _ => #FQOption::None,
                }
            }

            fn field_at_mut(&mut self, index: usize) -> #FQOption<&mut dyn #bevy_reflect_path::PartialReflect> {
                match index {
                    #(#field_indices => #fqoption::Some(#fields_mut),)*
                    _ => #FQOption::None,
                }
            }

            fn name_at(&self, index: usize) -> #FQOption<&str> {
                match index {
                    #(#field_indices => #fqoption::Some(#field_names),)*
                    _ => #FQOption::None,
                }
            }

            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) })*
                #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) })*
                #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
            }

            fn iter_fields(&self) -> #bevy_reflect_path::structs::FieldIter {
                #bevy_reflect_path::structs::FieldIter::new(self)
            }

            fn to_dynamic_struct(&self) -> #bevy_reflect_path::structs::DynamicStruct {
                let mut dynamic: #bevy_reflect_path::structs::DynamicStruct = #FQDefault::default();

Review comment (suggestion):
As mentioned in [this](https://github.com/bevyengine/bevy/pull/22702#discussion_r2725961917) 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 `None` on a field of another struct of the same type) may be too unintuitive.

I'm on board with `index_by_name` (though I think it should be renamed to `index_of`) and `name_at`, but `index_of` (in its current form) and `name_of` seem too situational and easy to mess up.


I'm willing to change my mind if people feel strongly that this is useful/necessary, though.

Code after:
                }
            }

            fn field_at(&self, index: usize) -> #FQOption<&dyn #bevy_reflect_path::PartialReflect> {
                match index {
                    #(#field_indices => #fqoption::Some(#fields_ref),)*
                    _ => #FQOption::None,
                }
            }

            fn field_at_mut(&mut self, index: usize) -> #FQOption<&mut dyn #bevy_reflect_path::PartialReflect> {
                match index {
                    #(#field_indices => #fqoption::Some(#fields_mut),)*
                    _ => #FQOption::None,
                }
            }

            fn name_at(&self, index: usize) -> #FQOption<&str> {
                match index {
                    #(#field_indices => #fqoption::Some(#field_names),)*
                    _ => #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
            }

            fn iter_fields(&self) -> #bevy_reflect_path::structs::FieldIter {
                #bevy_reflect_path::structs::FieldIter::new(self)
            }

            fn to_dynamic_struct(&self) -> #bevy_reflect_path::structs::DynamicStruct {
                let mut dynamic: #bevy_reflect_path::structs::DynamicStruct = #FQDefault::default();
                dynamic.set_represented_type(#bevy_reflect_path::PartialReflect::get_represented_type_info(self));
                #(dynamic.insert_boxed(#field_names, #bevy_reflect_path::PartialReflect::to_dynamic(#fields_ref));)*
                dynamic
            }
        }

        impl #impl_generics #bevy_reflect_path::PartialReflect for #struct_path #ty_generics #where_reflect_clause {
            #[inline]
            fn get_represented_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> {
                #FQOption::Some(<Self as #bevy_reflect_path::Typed>::type_info())