Skip to content

Commit

Permalink
Merge pull request #31 from MrGVSV/fix-generics
Browse files Browse the repository at this point in the history
Fix generics with `#[schematic(asset)]`
  • Loading branch information
MrGVSV authored Apr 28, 2023
2 parents 5701889 + 8182fa4 commit a0f4e22
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ serde_yaml = { version = "0.9", optional = true, default-features = false }
ron = "0.8"
bevy = "0.10.1"
bevy_prototype_lyon = "0.8.0"
trybuild = "1.0.71"

[[example]]
name = "basic_schematic"
Expand Down Expand Up @@ -110,3 +111,7 @@ required-features = ["ron", "auto_name"]
name = "templates"
path = "examples/templates.rs"
required-features = ["ron", "auto_name", "custom_schematics", "bevy_sprite", "yaml"]

[[test]]
name = "compile_tests"
path = "tests/compile_tests.rs"
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
6 changes: 6 additions & 0 deletions tests/compile_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[test]
fn test() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/compile_tests/*.fail.rs");
t.pass("tests/compile_tests/*.pass.rs");
}
36 changes: 36 additions & 0 deletions tests/compile_tests/generics.pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use bevy::asset::Asset;
use bevy::prelude::*;
use bevy_proto::prelude::*;
use std::borrow::Cow;

#[derive(Component, Schematic, Reflect)]
#[reflect(Schematic)]
struct TupleStructWithGenerics<'a: 'static, T: Asset, const N: usize>(
#[schematic(asset)] Handle<T>,
Cow<'a, str>,
[i32; N],
);

#[derive(Component, Schematic, Reflect)]
#[reflect(Schematic)]
struct StructWithGenerics<'a: 'static, T: Asset, const N: usize> {
#[schematic(asset)]
asset: Handle<T>,
string: Cow<'a, str>,
array: [i32; N],
}

#[derive(Component, Schematic, Reflect)]
#[reflect(Schematic)]
enum EnumWithGenerics<'a: 'static, T: Asset, const N: usize> {
Unit,
Tuple(#[schematic(asset)] Handle<T>, Cow<'a, str>, [i32; N]),
Struct {
#[schematic(asset)]
asset: Handle<T>,
string: Cow<'a, str>,
array: [i32; N],
},
}

fn main() {}

0 comments on commit a0f4e22

Please sign in to comment.