Skip to content

Commit

Permalink
Use PhantomData to fix generics being lost
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed Apr 28, 2023
1 parent 5701889 commit d5c13d6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
3 changes: 2 additions & 1 deletion bevy_proto_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ syn = "2.0"
quote = "1.0"
proc-macro2 = "1.0"
proc-macro-crate = "1.3"
optional-error = "0.1"
optional-error = "0.1"
to_phantom = "0.1"
3 changes: 2 additions & 1 deletion bevy_proto_derive/src/schematic/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ impl DeriveSchematic {

Some(quote! {
match #INPUT_IDENT {
#(#arms),*
#(#arms,)*
_ => unreachable!(),
}
})
}
Expand Down
16 changes: 12 additions & 4 deletions bevy_proto_derive/src/schematic/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use proc_macro2::{Ident, Span, TokenStream};
use quote::{format_ident, quote, ToTokens};
use syn::token::Pub;
use syn::{Generics, Token, TypePath, Visibility};
use to_phantom::ToPhantom;

use crate::schematic::data::SchematicData;
use crate::schematic::fields::SchematicField;
Expand Down Expand Up @@ -120,6 +121,7 @@ impl<'a> ToTokens for Input<'a> {
let input_ty = quote!(#input_ident #impl_ty_generics);
// Ex: Foo<T: Bar>
let input_ty_def = quote!(#input_ident #impl_generics);
let phantom_ty = self.generics.to_phantom();

let make_from_impl = |body: TokenStream| {
quote! {
Expand Down Expand Up @@ -158,7 +160,9 @@ impl<'a> ToTokens for Input<'a> {
tokens.extend(quote! {
#[derive(#bevy_crate::prelude::Reflect, #bevy_crate::prelude::FromReflect)]
#vis struct #input_ty_def (
#(#filtered),*
#(#filtered,)*
#[reflect(ignore, default)]
#phantom_ty
) #where_clause;

#from_impl
Expand All @@ -178,7 +182,9 @@ impl<'a> ToTokens for Input<'a> {
tokens.extend(quote! {
#[derive(#bevy_crate::prelude::Reflect, #bevy_crate::prelude::FromReflect)]
#vis struct #input_ty_def #where_clause {
#(#filtered),*
#(#filtered,)*
#[reflect(ignore, default)]
__phantom_ty__: #phantom_ty
}

#from_impl
Expand All @@ -191,14 +197,16 @@ impl<'a> ToTokens for Input<'a> {

let from_impl = make_from_impl(quote! {
match #INPUT_IDENT {
#(#constructors),*
#(#constructors,)*
_ => unreachable!(),
}
});

tokens.extend(quote! {
#[derive(#bevy_crate::prelude::Reflect, #bevy_crate::prelude::FromReflect)]
#vis enum #input_ty_def #where_clause {
#(#variants),*
#(#variants,)*
_Phantom(#[reflect(ignore, default)] #phantom_ty)
}

#from_impl
Expand Down

0 comments on commit d5c13d6

Please sign in to comment.