-
Notifications
You must be signed in to change notification settings - Fork 2
Support for dynamic dictionaries of buffers #52
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 32 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
36d604b
Introducing the concept of a buffer map
mxgrey d9b9be1
Refactoring Buffered trait
mxgrey 763244b
Figuring out how to dynamically access buffers
mxgrey 96953c5
Prototype of AnyBufferMut
mxgrey f3ddbf5
Any buffer access is working
mxgrey 9935817
Add AnyBuffer drain and restructure any_buffer module
mxgrey 442c676
Clean up any_buffer implementation
mxgrey d44e383
Introducing JsonBuffer type
mxgrey 70cec2c
Refactoring the use of AnyBufferStorageAccessInterface
mxgrey 8f79d53
Fix global retention of AnyBufferAccessInterface
mxgrey b21c218
Fleshing out implementation of JsonBuffer
mxgrey 792e0b6
Almost done with JsonBuffer
mxgrey ae65f24
Implemented Joined and Accessed for AnyBuffer and JsonBuffer
mxgrey 7c9d490
Add tests for JsonBuffer
mxgrey 0d1224f
Implement JsonBufferView
mxgrey 5dcae45
Implement AnyBufferView
mxgrey ca7a226
Draft an example implementation of JoinedValue
mxgrey 096ccf4
Fix formatting
mxgrey 800a112
Remove use of 1.75 unstable function
mxgrey de6596d
Implement more general buffer downcasting
mxgrey bd32a47
fix doc comments
mxgrey c419ab3
Refactor buffer keys
mxgrey a6bf08d
Implement more general buffer key downcasting
mxgrey a40b8d3
Invert the implementation of JoinedValue
mxgrey 617e22f
Refactor Bufferable so we no longer need join_into
mxgrey 4c087ea
Fix style
mxgrey 858c7e6
Update docs
mxgrey aa22f87
Fix style
mxgrey 5dd9a30
Merge branch 'main' into buffer_map
mxgrey f64871e
add derive `JoinedValue` to implement trait automatically (#53)
koonpeng e94c170
Generalize buffer map keys
mxgrey 6e19af7
Support for joining Vec and json values
mxgrey 4e35ec8
Working towards BufferKeyMap implementation
mxgrey 87387e3
BufferKeyMap macro (#56)
mxgrey 47bd4d0
Merge branch 'main' into buffer_map
mxgrey c096209
Rename traits to improve semantics (#57)
mxgrey 911a5e1
Tighten up leaks
mxgrey 8a7603c
Fix style
mxgrey 530c92f
Iterate on require buffer functions
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
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,273 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::{format_ident, quote}; | ||
use syn::{parse_quote, Field, Generics, Ident, ItemStruct, Type, TypePath}; | ||
|
||
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 StructConfig { | ||
buffer_struct_name: buffer_struct_ident, | ||
} = StructConfig::from_data_struct(&input_struct); | ||
let buffer_struct_vis = &input_struct.vis; | ||
|
||
let (field_ident, _, field_config) = get_fields_map(&input_struct.fields)?; | ||
let buffer: Vec<&Type> = field_config.iter().map(|config| &config.buffer).collect(); | ||
let noncopy = field_config.iter().any(|config| config.noncopy); | ||
|
||
let buffer_struct: ItemStruct = parse_quote! { | ||
#[allow(non_camel_case_types, unused)] | ||
#buffer_struct_vis struct #buffer_struct_ident #impl_generics #where_clause { | ||
#( | ||
#buffer_struct_vis #field_ident: #buffer, | ||
)* | ||
} | ||
}; | ||
|
||
let buffer_clone_impl = if noncopy { | ||
// Clone impl for structs with a buffer that is not copyable | ||
quote! { | ||
impl #impl_generics ::std::clone::Clone for #buffer_struct_ident #ty_generics #where_clause { | ||
fn clone(&self) -> Self { | ||
Self { | ||
#( | ||
#field_ident: self.#field_ident.clone(), | ||
)* | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
// Clone and copy impl for structs with buffers that are all copyable | ||
quote! { | ||
impl #impl_generics ::std::clone::Clone for #buffer_struct_ident #ty_generics #where_clause { | ||
fn clone(&self) -> Self { | ||
*self | ||
} | ||
} | ||
|
||
impl #impl_generics ::std::marker::Copy for #buffer_struct_ident #ty_generics #where_clause {} | ||
} | ||
}; | ||
|
||
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 | ||
|
||
#buffer_clone_impl | ||
|
||
impl #impl_generics #struct_ident #ty_generics #where_clause { | ||
fn select_buffers( | ||
#( | ||
#field_ident: #buffer, | ||
)* | ||
) -> #buffer_struct_ident #ty_generics { | ||
#buffer_struct_ident { | ||
#( | ||
#field_ident, | ||
)* | ||
} | ||
} | ||
} | ||
|
||
#impl_buffer_map_layout | ||
|
||
#impl_joined | ||
}; | ||
|
||
Ok(gen.into()) | ||
} | ||
|
||
/// Code that are currently unused but could be used in the future, move them out of this mod if | ||
/// they are ever used. | ||
#[allow(unused)] | ||
mod _unused { | ||
use super::*; | ||
|
||
/// Converts a list of generics to a [`PhantomData`] TypePath. | ||
/// e.g. `::std::marker::PhantomData<fn(T,)>` | ||
fn to_phantom_data(generics: &Generics) -> TypePath { | ||
let lifetimes: Vec<Type> = generics | ||
.lifetimes() | ||
.map(|lt| { | ||
let lt = <.lifetime; | ||
let ty: Type = parse_quote! { & #lt () }; | ||
ty | ||
}) | ||
.collect(); | ||
let ty_params: Vec<&Ident> = generics.type_params().map(|ty| &ty.ident).collect(); | ||
parse_quote! { ::std::marker::PhantomData<fn(#(#lifetimes,)* #(#ty_params,)*)> } | ||
} | ||
} | ||
|
||
struct StructConfig { | ||
buffer_struct_name: Ident, | ||
} | ||
|
||
impl StructConfig { | ||
fn from_data_struct(data_struct: &ItemStruct) -> Self { | ||
let mut config = Self { | ||
buffer_struct_name: format_ident!("__bevy_impulse_{}_Buffers", data_struct.ident), | ||
}; | ||
|
||
let attr = data_struct | ||
.attrs | ||
.iter() | ||
.find(|attr| attr.path().is_ident("joined")); | ||
|
||
if let Some(attr) = attr { | ||
attr.parse_nested_meta(|meta| { | ||
if meta.path.is_ident("buffers_struct_name") { | ||
config.buffer_struct_name = meta.value()?.parse()?; | ||
} | ||
Ok(()) | ||
}) | ||
// panic if attribute is malformed, this will result in a compile error which is intended. | ||
.unwrap(); | ||
} | ||
|
||
config | ||
} | ||
} | ||
|
||
struct FieldConfig { | ||
buffer: Type, | ||
noncopy: bool, | ||
} | ||
|
||
impl FieldConfig { | ||
fn from_field(field: &Field) -> Self { | ||
let ty = &field.ty; | ||
let mut config = Self { | ||
buffer: parse_quote! { ::bevy_impulse::Buffer<#ty> }, | ||
noncopy: false, | ||
}; | ||
|
||
for attr in field | ||
.attrs | ||
.iter() | ||
.filter(|attr| attr.path().is_ident("joined")) | ||
{ | ||
attr.parse_nested_meta(|meta| { | ||
if meta.path.is_ident("buffer") { | ||
config.buffer = meta.value()?.parse()?; | ||
} | ||
if meta.path.is_ident("noncopy_buffer") { | ||
config.noncopy = true; | ||
} | ||
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>, Vec<FieldConfig>)> { | ||
match fields { | ||
syn::Fields::Named(data) => { | ||
let mut idents = Vec::new(); | ||
let mut types = Vec::new(); | ||
let mut configs = 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); | ||
configs.push(FieldConfig::from_field(field)); | ||
} | ||
Ok((idents, types, configs)) | ||
} | ||
_ => 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_config) = get_fields_map(&item_struct.fields)?; | ||
let buffer: Vec<&Type> = field_config.iter().map(|config| &config.buffer).collect(); | ||
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 try_from_buffer_map(buffers: &::bevy_impulse::BufferMap) -> Result<Self, ::bevy_impulse::IncompatibleLayout> { | ||
let mut compatibility = ::bevy_impulse::IncompatibleLayout::default(); | ||
#( | ||
let #field_ident = compatibility.require_buffer_by_literal::<#buffer>(#map_key, buffers); | ||
)* | ||
|
||
// Unwrap the Ok after inspecting every field so that the | ||
// IncompatibleLayout error can include all information about | ||
// which fields were incompatible. | ||
#( | ||
let Ok(#field_ident) = #field_ident else { | ||
return Err(compatibility); | ||
}; | ||
)* | ||
|
||
Ok(Self { | ||
#( | ||
#field_ident, | ||
)* | ||
}) | ||
} | ||
} | ||
|
||
impl #impl_generics ::bevy_impulse::BufferMapStruct for #struct_ident #ty_generics #where_clause { | ||
fn buffer_list(&self) -> ::smallvec::SmallVec<[AnyBuffer; 8]> { | ||
use smallvec::smallvec; | ||
smallvec![#( | ||
::bevy_impulse::AsAnyBuffer::as_any_buffer(&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.
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.
Uh oh!
There was an error while loading. Please reload this page.