Skip to content

Commit bb88c8d

Browse files
committed
remove support for customizing buffers, select_buffers allows any buffer types instead
Signed-off-by: Teo Koon Peng <[email protected]>
1 parent cff7138 commit bb88c8d

File tree

4 files changed

+62
-20
lines changed

4 files changed

+62
-20
lines changed

macros/src/buffer.rs

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
use proc_macro::TokenStream;
1+
use proc_macro2::TokenStream;
22
use quote::{format_ident, quote};
3-
use syn::{parse_quote, Ident, ItemStruct, Type};
3+
use syn::{parse_quote, Field, Ident, ItemStruct, Type};
44

55
use crate::Result;
66

77
pub(crate) fn impl_joined_value(input_struct: &ItemStruct) -> Result<TokenStream> {
88
let struct_ident = &input_struct.ident;
99
let (impl_generics, ty_generics, where_clause) = input_struct.generics.split_for_impl();
10-
let (field_ident, field_type): (Vec<_>, Vec<_>) =
11-
get_fields_map(&input_struct.fields)?.into_iter().unzip();
10+
let (field_ident, field_type, _) = get_fields_map(&input_struct.fields)?;
1211
let struct_buffer_ident = format_ident!("__bevy_impulse_{}_Buffers", struct_ident);
1312

1413
let buffer_struct: ItemStruct = parse_quote! {
@@ -33,13 +32,14 @@ pub(crate) fn impl_joined_value(input_struct: &ItemStruct) -> Result<TokenStream
3332

3433
impl #impl_generics #struct_ident #ty_generics #where_clause {
3534
fn select_buffers(
35+
builder: &mut ::bevy_impulse::Builder,
3636
#(
37-
#field_ident: ::bevy_impulse::Buffer<#field_type>,
37+
#field_ident: impl ::bevy_impulse::Bufferable<BufferType = ::bevy_impulse::Buffer<#field_type>>,
3838
)*
3939
) -> #struct_buffer_ident #ty_generics {
4040
#struct_buffer_ident {
4141
#(
42-
#field_ident,
42+
#field_ident: #field_ident.into_buffer(builder),
4343
)*
4444
}
4545
}
@@ -53,18 +53,52 @@ pub(crate) fn impl_joined_value(input_struct: &ItemStruct) -> Result<TokenStream
5353
Ok(gen.into())
5454
}
5555

