Skip to content

Commit

Permalink
Generates an iterator for the SOA (#134)
Browse files Browse the repository at this point in the history
* Generates an iterator for the SOA

It makes it easier to use for point by point algorithms.

* fix viz in tests
  • Loading branch information
gbin authored Dec 5, 2024
1 parent d21d557 commit 7a9b8e3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
38 changes: 38 additions & 0 deletions core/cu29_soa_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,38 @@ pub fn derive_soa(input: TokenStream) -> TokenStream {
}
}

let soa_struct_name_iterator = format_ident!("{}Iterator", name);

let iterator = quote! {
pub struct #soa_struct_name_iterator<'a, const N: usize> {
soa_struct: &'a #soa_struct_name<N>,
current: usize,
}

impl<'a, const N: usize> #soa_struct_name_iterator<'a, N> {
pub fn new(soa_struct: &'a #soa_struct_name<N>) -> Self {
Self {
soa_struct,
current: 0,
}
}
}

impl<'a, const N: usize> Iterator for #soa_struct_name_iterator<'a, N> {
type Item = super::#name;

fn next(&mut self) -> Option<Self::Item> {
if self.current < self.soa_struct.len {
let item = self.soa_struct.get(self.current); // Reuse `get` method
self.current += 1;
Some(item)
} else {
None
}
}
}
};

let expanded = quote! {
#visibility mod #module_name {
use bincode::{Decode, Encode};
Expand Down Expand Up @@ -209,6 +241,10 @@ pub fn derive_soa(input: TokenStream) -> TokenStream {
}
}

pub fn iter(&self) -> #soa_struct_name_iterator<N> {
#soa_struct_name_iterator::new(self)
}

#(
pub fn #field_names(&self) -> &[#field_types] {
&self.#field_names
Expand Down Expand Up @@ -272,6 +308,8 @@ pub fn derive_soa(input: TokenStream) -> TokenStream {
}
}

#iterator

}
#visibility use #module_name::#soa_struct_name;
};
Expand Down
2 changes: 1 addition & 1 deletion core/cu29_soa_derive/tests/proctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests {
use bincode::{Decode, Encode};
use cu29_soa_derive::Soa;
#[derive(Debug, Clone, Default, PartialEq, Soa, Encode, Decode)]
pub(crate) struct Xyz {
pub struct Xyz {
x: f32,
y: f32,
z: f32,
Expand Down

0 comments on commit 7a9b8e3

Please sign in to comment.