Path: blob/main/crates/bevy_reflect/derive/src/impls/opaque.rs
6600 views
use crate::{1impls::{common_partial_reflect_methods, impl_full_reflect, impl_type_path, impl_typed},2where_clause_options::WhereClauseOptions,3ReflectMeta,4};5use bevy_macro_utils::fq_std::{FQClone, FQOption, FQResult};6use quote::quote;78/// Implements `GetTypeRegistration` and `Reflect` for the given type data.9pub(crate) fn impl_opaque(meta: &ReflectMeta) -> proc_macro2::TokenStream {10let bevy_reflect_path = meta.bevy_reflect_path();11let type_path = meta.type_path();1213#[cfg(feature = "documentation")]14let with_docs = {15let doc = quote::ToTokens::to_token_stream(meta.doc());16Some(quote!(.with_docs(#doc)))17};18#[cfg(not(feature = "documentation"))]19let with_docs: Option<proc_macro2::TokenStream> = None;2021let where_clause_options = WhereClauseOptions::new(meta);22let typed_impl = impl_typed(23&where_clause_options,24quote! {25let info = #bevy_reflect_path::OpaqueInfo::new::<Self>() #with_docs;26#bevy_reflect_path::TypeInfo::Opaque(info)27},28);2930let type_path_impl = impl_type_path(meta);31let full_reflect_impl = impl_full_reflect(&where_clause_options);32let common_methods = common_partial_reflect_methods(meta, || None, || None);33let clone_fn = meta.attrs().get_clone_impl(bevy_reflect_path);3435let apply_impl = if let Some(remote_ty) = meta.remote_ty() {36let ty = remote_ty.type_path();37quote! {38if let #FQOption::Some(value) = <dyn #bevy_reflect_path::PartialReflect>::try_downcast_ref::<#ty>(value) {39*self = Self(#FQClone::clone(value));40return #FQResult::Ok(());41}42}43} else {44quote! {45if let #FQOption::Some(value) = <dyn #bevy_reflect_path::PartialReflect>::try_downcast_ref::<Self>(value) {46*self = #FQClone::clone(value);47return #FQResult::Ok(());48}49}50};5152#[cfg(not(feature = "functions"))]53let function_impls = None::<proc_macro2::TokenStream>;54#[cfg(feature = "functions")]55let function_impls = crate::impls::impl_function_traits(&where_clause_options);5657#[cfg(not(feature = "auto_register"))]58let auto_register = None::<proc_macro2::TokenStream>;59#[cfg(feature = "auto_register")]60let auto_register = crate::impls::reflect_auto_registration(meta);6162let (impl_generics, ty_generics, where_clause) = type_path.generics().split_for_impl();63let where_reflect_clause = where_clause_options.extend_where_clause(where_clause);64let get_type_registration_impl = meta.get_type_registration(&where_clause_options);6566quote! {67#get_type_registration_impl6869#type_path_impl7071#typed_impl7273#full_reflect_impl7475#function_impls7677#auto_register7879impl #impl_generics #bevy_reflect_path::PartialReflect for #type_path #ty_generics #where_reflect_clause {80#[inline]81fn get_represented_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> {82#FQOption::Some(<Self as #bevy_reflect_path::Typed>::type_info())83}8485#[inline]86fn to_dynamic(&self) -> #bevy_reflect_path::__macro_exports::alloc_utils::Box<dyn #bevy_reflect_path::PartialReflect> {87#bevy_reflect_path::__macro_exports::alloc_utils::Box::new(#FQClone::clone(self))88}8990#[inline]91fn try_apply(92&mut self,93value: &dyn #bevy_reflect_path::PartialReflect94) -> #FQResult<(), #bevy_reflect_path::ApplyError> {95#apply_impl9697#FQResult::Err(98#bevy_reflect_path::ApplyError::MismatchedTypes {99from_type: ::core::convert::Into::into(#bevy_reflect_path::DynamicTypePath::reflect_type_path(value)),100to_type: ::core::convert::Into::into(<Self as #bevy_reflect_path::TypePath>::type_path()),101}102)103}104105#[inline]106fn reflect_kind(&self) -> #bevy_reflect_path::ReflectKind {107#bevy_reflect_path::ReflectKind::Opaque108}109110#[inline]111fn reflect_ref(&self) -> #bevy_reflect_path::ReflectRef {112#bevy_reflect_path::ReflectRef::Opaque(self)113}114115#[inline]116fn reflect_mut(&mut self) -> #bevy_reflect_path::ReflectMut {117#bevy_reflect_path::ReflectMut::Opaque(self)118}119120#[inline]121fn reflect_owned(self: #bevy_reflect_path::__macro_exports::alloc_utils::Box<Self>) -> #bevy_reflect_path::ReflectOwned {122#bevy_reflect_path::ReflectOwned::Opaque(self)123}124125#common_methods126127#clone_fn128}129}130}131132133