Skip to content

#[soa_attr] on field #43

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,36 @@ If you want to add attribute to a specific generated struct(such as
attribute `#[soa_attr(Vec, cfg_attr(test, derive(PartialEq)))]` to the
struct declaration.

`soa_attr` could also be added to specific generated struct's field.

For example:

```rust
#[derive(Debug, PartialEq, StructOfArray)]
#[soa_attr(Vec, cfg_attr(test, derive(PartialEq)))]
pub struct Cheese {
#[soa_attr(Vec, deprecated)]
pub smell: f64,
pub color: (f64, f64, f64),
pub with_mushrooms: bool,
pub name: String,
}
```

will generate the struct like:

```rust
#[cfg_attr(test, derive(PartialEq))]
pub struct CheeseVec {
#[deprecated]
pub smell: Vec<f64>,
pub color: Vec<(f64, f64, f64)>,
pub with_mushrooms: Vec<bool>,
pub name: Vec<String>,
}
```


Mappings for first argument of ``soa_attr`` to the generated struct for ``Cheese``:
* `Vec` => `CheeseVec`
* `Slice` => `CheeseSlice`
Expand All @@ -79,6 +98,9 @@ Mappings for first argument of ``soa_attr`` to the generated struct for ``Cheese
* `Ptr` => `CheesePtr`
* `PtrMut` => `CheesePtrMut`


To be mentioned, there is an [issue](https://github.com/lumol-org/soa-derive/issues/44) related to `#[soa_attr(Vec, serde(skip))]` on field.

## Usage and API

All the generated code have some generated documentation with it, so you
Expand Down
35 changes: 27 additions & 8 deletions soa-derive-internal/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ pub struct Input {
pub name: Ident,
/// The list of fields in the struct
pub fields: Vec<Field>,
/// Additional attributes requested with `#[soa_attr(...)]` on fields
pub field_attrs: Vec<ExtraAttributes>,
/// The struct overall visibility
pub visibility: Visibility,
/// Additional attributes requested with `#[soa_attr(...)]` or
/// `#[soa_derive()]`
pub attrs: ExtraAttributes,
}

pub struct ExtraAttributes {
// did the user explicitly asked us to derive clone?
pub derive_clone: bool,
}

pub struct ExtraAttributes {
pub vec: Vec<Meta>,
pub slice: Vec<Meta>,
pub slice_mut: Vec<Meta>,
Expand All @@ -35,7 +37,6 @@ pub struct ExtraAttributes {
impl ExtraAttributes {
fn new() -> ExtraAttributes {
ExtraAttributes {
derive_clone: false,
vec: Vec::new(),
slice: Vec::new(),
slice_mut: Vec::new(),
Expand Down Expand Up @@ -130,9 +131,6 @@ impl ExtraAttributes {
// always add this derive to the Vec struct
self.vec.push(derive);

if path.is_ident("Clone") {
self.derive_clone = true;
}
}
}

Expand All @@ -149,8 +147,23 @@ fn create_derive_meta(path: Path) -> Meta {

impl Input {
pub fn new(input: DeriveInput) -> Input {
let fields = match input.data {
Data::Struct(s) => s.fields.iter().cloned().collect::<Vec<_>>(),
let mut fields = Vec::new();
let mut field_attrs = Vec::new();
match input.data {
Data::Struct(s) => {
for field in s.fields.iter() {
let mut extra_attrs = ExtraAttributes::new();
fields.push(field.clone());
for attr in &field.attrs {
if let Ok(meta) = attr.parse_meta() {
if meta.path().is_ident("soa_attr") {
extra_attrs.parse(&meta);
}
}
}
field_attrs.push(extra_attrs);
}
}
_ => panic!("#[derive(StructOfArray)] only supports struct"),
};

Expand All @@ -160,6 +173,7 @@ impl Input {

let mut extra_attrs = ExtraAttributes::new();

let mut derive_clone = false;
for attr in input.attrs {
if let Ok(meta) = attr.parse_meta() {
if meta.path().is_ident("soa_derive") {
Expand All @@ -173,6 +187,9 @@ impl Input {
panic!("can not derive Copy for SoA vectors");
}
extra_attrs.add_derive(path);
if path.is_ident("Clone") {
derive_clone = true;
}
}
NestedMeta::Lit(_) => {
panic!(
Expand Down Expand Up @@ -200,6 +217,8 @@ impl Input {
fields: fields,
visibility: input.vis,
attrs: extra_attrs,
derive_clone,
field_attrs,
}
}

Expand Down
8 changes: 8 additions & 0 deletions soa-derive-internal/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ pub fn derive(input: &Input) -> TokenStream {
.map(|field| format!("A mutable pointer to a `{0}` from a [`{1}`](struct.{1}.html)", field, vec_name))
.collect::<Vec<_>>();

let field_attrs = input.field_attrs.iter()
.map(|attr| &attr.ptr).collect::<Vec<_>>();

let mut_field_attrs = input.field_attrs.iter()
.map(|attr| &attr.ptr_mut).collect::<Vec<_>>();

quote! {
/// An analog of a pointer to
#[doc = #doc_url]
Expand All @@ -47,6 +53,7 @@ pub fn derive(input: &Input) -> TokenStream {
#visibility struct #ptr_name {
#(
#[doc = #fields_doc]
#(#[#field_attrs])*
pub #fields_names_1: *const #fields_types,
)*
}
Expand All @@ -59,6 +66,7 @@ pub fn derive(input: &Input) -> TokenStream {
#visibility struct #ptr_mut_name {
#(
#[doc = #fields_mut_doc]
#(#[#mut_field_attrs])*
pub #fields_names_1: *mut #fields_types,
)*
}
Expand Down
8 changes: 8 additions & 0 deletions soa-derive-internal/src/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ pub fn derive(input: &Input) -> TokenStream {
.map(|field| format!("A mutable reference to a `{0}` from a [`{1}`](struct.{1}.html)", field, vec_name))
.collect::<Vec<_>>();

let field_attrs = input.field_attrs.iter()
.map(|attr| &attr.ref_).collect::<Vec<_>>();

let mut_field_attrs = input.field_attrs.iter()
.map(|attr| &attr.ref_mut).collect::<Vec<_>>();

quote! {
/// A reference to a
#[doc = #doc_url]
Expand All @@ -43,6 +49,7 @@ pub fn derive(input: &Input) -> TokenStream {
#visibility struct #ref_name<'a> {
#(
#[doc = #fields_doc]
#(#[#field_attrs])*
pub #fields_names_1: &'a #fields_types,
)*
}
Expand All @@ -54,6 +61,7 @@ pub fn derive(input: &Input) -> TokenStream {
#visibility struct #ref_mut_name<'a> {
#(
#[doc = #fields_mut_doc]
#(#[#mut_field_attrs])*
pub #fields_names_1: &'a mut #fields_types,
)*
}
Expand Down
12 changes: 10 additions & 2 deletions soa-derive-internal/src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub fn derive(input: &Input) -> TokenStream {
.map(|field| format!("A slice of `{0}` from a [`{1}`](struct.{1}.html)", field, vec_name))
.collect::<Vec<_>>();

let field_attrs = input.field_attrs.iter()
.map(|attr| &attr.slice).collect::<Vec<_>>();

let mut generated = quote! {
/// A slice of
#[doc = #doc_url]
Expand All @@ -51,6 +54,7 @@ pub fn derive(input: &Input) -> TokenStream {
#visibility struct #slice_name<'a> {
#(
#[doc = #fields_doc]
#(#[#field_attrs])*
pub #fields_names: &'a [#fields_types],
)*
}
Expand Down Expand Up @@ -214,7 +218,7 @@ pub fn derive(input: &Input) -> TokenStream {
}
};

if input.attrs.derive_clone {
if input.derive_clone {
generated.append_all(quote!{
#[allow(dead_code)]
impl<'a> #slice_name<'a> {
Expand Down Expand Up @@ -272,6 +276,9 @@ pub fn derive_mut(input: &Input) -> TokenStream {
.map(|field| format!("A mutable slice of `{0}` from a [`{1}`](struct.{1}.html)", field, vec_name))
.collect::<Vec<_>>();

let field_attrs = input.field_attrs.iter()
.map(|attr| &attr.slice_mut).collect::<Vec<_>>();

let mut generated = quote! {
/// A mutable slice of
#[doc = #doc_url]
Expand All @@ -283,6 +290,7 @@ pub fn derive_mut(input: &Input) -> TokenStream {
#visibility struct #slice_mut_name<'a> {
#(
#[doc = #fields_doc]
#(#[#field_attrs])*
pub #fields_names_1: &'a mut [#fields_types],
)*
}
Expand Down Expand Up @@ -524,7 +532,7 @@ pub fn derive_mut(input: &Input) -> TokenStream {
}
};

if input.attrs.derive_clone {
if input.derive_clone {
generated.append_all(quote!{
#[allow(dead_code)]
impl<'a> #slice_mut_name<'a> {
Expand Down
6 changes: 5 additions & 1 deletion soa-derive-internal/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub fn derive(input: &Input) -> TokenStream {
let fields_types = &input.fields.iter()
.map(|field| &field.ty)
.collect::<Vec<_>>();

let field_attrs = input.field_attrs.iter()
.map(|attr| &attr.vec).collect::<Vec<_>>();

let mut generated = quote! {
/// An analog to `
Expand All @@ -45,6 +48,7 @@ pub fn derive(input: &Input) -> TokenStream {
#visibility struct #vec_name {
#(
#[doc = #fields_doc]
#(#[#field_attrs])*
pub #fields_names: Vec<#fields_types>,
)*
}
Expand Down Expand Up @@ -350,7 +354,7 @@ pub fn derive(input: &Input) -> TokenStream {
}
};

if input.attrs.derive_clone {
if input.derive_clone {
generated.append_all(quote!{
#[allow(dead_code)]
impl #vec_name {
Expand Down
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,17 @@
//! attribute `#[soa_attr(Vec, cfg_attr(test, derive(PartialEq)))]` to the
//! struct declaration.
//!
//! `soa_attr` could also be added to specific generated struct's field.
//!
//! For example:
//!
//! ```
//! # #[macro_use] extern crate soa_derive;
//! # fn main() {
//! #[derive(Debug, PartialEq, StructOfArray)]
//! #[soa_attr(Vec, cfg_attr(test, derive(PartialEq)))]
//! pub struct Cheese {
//! #[soa_attr(Vec, deprecated)]
//! pub smell: f64,
//! pub color: (f64, f64, f64),
//! pub with_mushrooms: bool,
Expand All @@ -71,6 +76,19 @@
//! # }
//! ```
//!
//! will generate the struct like:
//!
//! ```rust
//! #[cfg_attr(test, derive(PartialEq))]
//! pub struct CheeseVec {
//! #[deprecated]
//! pub smell: Vec<f64>,
//! pub color: Vec<(f64, f64, f64)>,
//! pub with_mushrooms: Vec<bool>,
//! pub name: Vec<String>,
//! }
//! ```
//!
//! Mappings for first argument of ``soa_attr`` to the generated struct for ``Cheese``:
//! * `Vec` => `CheeseVec`
//! * `Slice` => `CheeseSlice`
Expand All @@ -80,6 +98,9 @@
//! * `Ptr` => `CheesePtr`
//! * `PtrMut` => `CheesePtrMut`
//!
//!
//! To be mentioned, there is an [issue](https://github.com/lumol-org/soa-derive/issues/44) related to `#[soa_attr(Vec, serde(skip))]` on field.
//!
//! # Usage and API
//!
//! All the generated code have some generated documentation with it, so you
Expand Down
Loading