Path: blob/main/crates/bevy_reflect/derive/src/impls/structs.rs
6600 views
use crate::{1impls::{common_partial_reflect_methods, impl_full_reflect, impl_type_path, impl_typed},2struct_utility::FieldAccessors,3ReflectStruct,4};5use bevy_macro_utils::fq_std::{FQDefault, FQOption, FQResult};6use quote::{quote, ToTokens};78/// Implements `Struct`, `GetTypeRegistration`, and `Reflect` for the given derive data.9pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> proc_macro2::TokenStream {10let fqoption = FQOption.into_token_stream();1112let bevy_reflect_path = reflect_struct.meta().bevy_reflect_path();13let struct_path = reflect_struct.meta().type_path();1415let field_names = reflect_struct16.active_fields()17.map(|field| {18field19.data20.ident21.as_ref()22.map(ToString::to_string)23.unwrap_or_else(|| field.declaration_index.to_string())24})25.collect::<Vec<String>>();2627let FieldAccessors {28fields_ref,29fields_mut,30field_indices,31field_count,32..33} = FieldAccessors::new(reflect_struct);3435let where_clause_options = reflect_struct.where_clause_options();36let typed_impl = impl_typed(&where_clause_options, reflect_struct.to_info_tokens(false));3738let type_path_impl = impl_type_path(reflect_struct.meta());39let full_reflect_impl = impl_full_reflect(&where_clause_options);40let common_methods = common_partial_reflect_methods(41reflect_struct.meta(),42|| Some(quote!(#bevy_reflect_path::struct_partial_eq)),43|| None,44);45let clone_fn = reflect_struct.get_clone_impl();4647#[cfg(not(feature = "functions"))]48let function_impls = None::<proc_macro2::TokenStream>;49#[cfg(feature = "functions")]50let function_impls = crate::impls::impl_function_traits(&where_clause_options);5152let get_type_registration_impl = reflect_struct.get_type_registration(&where_clause_options);5354let (impl_generics, ty_generics, where_clause) = reflect_struct55.meta()56.type_path()57.generics()58.split_for_impl();5960#[cfg(not(feature = "auto_register"))]61let auto_register = None::<proc_macro2::TokenStream>;62#[cfg(feature = "auto_register")]63let auto_register = crate::impls::reflect_auto_registration(reflect_struct.meta());6465let where_reflect_clause = where_clause_options.extend_where_clause(where_clause);6667quote! {68#get_type_registration_impl6970#typed_impl7172#type_path_impl7374#full_reflect_impl7576#function_impls7778#auto_register7980impl #impl_generics #bevy_reflect_path::Struct for #struct_path #ty_generics #where_reflect_clause {81fn field(&self, name: &str) -> #FQOption<&dyn #bevy_reflect_path::PartialReflect> {82match name {83#(#field_names => #fqoption::Some(#fields_ref),)*84_ => #FQOption::None,85}86}8788fn field_mut(&mut self, name: &str) -> #FQOption<&mut dyn #bevy_reflect_path::PartialReflect> {89match name {90#(#field_names => #fqoption::Some(#fields_mut),)*91_ => #FQOption::None,92}93}9495fn field_at(&self, index: usize) -> #FQOption<&dyn #bevy_reflect_path::PartialReflect> {96match index {97#(#field_indices => #fqoption::Some(#fields_ref),)*98_ => #FQOption::None,99}100}101102fn field_at_mut(&mut self, index: usize) -> #FQOption<&mut dyn #bevy_reflect_path::PartialReflect> {103match index {104#(#field_indices => #fqoption::Some(#fields_mut),)*105_ => #FQOption::None,106}107}108109fn name_at(&self, index: usize) -> #FQOption<&str> {110match index {111#(#field_indices => #fqoption::Some(#field_names),)*112_ => #FQOption::None,113}114}115116fn field_len(&self) -> usize {117#field_count118}119120fn iter_fields(&self) -> #bevy_reflect_path::FieldIter {121#bevy_reflect_path::FieldIter::new(self)122}123124fn to_dynamic_struct(&self) -> #bevy_reflect_path::DynamicStruct {125let mut dynamic: #bevy_reflect_path::DynamicStruct = #FQDefault::default();126dynamic.set_represented_type(#bevy_reflect_path::PartialReflect::get_represented_type_info(self));127#(dynamic.insert_boxed(#field_names, #bevy_reflect_path::PartialReflect::to_dynamic(#fields_ref));)*128dynamic129}130}131132impl #impl_generics #bevy_reflect_path::PartialReflect for #struct_path #ty_generics #where_reflect_clause {133#[inline]134fn get_represented_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> {135#FQOption::Some(<Self as #bevy_reflect_path::Typed>::type_info())136}137138#[inline]139fn try_apply(140&mut self,141value: &dyn #bevy_reflect_path::PartialReflect142) -> #FQResult<(), #bevy_reflect_path::ApplyError> {143if let #bevy_reflect_path::ReflectRef::Struct(struct_value)144= #bevy_reflect_path::PartialReflect::reflect_ref(value) {145for (i, value) in ::core::iter::Iterator::enumerate(#bevy_reflect_path::Struct::iter_fields(struct_value)) {146let name = #bevy_reflect_path::Struct::name_at(struct_value, i).unwrap();147if let #FQOption::Some(v) = #bevy_reflect_path::Struct::field_mut(self, name) {148#bevy_reflect_path::PartialReflect::try_apply(v, value)?;149}150}151} else {152return #FQResult::Err(153#bevy_reflect_path::ApplyError::MismatchedKinds {154from_kind: #bevy_reflect_path::PartialReflect::reflect_kind(value),155to_kind: #bevy_reflect_path::ReflectKind::Struct156}157);158}159#FQResult::Ok(())160}161#[inline]162fn reflect_kind(&self) -> #bevy_reflect_path::ReflectKind {163#bevy_reflect_path::ReflectKind::Struct164}165#[inline]166fn reflect_ref(&self) -> #bevy_reflect_path::ReflectRef {167#bevy_reflect_path::ReflectRef::Struct(self)168}169#[inline]170fn reflect_mut(&mut self) -> #bevy_reflect_path::ReflectMut {171#bevy_reflect_path::ReflectMut::Struct(self)172}173#[inline]174fn reflect_owned(self: #bevy_reflect_path::__macro_exports::alloc_utils::Box<Self>) -> #bevy_reflect_path::ReflectOwned {175#bevy_reflect_path::ReflectOwned::Struct(self)176}177178#common_methods179180#clone_fn181}182}183}184185186