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):
One idea for getting around the limitations I mention [here](https://github.com/bevyengine/bevy/pull/22708/changes#r2729105400), is to instead make `name_of` and friends return a `Result` with a potential error of like `NameOfError::NonUniqueFieldType`. And then return that error if the struct contains two of the same type field.

But I don't love that idea as it still is super limiting (just not as much).

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())