Skip to content
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

Support for dynamic dictionaries of buffers #52

Merged
merged 39 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
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 Jan 23, 2025
d9b9be1
Refactoring Buffered trait
mxgrey Jan 24, 2025
763244b
Figuring out how to dynamically access buffers
mxgrey Jan 24, 2025
96953c5
Prototype of AnyBufferMut
mxgrey Jan 27, 2025
f3ddbf5
Any buffer access is working
mxgrey Jan 29, 2025
9935817
Add AnyBuffer drain and restructure any_buffer module
mxgrey Jan 29, 2025
442c676
Clean up any_buffer implementation
mxgrey Jan 29, 2025
d44e383
Introducing JsonBuffer type
mxgrey Jan 29, 2025
70cec2c
Refactoring the use of AnyBufferStorageAccessInterface
mxgrey Jan 30, 2025
8f79d53
Fix global retention of AnyBufferAccessInterface
mxgrey Jan 30, 2025
b21c218
Fleshing out implementation of JsonBuffer
mxgrey Jan 30, 2025
792e0b6
Almost done with JsonBuffer
mxgrey Jan 31, 2025
ae65f24
Implemented Joined and Accessed for AnyBuffer and JsonBuffer
mxgrey Feb 2, 2025
7c9d490
Add tests for JsonBuffer
mxgrey Feb 2, 2025
0d1224f
Implement JsonBufferView
mxgrey Feb 2, 2025
5dcae45
Implement AnyBufferView
mxgrey Feb 2, 2025
ca7a226
Draft an example implementation of JoinedValue
mxgrey Feb 2, 2025
096ccf4
Fix formatting
mxgrey Feb 2, 2025
800a112
Remove use of 1.75 unstable function
mxgrey Feb 2, 2025
de6596d
Implement more general buffer downcasting
mxgrey Feb 3, 2025
bd32a47
fix doc comments
mxgrey Feb 3, 2025
c419ab3
Refactor buffer keys
mxgrey Feb 3, 2025
a6bf08d
Implement more general buffer key downcasting
mxgrey Feb 3, 2025
a40b8d3
Invert the implementation of JoinedValue
mxgrey Feb 6, 2025
617e22f
Refactor Bufferable so we no longer need join_into
mxgrey Feb 6, 2025
4c087ea
Fix style
mxgrey Feb 6, 2025
858c7e6
Update docs
mxgrey Feb 6, 2025
aa22f87
Fix style
mxgrey Feb 6, 2025
5dd9a30
Merge branch 'main' into buffer_map
mxgrey Feb 14, 2025
f64871e
add derive `JoinedValue` to implement trait automatically (#53)
koonpeng Feb 17, 2025
e94c170
Generalize buffer map keys
mxgrey Feb 17, 2025
6e19af7
Support for joining Vec and json values
mxgrey Feb 17, 2025
4e35ec8
Working towards BufferKeyMap implementation
mxgrey Feb 18, 2025
87387e3
BufferKeyMap macro (#56)
mxgrey Feb 20, 2025
47bd4d0
Merge branch 'main' into buffer_map
mxgrey Feb 20, 2025
c096209
Rename traits to improve semantics (#57)
mxgrey Feb 20, 2025
911a5e1
Tighten up leaks
mxgrey Feb 21, 2025
8a7603c
Fix style
mxgrey Feb 21, 2025
530c92f
Iterate on require buffer functions
mxgrey Feb 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci_linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ jobs:
run: cargo test --features single_threaded_async

- name: Build docs
run: cargo doc
run: cargo doc --all-features

1 change: 1 addition & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ proc-macro = true
[dependencies]
syn = "2.0"
quote = "1.0"
proc-macro2 = "1.0.93"
273 changes: 273 additions & 0 deletions macros/src/buffer.rs
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 = &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())
}
20 changes: 19 additions & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
*
*/

mod buffer;
use buffer::impl_joined_value;

use proc_macro::TokenStream;
use quote::quote;
use syn::DeriveInput;
use syn::{parse_macro_input, DeriveInput, ItemStruct};

#[proc_macro_derive(Stream)]
pub fn simple_stream_macro(item: TokenStream) -> TokenStream {
Expand Down Expand Up @@ -58,3 +61,18 @@ pub fn delivery_label_macro(item: TokenStream) -> TokenStream {
}
.into()
}

/// The result error is the compiler error message to be displayed.
type Result<T> = std::result::Result<T, String>;

#[proc_macro_derive(JoinedValue, attributes(joined))]
pub fn derive_joined_value(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as ItemStruct);
match impl_joined_value(&input) {
Ok(tokens) => tokens.into(),
Err(msg) => quote! {
compile_error!(#msg);
}
.into(),
}
}
Loading