-
Notifications
You must be signed in to change notification settings - Fork 2
derive Joined
to implement JoinedValue
and BufferMapLayout
#53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
1137c08
derive joined
koonpeng 9bc7cfe
remove use of unwrap
koonpeng 83d6595
use full path
koonpeng 5dbfa63
cleanup
koonpeng 68681e4
derive BufferKeyMap
koonpeng 1deada0
comments
koonpeng b612a05
Merge remote-tracking branch 'origin/buffer_map' into koonpeng/derive…
koonpeng af2caf7
support generics
koonpeng cff7138
add `select_buffers` to avoid the need to directly reference generate…
koonpeng bb88c8d
remove support for customizing buffers, select_buffers allows any buf…
koonpeng 957f17d
revert changes to AnyBuffer
koonpeng dde0e6d
add helper attribute to customized generated buffer struct ident
koonpeng 01af23f
remove builder argument in select_buffers; add test for select_buffer…
koonpeng 25ec4f2
wip buffer_type helper but without auto downcasting, doesnt work due …
koonpeng 943ea33
check for any
koonpeng 37ee9b9
allow buffer_downcast to downcast back to the original Buffer<T>
koonpeng a00f09f
move test_select_buffers_json
koonpeng e61c3ff
put unused code into its own mod; to_phantom_data uses fn(...) to all…
koonpeng 5299c25
rename helper attributes
koonpeng 81687f9
Merge remote-tracking branch 'origin/buffer_map' into koonpeng/derive…
koonpeng b110e87
Test for generics in buffers, and fix Clone/Copy semantics
mxgrey 31b3927
Fix style
mxgrey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ proc-macro = true | |
[dependencies] | ||
syn = "2.0" | ||
quote = "1.0" | ||
proc-macro2 = "1.0.93" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::{format_ident, quote}; | ||
use syn::{parse_quote, Ident, ItemStruct, Type}; | ||
|
||
use crate::Result; | ||
|
||
pub(crate) fn impl_joined_value(input_struct: &ItemStruct) -> Result<TokenStream> { | ||
let struct_ident = &input_struct.ident; | ||
let (impl_generics, ty_generics, where_clause) = input_struct.generics.split_for_impl(); | ||
let (field_ident, field_type) = get_fields_map(&input_struct.fields)?; | ||
let BufferStructConfig { | ||
struct_name: buffer_struct_ident, | ||
} = BufferStructConfig::from_data_struct(&input_struct); | ||
let buffer_struct_vis = &input_struct.vis; | ||
|
||
let buffer_struct: ItemStruct = parse_quote! { | ||
#[derive(Clone)] | ||
#[allow(non_camel_case_types)] | ||
#buffer_struct_vis struct #buffer_struct_ident #impl_generics #where_clause { | ||
#( | ||
#buffer_struct_vis #field_ident: ::bevy_impulse::Buffer<#field_type>, | ||
)* | ||
} | ||
}; | ||
|
||
let impl_buffer_map_layout = impl_buffer_map_layout(&buffer_struct, &input_struct)?; | ||
let impl_joined = impl_joined(&buffer_struct, &input_struct)?; | ||
|
||
let gen = quote! { | ||
impl #impl_generics ::bevy_impulse::JoinedValue for #struct_ident #ty_generics #where_clause { | ||
type Buffers = #buffer_struct_ident #ty_generics; | ||
} | ||
|
||
#buffer_struct | ||
|
||
impl #impl_generics #struct_ident #ty_generics #where_clause { | ||
fn select_buffers( | ||
builder: &mut ::bevy_impulse::Builder, | ||
#( | ||
#field_ident: impl ::bevy_impulse::Bufferable<BufferType = ::bevy_impulse::Buffer<#field_type>>, | ||
)* | ||
) -> #buffer_struct_ident #ty_generics { | ||
#buffer_struct_ident { | ||
#( | ||
#field_ident: #field_ident.into_buffer(builder), | ||
)* | ||
} | ||
} | ||
} | ||
|
||
#impl_buffer_map_layout | ||
|
||
#impl_joined | ||
}; | ||
|
||
Ok(gen.into()) | ||
} | ||
|
||
struct BufferStructConfig { | ||
struct_name: Ident, | ||
} | ||
|
||
impl BufferStructConfig { | ||
fn from_data_struct(data_struct: &ItemStruct) -> Self { | ||
let mut config = Self { | ||
struct_name: format_ident!("__bevy_impulse_{}_Buffers", data_struct.ident), | ||
}; | ||
|
||
let attr = data_struct | ||
.attrs | ||
.iter() | ||
.find(|attr| attr.path().is_ident("buffers")); | ||
|
||
if let Some(attr) = attr { | ||
attr.parse_nested_meta(|meta| { | ||
if meta.path.is_ident("struct_name") { | ||
config.struct_name = meta.value()?.parse()?; | ||
} | ||
Ok(()) | ||
}) | ||
// panic if attribute is malformed, this will result in a compile error which is intended. | ||
.unwrap(); | ||
} | ||
|
||
config | ||
} | ||
} | ||
|
||
fn get_fields_map(fields: &syn::Fields) -> Result<(Vec<&Ident>, Vec<&Type>)> { | ||
match fields { | ||
syn::Fields::Named(data) => { | ||
let mut idents = Vec::new(); | ||
let mut types = Vec::new(); | ||
for field in &data.named { | ||
let ident = field | ||
.ident | ||
.as_ref() | ||
.ok_or("expected named fields".to_string())?; | ||
idents.push(ident); | ||
types.push(&field.ty); | ||
} | ||
Ok((idents, types)) | ||
} | ||
_ => return Err("expected named fields".to_string()), | ||
} | ||
} | ||
|
||
/// Params: | ||
/// buffer_struct: The struct to implement `BufferMapLayout`. | ||
/// item_struct: The struct which `buffer_struct` is derived from. | ||
fn impl_buffer_map_layout( | ||
buffer_struct: &ItemStruct, | ||
item_struct: &ItemStruct, | ||
) -> Result<proc_macro2::TokenStream> { | ||
let struct_ident = &buffer_struct.ident; | ||
let (impl_generics, ty_generics, where_clause) = buffer_struct.generics.split_for_impl(); | ||
let (field_ident, field_type) = get_fields_map(&item_struct.fields)?; | ||
let map_key: Vec<String> = field_ident.iter().map(|v| v.to_string()).collect(); | ||
|
||
Ok(quote! { | ||
impl #impl_generics ::bevy_impulse::BufferMapLayout for #struct_ident #ty_generics #where_clause { | ||
fn buffer_list(&self) -> ::smallvec::SmallVec<[AnyBuffer; 8]> { | ||
use smallvec::smallvec; | ||
smallvec![#( | ||
self.#field_ident.as_any_buffer(), | ||
)*] | ||
} | ||
|
||
fn try_from_buffer_map(buffers: &::bevy_impulse::BufferMap) -> Result<Self, ::bevy_impulse::IncompatibleLayout> { | ||
let mut compatibility = ::bevy_impulse::IncompatibleLayout::default(); | ||
#( | ||
let #field_ident = if let Ok(buffer) = compatibility.require_message_type::<#field_type>(#map_key, buffers) { | ||
buffer | ||
} else { | ||
return Err(compatibility); | ||
}; | ||
)* | ||
|
||
Ok(Self {#( | ||
#field_ident, | ||
)*}) | ||
} | ||
} | ||
} | ||
.into()) | ||
} | ||
|
||
/// Params: | ||
/// joined_struct: The struct to implement `Joined`. | ||
/// item_struct: The associated `Item` type to use for the `Joined` implementation. | ||
fn impl_joined( | ||
joined_struct: &ItemStruct, | ||
item_struct: &ItemStruct, | ||
) -> Result<proc_macro2::TokenStream> { | ||
let struct_ident = &joined_struct.ident; | ||
let item_struct_ident = &item_struct.ident; | ||
let (impl_generics, ty_generics, where_clause) = item_struct.generics.split_for_impl(); | ||
let (field_ident, _) = get_fields_map(&item_struct.fields)?; | ||
|
||
Ok(quote! { | ||
impl #impl_generics ::bevy_impulse::Joined for #struct_ident #ty_generics #where_clause { | ||
type Item = #item_struct_ident #ty_generics; | ||
|
||
fn pull(&self, session: ::bevy_ecs::prelude::Entity, world: &mut ::bevy_ecs::prelude::World) -> Result<Self::Item, ::bevy_impulse::OperationError> { | ||
#( | ||
let #field_ident = self.#field_ident.pull(session, world)?; | ||
)* | ||
|
||
Ok(Self::Item {#( | ||
#field_ident, | ||
)*}) | ||
} | ||
} | ||
}.into()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather not have
builder
as an argument. I think we should leave it to users to calloutput.into_buffer(builder)
from outside ofselect_buffers
and haveselect_buffers
just take in the specific buffer types that are expected. This will especially matter for supporting specialized buffer types likeAnyBuffer
andJsonBuffer
.