File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff 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>;
Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments