Path: blob/main/crates/bevy_reflect/derive/src/impls/tuple_structs.rs
9374 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 `TupleStruct`, `GetTypeRegistration`, and `Reflect` for the given derive data.9pub(crate) fn impl_tuple_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 FieldAccessors {16fields_ref,17fields_mut,18field_indices,19field_count,20..21} = FieldAccessors::new(reflect_struct);2223let where_clause_options = reflect_struct.where_clause_options();24let get_type_registration_impl = reflect_struct.get_type_registration(&where_clause_options);2526let typed_impl = impl_typed(&where_clause_options, reflect_struct.to_info_tokens(true));2728let type_path_impl = impl_type_path(reflect_struct.meta());29let full_reflect_impl = impl_full_reflect(&where_clause_options);30let common_methods = common_partial_reflect_methods(31reflect_struct.meta(),32|| Some(quote!(#bevy_reflect_path::tuple_struct::tuple_struct_partial_eq)),33|| None,34|| Some(quote!(#bevy_reflect_path::tuple_struct::tuple_struct_partial_cmp)),35);36let clone_fn = reflect_struct.get_clone_impl();3738#[cfg(not(feature = "functions"))]39let function_impls = None::<proc_macro2::TokenStream>;40#[cfg(feature = "functions")]41let function_impls = crate::impls::impl_function_traits(&where_clause_options);4243let (impl_generics, ty_generics, where_clause) = reflect_struct44.meta()45.type_path()46.generics()47.split_for_impl();4849#[cfg(not(feature = "auto_register"))]50let auto_register = None::<proc_macro2::TokenStream>;51#[cfg(feature = "auto_register")]52let auto_register = crate::impls::reflect_auto_registration(reflect_struct.meta());5354let where_reflect_clause = where_clause_options.extend_where_clause(where_clause);5556quote! {57#get_type_registration_impl5859#typed_impl6061#type_path_impl6263#full_reflect_impl6465#function_impls6667#auto_register6869impl #impl_generics #bevy_reflect_path::tuple_struct::TupleStruct for #struct_path #ty_generics #where_reflect_clause {70fn field(&self, index: usize) -> #FQOption<&dyn #bevy_reflect_path::PartialReflect> {71match index {72#(#field_indices => #fqoption::Some(#fields_ref),)*73_ => #FQOption::None,74}75}7677fn field_mut(&mut self, index: usize) -> #FQOption<&mut dyn #bevy_reflect_path::PartialReflect> {78match index {79#(#field_indices => #fqoption::Some(#fields_mut),)*80_ => #FQOption::None,81}82}83#[inline]84fn field_len(&self) -> usize {85#field_count86}87#[inline]88fn iter_fields(&self) -> #bevy_reflect_path::tuple_struct::TupleStructFieldIter {89#bevy_reflect_path::tuple_struct::TupleStructFieldIter::new(self)90}9192fn to_dynamic_tuple_struct(&self) -> #bevy_reflect_path::tuple_struct::DynamicTupleStruct {93let mut dynamic: #bevy_reflect_path::tuple_struct::DynamicTupleStruct = #FQDefault::default();94dynamic.set_represented_type(#bevy_reflect_path::PartialReflect::get_represented_type_info(self));95#(dynamic.insert_boxed(#bevy_reflect_path::PartialReflect::to_dynamic(#fields_ref));)*96dynamic97}98}99100impl #impl_generics #bevy_reflect_path::PartialReflect for #struct_path #ty_generics #where_reflect_clause {101#[inline]102fn get_represented_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> {103#FQOption::Some(<Self as #bevy_reflect_path::Typed>::type_info())104}105106#[inline]107fn try_apply(108&mut self,109value: &dyn #bevy_reflect_path::PartialReflect110) -> #FQResult<(), #bevy_reflect_path::ApplyError> {111if let #bevy_reflect_path::ReflectRef::TupleStruct(struct_value) =112#bevy_reflect_path::PartialReflect::reflect_ref(value) {113for (i, value) in ::core::iter::Iterator::enumerate(#bevy_reflect_path::tuple_struct::TupleStruct::iter_fields(struct_value)) {114if let #FQOption::Some(v) = #bevy_reflect_path::tuple_struct::TupleStruct::field_mut(self, i) {115#bevy_reflect_path::PartialReflect::try_apply(v, value)?;116}117}118} else {119return #FQResult::Err(120#bevy_reflect_path::ApplyError::MismatchedKinds {121from_kind: #bevy_reflect_path::PartialReflect::reflect_kind(value),122to_kind: #bevy_reflect_path::ReflectKind::TupleStruct,123}124);125}126#FQResult::Ok(())127}128#[inline]129fn reflect_kind(&self) -> #bevy_reflect_path::ReflectKind {130#bevy_reflect_path::ReflectKind::TupleStruct131}132#[inline]133fn reflect_ref(&self) -> #bevy_reflect_path::ReflectRef {134#bevy_reflect_path::ReflectRef::TupleStruct(self)135}136#[inline]137fn reflect_mut(&mut self) -> #bevy_reflect_path::ReflectMut {138#bevy_reflect_path::ReflectMut::TupleStruct(self)139}140#[inline]141fn reflect_owned(self: #bevy_reflect_path::__macro_exports::alloc_utils::Box<Self>) -> #bevy_reflect_path::ReflectOwned {142#bevy_reflect_path::ReflectOwned::TupleStruct(self)143}144145#common_methods146147#clone_fn148}149}150}151152153