56-
fn get_fields_map(fields: &syn::Fields) -> Result<Vec<(&Ident, &Type)>> {
56+
struct FieldConfig {
57+
buffer_type: TokenStream,
58+
}
59+
60+
impl FieldConfig {
61+
fn from_field(field: &Field) -> Self {
62+
let ty = &field.ty;
63+
let mut buffer_type = quote! { ::bevy_impulse::Buffer<#ty> };
64+
65+
let attr = field
66+
.attrs
67+
.iter()
68+
.find(|attr| attr.path().is_ident("bevy_impulse"));
69+
70+
if let Some(attr) = attr {
71+
attr.parse_nested_meta(|meta| {
72+
if meta.path.is_ident("buffer_type") {
73+
buffer_type = meta.value()?.parse()?;
74+
}
75+
Ok(())
76+
})
77+
// panic if attribute is malformed, this will result in a compile error
78+
.unwrap();
79+
}
80+
81+
Self { buffer_type }
82+
}
83+
}
84+
85+
fn get_fields_map(fields: &syn::Fields) -> Result<(Vec<&Ident>, Vec<&Type>, Vec<FieldConfig>)> {
5786
match fields {
5887
syn::Fields::Named(data) => {
59-
let mut idents_types = Vec::with_capacity(data.named.len());
88+
let mut idents = Vec::new();
89+
let mut types = Vec::new();
90+
let mut configs = Vec::new();
6091
for field in &data.named {
6192
let ident = field
6293
.ident
6394
.as_ref()
6495
.ok_or("expected named fields".to_string())?;
65-
idents_types.push((ident, &field.ty));
96+
let config = FieldConfig::from_field(field);
97+
idents.push(ident);
98+
types.push(&field.ty);
99+
configs.push(config);
66100
}
67-
Ok(idents_types)
101+
Ok((idents, types, configs))
68102
}
69103
_ => return Err("expected named fields".to_string()),
70104
}
@@ -79,8 +113,7 @@ fn impl_buffer_map_layout(
79113
) -> Result<proc_macro2::TokenStream> {
80114
let struct_ident = &buffer_struct.ident;
81115
let (impl_generics, ty_generics, where_clause) = buffer_struct.generics.split_for_impl();
82-
let (field_ident, field_type): (Vec<_>, Vec<_>) =
83-
get_fields_map(&item_struct.fields)?.into_iter().unzip();
116+
let (field_ident, field_type, _) = get_fields_map(&item_struct.fields)?;
84117
let map_key: Vec<String> = field_ident.iter().map(|v| v.to_string()).collect();
85118

86119
Ok(quote! {
@@ -121,8 +154,7 @@ fn impl_joined(
121154
let struct_ident = &joined_struct.ident;
122155
let item_struct_ident = &item_struct.ident;
123156
let (impl_generics, ty_generics, where_clause) = item_struct.generics.split_for_impl();
124-
let (field_ident, _): (Vec<_>, Vec<_>) =
125-
get_fields_map(&item_struct.fields)?.into_iter().unzip();
157+
let (field_ident, _, _) = get_fields_map(&item_struct.fields)?;
126158

127159
Ok(quote! {
128160
impl #impl_generics ::bevy_impulse::Joined for #struct_ident #ty_generics #where_clause {

macros/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ pub fn delivery_label_macro(item: TokenStream) -> TokenStream {
6565
/// The result error is the compiler error message to be displayed.
6666
type Result<T> = std::result::Result<T, String>;
6767

68-
#[proc_macro_derive(JoinedValue)]
68+
#[proc_macro_derive(JoinedValue, attributes(bevy_impulse))]
6969
pub fn derive_joined_value(input: TokenStream) -> TokenStream {
7070
let input = parse_macro_input!(input as ItemStruct);
7171
match impl_joined_value(&input) {
72-
Ok(tokens) => tokens,
72+
Ok(tokens) => tokens.into(),
7373
Err(msg) => quote! {
7474
compile_error!(#msg);
7575
}

src/buffer/any_buffer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ impl AnyBuffer {
8686
.entry(TypeId::of::<T>())
8787
.or_insert_with(|| Box::leak(Box::new(AnyBufferAccessImpl::<T>::new())))
8888
}
89+
90+
pub fn as_any_buffer(self) -> AnyBuffer {
91+
self
92+
}
8993
}
9094

9195
impl std::fmt::Debug for AnyBuffer {

src/buffer/buffer_map.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,17 @@ mod tests {
369369
let mut context = TestingContext::minimal_plugins();
370370

371371
let workflow = context.spawn_io_workflow(|scope, builder| {
372+
let buffer_i64 = builder.create_buffer(BufferSettings::default());
373+
let buffer_f64 = builder.create_buffer(BufferSettings::default());
374+
let buffer_string = builder.create_buffer(BufferSettings::default());
375+
let buffer_generic = builder.create_buffer(BufferSettings::default());
376+
372377
let buffers = TestJoinedValue::select_buffers(
373-
builder.create_buffer(BufferSettings::default()),
374-
builder.create_buffer(BufferSettings::default()),
375-
builder.create_buffer(BufferSettings::default()),
376-
builder.create_buffer(BufferSettings::default()),
378+
builder,
379+
buffer_i64,
380+
buffer_f64,
381+
buffer_string,
382+
buffer_generic,
377383
);
378384

379385
scope.input.chain(builder).fork_unzip((

0 commit comments

Comments
 (0)