Path: blob/main/crates/bevy_reflect/derive/src/registration.rs
6599 views
//! Contains code related specifically to Bevy's type registration.12use crate::{serialization::SerializationDataDef, where_clause_options::WhereClauseOptions};3use quote::quote;4use syn::Type;56/// Creates the `GetTypeRegistration` impl for the given type data.7pub(crate) fn impl_get_type_registration<'a>(8where_clause_options: &WhereClauseOptions,9serialization_data: Option<&SerializationDataDef>,10type_dependencies: Option<impl Iterator<Item = &'a Type>>,11) -> proc_macro2::TokenStream {12let meta = where_clause_options.meta();13let type_path = meta.type_path();14let bevy_reflect_path = meta.bevy_reflect_path();15let registration_data = meta.attrs().idents();1617let type_deps_fn = type_dependencies.map(|deps| {18quote! {19#[inline(never)]20fn register_type_dependencies(registry: &mut #bevy_reflect_path::TypeRegistry) {21#(<#deps as #bevy_reflect_path::__macro_exports::RegisterForReflection>::__register(registry);)*22}23}24});2526let (impl_generics, ty_generics, where_clause) = type_path.generics().split_for_impl();27let where_reflect_clause = where_clause_options.extend_where_clause(where_clause);2829let from_reflect_data = if meta.from_reflect().should_auto_derive() {30Some(quote! {31registration.insert::<#bevy_reflect_path::ReflectFromReflect>(#bevy_reflect_path::FromType::<Self>::from_type());32})33} else {34None35};3637let serialization_data = serialization_data.map(|data| {38let serialization_data = data.as_serialization_data(bevy_reflect_path);39quote! {40registration.insert::<#bevy_reflect_path::serde::SerializationData>(#serialization_data);41}42});4344quote! {45impl #impl_generics #bevy_reflect_path::GetTypeRegistration for #type_path #ty_generics #where_reflect_clause {46fn get_type_registration() -> #bevy_reflect_path::TypeRegistration {47let mut registration = #bevy_reflect_path::TypeRegistration::of::<Self>();48registration.insert::<#bevy_reflect_path::ReflectFromPtr>(#bevy_reflect_path::FromType::<Self>::from_type());49#from_reflect_data50#serialization_data51#(registration.insert::<#registration_data>(#bevy_reflect_path::FromType::<Self>::from_type());)*52registration53}5455#type_deps_fn56}57}58}596061