-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
231 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,170 @@ | ||
use { | ||
crate::member, | ||
proc_macro2::TokenStream, | ||
syn::{spanned::Spanned, Data, DataStruct, DeriveInput}, | ||
}; | ||
|
||
pub(crate) fn derive(input: DeriveInput) -> TokenStream { | ||
use std::iter; | ||
|
||
let Data::Struct(DataStruct { fields, .. }) = input.data else { | ||
return quote::quote_spanned! { input.ident.span() => | ||
::std::compile_error!("the instance type must be a struct"); | ||
}; | ||
}; | ||
|
||
if !input.generics.params.is_empty() { | ||
return quote::quote_spanned! { input.generics.params.span() => | ||
::std::compile_error!("the instance struct cannot have generic parameters"); | ||
}; | ||
} | ||
|
||
if fields.is_empty() { | ||
return quote::quote_spanned! { fields.span() => | ||
::std::compile_error!("the instance struct must have some fields"); | ||
}; | ||
} | ||
|
||
let name = input.ident; | ||
let projection_name = quote::format_ident!("{name}Projection"); | ||
let instance_types = fields.iter().map(|field| { | ||
let ty = &field.ty; | ||
quote::quote! { <#ty as ::dunge::instance::MemberProjection>::TYPE } | ||
}); | ||
|
||
let instance_set_members = iter::zip(0.., &fields).map(|(index, field)| { | ||
let ident = member::make(index, field.ident.clone()); | ||
quote::quote! { ::dunge::instance::SetMember::set_member(&self.#ident, setter) } | ||
}); | ||
|
||
let instance_fields = iter::zip(0.., &fields).map(|(index, field)| { | ||
let ident = member::make(index, field.ident.clone()); | ||
let ty = &field.ty; | ||
quote::quote! { #ident: <#ty as ::dunge::instance::MemberProjection>::Field } | ||
}); | ||
|
||
let instance_member_projections = iter::zip(0.., &fields).map(|(index, field)| { | ||
let ident = member::make(index, field.ident.clone()); | ||
let ty = &field.ty; | ||
quote::quote! { #ident: <#ty as ::dunge::instance::MemberProjection>::member_projection(id + #index) } | ||
}); | ||
|
||
quote::quote! { | ||
impl ::dunge::Instance for #name { | ||
type Projection = #projection_name; | ||
const DEF: ::dunge::sl::Define<::dunge::types::VectorType> = ::dunge::sl::Define::new(&[ | ||
#(#instance_types),*, | ||
]); | ||
} | ||
|
||
impl ::dunge::instance::Set for #name { | ||
fn set<'p>(&'p self, setter: &mut ::dunge::instance::Setter<'_, 'p>) { | ||
#(#instance_set_members);*; | ||
} | ||
} | ||
|
||
struct #projection_name { | ||
#(#instance_fields),*, | ||
} | ||
|
||
impl ::dunge::instance::Projection for #projection_name { | ||
fn projection(id: ::core::primitive::u32) -> Self { | ||
Self { | ||
#(#instance_member_projections),*, | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn derive_instance() { | ||
let input = quote::quote! { | ||
struct Transform { | ||
pos: Row<[f32; 2]>, | ||
col: Row<[f32; 3]>, | ||
} | ||
}; | ||
|
||
let input = syn::parse2(input).expect("parse input"); | ||
let actual = derive(input); | ||
let expected = quote::quote! { | ||
impl ::dunge::Instance for Transform { | ||
type Projection = TransformProjection; | ||
const DEF: ::dunge::sl::Define<::dunge::types::VectorType> = ::dunge::sl::Define::new(&[ | ||
<Row<[f32; 2]> as ::dunge::instance::MemberProjection>::TYPE, | ||
<Row<[f32; 3]> as ::dunge::instance::MemberProjection>::TYPE, | ||
]); | ||
} | ||
|
||
impl ::dunge::instance::Set for Transform { | ||
fn set<'p>(&'p self, setter: &mut ::dunge::instance::Setter<'_, 'p>) { | ||
::dunge::instance::SetMember::set_member(&self.pos, setter); | ||
::dunge::instance::SetMember::set_member(&self.col, setter); | ||
} | ||
} | ||
|
||
struct TransformProjection { | ||
pos: <Row<[f32; 2]> as ::dunge::instance::MemberProjection>::Field, | ||
col: <Row<[f32; 3]> as ::dunge::instance::MemberProjection>::Field, | ||
} | ||
|
||
impl ::dunge::instance::Projection for TransformProjection { | ||
fn projection(id: ::core::primitive::u32) -> Self { | ||
Self { | ||
pos: <Row<[f32; 2]> as ::dunge::instance::MemberProjection>::member_projection(id + 0u32), | ||
col: <Row<[f32; 3]> as ::dunge::instance::MemberProjection>::member_projection(id + 1u32), | ||
} | ||
} | ||
} | ||
}; | ||
|
||
assert_eq!(actual.to_string(), expected.to_string()); | ||
} | ||
|
||
#[test] | ||
fn derive_tuple_instance() { | ||
let input = quote::quote! { | ||
struct Transform(Row<[f32; 2]>, Row<[f32; 3]>); | ||
}; | ||
|
||
let input = syn::parse2(input).expect("parse input"); | ||
let actual = derive(input); | ||
let expected = quote::quote! { | ||
impl ::dunge::Instance for Transform { | ||
type Projection = TransformProjection; | ||
const DEF: ::dunge::sl::Define<::dunge::types::VectorType> = ::dunge::sl::Define::new(&[ | ||
<Row<[f32; 2]> as ::dunge::instance::MemberProjection>::TYPE, | ||
<Row<[f32; 3]> as ::dunge::instance::MemberProjection>::TYPE, | ||
]); | ||
} | ||
|
||
impl ::dunge::instance::Set for Transform { | ||
fn set<'p>(&'p self, setter: &mut ::dunge::instance::Setter<'_, 'p>) { | ||
::dunge::instance::SetMember::set_member(&self.0, setter); | ||
::dunge::instance::SetMember::set_member(&self.1, setter); | ||
} | ||
} | ||
|
||
struct TransformProjection { | ||
0: <Row<[f32; 2]> as ::dunge::instance::MemberProjection>::Field, | ||
1: <Row<[f32; 3]> as ::dunge::instance::MemberProjection>::Field, | ||
} | ||
|
||
impl ::dunge::instance::Projection for TransformProjection { | ||
fn projection(id: ::core::primitive::u32) -> Self { | ||
Self { | ||
0: <Row<[f32; 2]> as ::dunge::instance::MemberProjection>::member_projection(id + 0u32), | ||
1: <Row<[f32; 3]> as ::dunge::instance::MemberProjection>::member_projection(id + 1u32), | ||
} | ||
} | ||
} | ||
}; | ||
|
||
assert_eq!(actual.to_string(), expected.to_string()); | ||
} | ||
} |
This file contains 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