Skip to content

Commit

Permalink
RenderResource derive correctly propogating generics
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSWard committed Apr 30, 2021
1 parent ad15982 commit a306d1d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
3 changes: 2 additions & 1 deletion crates/bevy_derive/src/render_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ pub fn derive_render_resource(input: TokenStream) -> TokenStream {
let bevy_asset_path: Path = get_path(&modules.bevy_asset);
let bevy_core_path: Path = get_path(&modules.bevy_core);
let struct_name = &ast.ident;
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();

TokenStream::from(quote! {
impl #bevy_render_path::renderer::RenderResource for #struct_name {
impl #impl_generics #bevy_render_path::renderer::RenderResource for #struct_name #type_generics #where_clause {
fn resource_type(&self) -> Option<#bevy_render_path::renderer::RenderResourceType> {
Some(#bevy_render_path::renderer::RenderResourceType::Buffer)
}
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_derive/src/render_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ pub fn derive_render_resources(input: TokenStream) -> TokenStream {
attributes
});
let struct_name = &ast.ident;
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();
let struct_name_string = struct_name.to_string();

if attributes.from_self {
TokenStream::from(quote! {
impl #bevy_render_path::renderer::RenderResources for #struct_name {
impl #impl_generics #bevy_render_path::renderer::RenderResources for #struct_name #type_generics #where_clause {
fn render_resources_len(&self) -> usize {
1
}
Expand Down Expand Up @@ -154,7 +155,7 @@ pub fn derive_render_resources(input: TokenStream) -> TokenStream {
#(#render_resource_hints,)*
];

impl #bevy_render_path::renderer::RenderResources for #struct_name {
impl #impl_generics #bevy_render_path::renderer::RenderResources for #struct_name #type_generics #where_clause {
fn render_resources_len(&self) -> usize {
#render_resource_count
}
Expand Down
49 changes: 49 additions & 0 deletions crates/bevy_render/src/renderer/render_resource/render_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,52 @@ impl RenderResources for bevy_transform::prelude::GlobalTransform {
RenderResourceIterator::new(self)
}
}

#[cfg(test)]
mod test {
use super::*;

#[derive(RenderResource, Bytes)]
#[as_crate(bevy_render)]
struct GenericRenderResource<T>
where
T: Bytes + Send + Sync + 'static,
{
value: T,
}

#[derive(RenderResources)]
#[as_crate(bevy_render)]
struct GenericRenderResources<T>
where
T: RenderResource + Send + Sync + 'static,
{
resource: T,
}

#[derive(Bytes, RenderResource, RenderResources)]
#[render_resources(from_self)]
#[as_crate(bevy_render)]
struct FromSelfGenericRenderResources<T>
where
T: Bytes + Send + Sync + 'static,
{
value: T,
}

fn test_impl_render_resource(_: &impl RenderResource) {}
fn test_impl_render_resources(_: &impl RenderResources) {}

#[test]
fn test_generic_render_resource_derive() {
let resource = GenericRenderResource { value: 42 };
test_impl_render_resource(&resource);

let resources = GenericRenderResources { resource };
test_impl_render_resources(&resources);

let from_self_resources = FromSelfGenericRenderResources { value: 42 };
test_impl_render_resource(&from_self_resources);
test_impl_render_resources(&from_self_resources);
}
}

0 comments on commit a306d1d

Please sign in to comment.