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 2 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
1 change: 1 addition & 0 deletions soa-derive-internal/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ impl Input {
match meta.path.get_ident() {
Some(ident) => {
assert!(ident != "Copy", "can not derive Copy for SoA vectors");
assert!(ident != "Default", "Default is already derived for SoA vectors");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes this PR a breaking change.

extra_attrs.add_derive(ident);
}
None => {
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