Skip to content

Commit c1813a5

Browse files
CGMossaLuthaf
authored andcommitted
Added FromIterator
Added a from iterator, and added a test case.
1 parent 3a14eb2 commit c1813a5

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

soa-derive-internal/src/iter.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,19 @@ pub fn derive(input: &Input) -> TokenStream {
202202
}
203203
}
204204

205+
206+
impl<'a> std::iter::FromIterator<&'a #name> for #vec_name {
207+
fn from_iter<T: IntoIterator<Item=&'a #name>>(iter: T) -> Self {
208+
let mut result = #vec_name::new();
209+
for element in iter {
210+
#(
211+
(result.#fields_names).push(element.#fields_names.clone());
212+
)*
213+
}
214+
result
215+
}
216+
}
217+
205218
impl<'a, 'b> IntoIterator for &'a #slice_name<'b> {
206219
type Item = #ref_name<'a>;
207220
type IntoIter = #detail_mod::Iter<'a>;

tests/iter.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,20 @@ fn iter_mut() {
5050
assert_eq!(particles.mass[1], 2.0);
5151
assert_eq!(particles.mass[2], 2.0);
5252
}
53+
54+
#[test]
55+
fn from_iter() {
56+
let mut vec_with_particles = Vec::new();
57+
vec_with_particles.push(Particle::new(String::from("Na"), 0.0));
58+
vec_with_particles.push(Particle::new(String::from("Cl"), 0.0));
59+
vec_with_particles.push(Particle::new(String::from("Zn"), 0.0));
60+
61+
let particles_from_iter: ParticleVec = vec_with_particles.iter().collect();
62+
63+
let mut particles = ParticleVec::new();
64+
particles.push(Particle::new(String::from("Na"), 0.0));
65+
particles.push(Particle::new(String::from("Cl"), 0.0));
66+
particles.push(Particle::new(String::from("Zn"), 0.0));
67+
68+
assert_eq!(particles, particles_from_iter)
69+
}

0 commit comments

Comments
 (0)