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

Always implement Default for Vec #63

Merged
merged 3 commits into from
Mar 18, 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
5 changes: 4 additions & 1 deletion soa-derive-internal/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ impl Input {
match meta.path.get_ident() {
Some(ident) => {
assert!(ident != "Copy", "can not derive Copy for SoA vectors");
extra_attrs.add_derive(ident);
if ident != "Default" {
// ignore as Default is already derived for SoA vectors, slices and mut slices
extra_attrs.add_derive(ident);
}
}
None => {
panic!(
Expand Down
2 changes: 2 additions & 0 deletions soa-derive-internal/src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub fn derive(input: &Input) -> TokenStream {
#[allow(dead_code)]
#[derive(Copy, Clone)]
#(#[#attrs])*
#[derive(Default)]
#visibility struct #slice_name<'a> {
#(
/// slice of `
Expand Down Expand Up @@ -337,6 +338,7 @@ pub fn derive_mut(input: &Input) -> TokenStream {
/// .
#[allow(dead_code)]
#(#[#attrs])*
#[derive(Default)]
#visibility struct #slice_mut_name<'a> {
#(
/// slice of `
Expand Down
10 changes: 2 additions & 8 deletions soa-derive-internal/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ pub fn derive(input: &Input) -> TokenStream {
|_, field_type| quote! { Vec<#field_type> },
).collect::<Vec<_>>();

let vec_new = input.map_fields_nested_or(
|_, field_type| quote! { <#field_type as StructOfArray>::Type::new()},
|_, _| quote! { Vec::new() },
).collect::<Vec<_>>();

let vec_with_capacity = input.map_fields_nested_or(
|_, field_type| quote! { <#field_type as StructOfArray>::Type::with_capacity(capacity) },
|_, _| quote! { Vec::with_capacity(capacity) },
Expand Down Expand Up @@ -74,6 +69,7 @@ pub fn derive(input: &Input) -> TokenStream {
/// ` with Struct of Array (SoA) layout
#[allow(dead_code)]
#(#[#attrs])*
#[derive(Default)]
#visibility struct #vec_name {
#(
/// a vector of `
Expand All @@ -91,9 +87,7 @@ pub fn derive(input: &Input) -> TokenStream {
#[doc = #vec_name_str]
/// ::new()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.new)
pub fn new() -> #vec_name {
#vec_name {
#( #fields_names: #vec_new, )*
}
Default::default()
}

/// Similar to [`
Expand Down
Loading