Skip to content

Commit ccd6af3

Browse files
authored
Use associate type to link the generated soa struct with original struct
1 parent e4ce15a commit ccd6af3

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ few helper structs: `CheeseSlice`, `CheeseSliceMut`, `CheeseRef` and
3333
`CheeseRefMut` corresponding respectivly to `&[Cheese]`, `&mut [Cheese]`,
3434
`&Cheese` and `&mut Cheese`.
3535

36+
Any struct derived by StructOfArray will auto impl trait `StructOfArray`,
37+
You can use `<Cheese as StructOfArray>::Type` instead of explicit named type `CheeseVec`;
38+
3639
## How to use it
3740

3841
Add `#[derive(StructOfArray)]` to each struct you want to derive a struct of

soa-derive-internal/src/lib.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use proc_macro2::TokenStream;
1010
use quote::TokenStreamExt;
1111

1212
mod input;
13-
mod vec;
14-
mod refs;
13+
mod iter;
1514
mod ptr;
15+
mod refs;
1616
mod slice;
17-
mod iter;
17+
mod vec;
1818

1919
#[proc_macro_derive(StructOfArray, attributes(soa_derive))]
2020
pub fn soa_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
@@ -28,5 +28,19 @@ pub fn soa_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
2828
generated.append_all(slice::derive(&input));
2929
generated.append_all(slice::derive_mut(&input));
3030
generated.append_all(iter::derive(&input));
31+
generated.append_all(derive_trait(&input));
3132
generated.into()
3233
}
34+
35+
use crate::input::Input;
36+
use quote::quote;
37+
fn derive_trait(input: &Input) -> TokenStream {
38+
let name = &input.name;
39+
let vec_name = &input.vec_name();
40+
41+
quote! {
42+
impl soa_derive::StructOfArray for #name {
43+
type Type = #vec_name;
44+
}
45+
}
46+
}

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@
160160
// macro_rules macro.
161161
pub use soa_derive_internal::StructOfArray;
162162

163+
/// Any struct derived by StructOfArray will auto impl this trait
164+
/// You can use `<Cheese as StructOfArray>::Type`
165+
/// instead of explicit named type `CheeseVec`; This will helpful in generics programing
166+
/// that generate struct can be expressed as `<T as StructOfArray>::Type`
167+
pub trait StructOfArray {
168+
type Type;
169+
}
170+
163171
/// Create an iterator over multiple fields in a Struct of array style vector.
164172
///
165173
/// This macro takes two main arguments: the array/slice container, and a list

tests/vec.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
mod particles;
22
use self::particles::{Particle, ParticleVec};
3+
use soa_derive::StructOfArray;
4+
5+
#[test]
6+
fn ty() {
7+
let _: <Particle as StructOfArray>::Type = ParticleVec::new();
8+
}
39

410
#[test]
511
fn push() {

0 commit comments

Comments
 (0)