Macro soa_derive::soa_zip
source · macro_rules! soa_zip { ($self: expr, [$($fields: tt)*] $(, $external: expr)* $(,)*) => { ... }; }
Expand description
Create an iterator over multiple fields in a Struct of array style vector.
This macro takes two main arguments: the array/slice container, and a list
of fields to use, inside square brackets. The iterator will give references
to the fields, which can be mutable references if the field name is prefixed
with mut
.
#[derive(StructOfArray)]
struct Cheese {
size: f64,
mass: f64,
smell: f64,
name: String,
}
let mut vec = CheeseVec::new();
// fill the vector
// Iterate over immutable references
for (mass, size, name) in soa_zip!(&vec, [mass, size, name]) {
println!("got {} kg and {} cm of {}", mass, size, name);
}
// Iterate over mutable references
for (mass, name) in soa_zip!(&mut vec, [mut mass, name]) {
println!("got {} kg of {}, eating 1 kg", mass, name);
*mass -= 1.0;
}
The iterator can also work with external iterators. In this case, the iterator will yields elements until any of the fields or one external iterator returns None.
let mut vec = CheeseVec::new();
let mut cellars = Vec::<Cellar>::new();
for (name, mass, cellar) in soa_zip!(&vec, [name, mass], &cellars) {
println!("we have {} kg of {} in {:#?}", mass, name, cellar);
}