Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Extend for Vec #64

Merged
merged 3 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions soa-derive-internal/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pub fn derive(input: &Input) -> TokenStream {
.map(|field| field.ident.clone().unwrap())
.collect::<Vec<_>>();

let fields_types = &input.fields.iter()
.map(|field| field.ty.clone())
.collect::<Vec<_>>();

let iter_type = input.map_fields_nested_or(
|_, field_type| quote! { <#field_type as soa_derive::SoAIter<'a>>::Iter },
|_, field_type| quote! { slice::Iter<'a, #field_type> },
Expand Down Expand Up @@ -289,6 +293,24 @@ pub fn derive(input: &Input) -> TokenStream {
self.as_mut_slice().into_iter()
}
}

impl Extend<#name> for #vec_name {
fn extend<I: IntoIterator<Item = #name>>(&mut self, iter: I) {
for item in iter {
self.push(item)
}
}
}

impl<'a> Extend<#ref_name<'a>> for #vec_name
// only expose if all fields are Clone
// https://github.com/rust-lang/rust/issues/48214#issuecomment-1150463333
where #( for<'b> #fields_types: Clone, )*
{
fn extend<I: IntoIterator<Item = #ref_name<'a>>>(&mut self, iter: I) {
self.extend(iter.into_iter().map(|item| item.to_owned()))
}
}
});
}

Expand Down
4 changes: 2 additions & 2 deletions soa-derive-internal/src/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub fn derive(input: &Input) -> TokenStream {
/// into an owned value. This is only available if all fields
/// implement `Clone`.
pub fn to_owned(&self) -> #name
// only expose to_owned is all fields are Clone
// only expose to_owned if all fields are Clone
// https://github.com/rust-lang/rust/issues/48214#issuecomment-1150463333
where #( for<'b> #fields_types: Clone, )*
{
Expand All @@ -138,7 +138,7 @@ pub fn derive(input: &Input) -> TokenStream {
/// into an owned value. This is only available if all fields
/// implement `Clone`.
pub fn to_owned(&self) -> #name
// only expose to_owned is all fields are Clone
// only expose to_owned if all fields are Clone
// https://github.com/rust-lang/rust/issues/48214#issuecomment-1150463333
where #( for<'b> #fields_types: Clone, )*
{
Expand Down
16 changes: 16 additions & 0 deletions tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,19 @@ fn from_iter() {

assert_eq!(particles, particles_from_iter)
}

#[test]
fn extend() {
let vec_with_particles = vec![
Particle::new(String::from("Na"), 0.0),
Particle::new(String::from("Cl"), 0.0),
Particle::new(String::from("Zn"), 0.0),
];

let particles_from_iter: ParticleVec = vec_with_particles.clone().into_iter().collect();

let mut particles = ParticleVec::new();
particles.extend(vec_with_particles);

assert_eq!(particles, particles_from_iter)
}
Loading