Path: blob/main/crates/bevy_reflect/derive/src/impls/tuple_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 `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_partial_eq)),33|| None,34);35let clone_fn = reflect_struct.get_clone_impl();3637#[cfg(not(feature = "functions"))]38let function_impls = None::<proc_macro2::TokenStream>;39#[cfg(feature = "functions")]40let function_impls = crate::impls::impl_function_traits(&where_clause_options);4142let (impl_generics, ty_generics, where_clause) = reflect_struct43.meta()44.type_path()45.generics()46.split_for_impl();4748#[cfg(not(feature = "auto_register"))]49let auto_register = None::<proc_macro2::TokenStream>;50#[cfg(feature = "auto_register")]51let auto_register = crate::impls::reflect_auto_registration(reflect_struct.meta());5253let where_reflect_clause = where_clause_options.extend_where_clause(where_clause);5455quote! {56#get_type_registration_impl5758#typed_impl5960#type_path_impl6162#full_reflect_impl6364#function_impls6566#auto_register6768impl #impl_generics #bevy_reflect_path::TupleStruct for #struct_path #ty_generics #where_reflect_clause {69fn field(&self, index: usize) -> #FQOption<&dyn #bevy_reflect_path::PartialReflect> {70match index {71#(#field_indices => #fqoption::Some(#fields_ref),)*72_ => #FQOption::None,73}74}7576fn field_mut(&mut self, index: usize) -> #FQOption<&mut dyn #bevy_reflect_path::PartialReflect> {77match index {78#(#field_indices => #fqoption::Some(#fields_mut),)*79_ => #FQOption::None,80}81}82#[inline]83fn field_len(&self) -> usize {84#field_count85}86#[inline]87fn iter_fields(&self) -> #bevy_reflect_path::TupleStructFieldIter {88#bevy_reflect_path::TupleStructFieldIter::new(self)89}9091fn to_dynamic_tuple_struct(&self) -> #bevy_reflect_path::DynamicTupleStruct {92let mut dynamic: #bevy_reflect_path::DynamicTupleStruct = #FQDefault::default();93dynamic.set_represented_type(#bevy_reflect_path::PartialReflect::get_represented_type_info(self));94#(dynamic.insert_boxed(#bevy_reflect_path::PartialReflect::to_dynamic(#fields_ref));)*95dynamic96}97}9899impl #impl_generics #bevy_reflect_path::PartialReflect for #struct_path #ty_generics #where_reflect_clause {100#[inline]101fn get_represented_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> {102#FQOption::Some(<Self as #bevy_reflect_path::Typed>::type_info())103}104105#[inline]106fn try_apply(107&mut self,108value: &dyn #bevy_reflect_path::PartialReflect109) -> #FQResult<(), #bevy_reflect_path::ApplyError> {110if let #bevy_reflect_path::ReflectRef::TupleStruct(struct_value) =111#bevy_reflect_path::PartialReflect::reflect_ref(value) {112for (i, value) in ::core::iter::Iterator::enumerate(#bevy_reflect_path::TupleStruct::iter_fields(struct_value)) {113if let #FQOption::Some(v) = #bevy_reflect_path::TupleStruct::field_mut(self, i) {114#bevy_reflect_path::PartialReflect::try_apply(v, value)?;115}116}117} else {118return #FQResult::Err(119#bevy_reflect_path::ApplyError::MismatchedKinds {120from_kind: #bevy_reflect_path::PartialReflect::reflect_kind(value),121to_kind: #bevy_reflect_path::ReflectKind::TupleStruct,122}123);124}125#FQResult::Ok(())126}127#[inline]128fn reflect_kind(&self) -> #bevy_reflect_path::ReflectKind {129#bevy_reflect_path::ReflectKind::TupleStruct130}131#[inline]132fn reflect_ref(&self) -> #bevy_reflect_path::ReflectRef {133#bevy_reflect_path::ReflectRef::TupleStruct(self)134}135#[inline]136fn reflect_mut(&mut self) -> #bevy_reflect_path::ReflectMut {137#bevy_reflect_path::ReflectMut::TupleStruct(self)138}139#[inline]140fn reflect_owned(self: #bevy_reflect_path::__macro_exports::alloc_utils::Box<Self>) -> #bevy_reflect_path::ReflectOwned {141#bevy_reflect_path::ReflectOwned::TupleStruct(self)142}143144#common_methods145146#clone_fn147}148}149}150151152