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 7 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
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
68 changes: 68 additions & 0 deletions tests/soa_attr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use serde::{Serialize, Deserialize};
use soa_derive::StructOfArray;

#[derive(Debug, Clone, PartialEq, StructOfArray)]
Expand Down Expand Up @@ -25,3 +26,70 @@ fn eq_test() {
};
assert_eq!(particles0, particles1);
}

#[derive(StructOfArray)]
#[soa_derive(PartialEq, Debug, Serialize, Deserialize)]
pub struct Point {
#[soa_attr(Slice, deprecated)]
#[soa_attr(RefMut, deprecated)]
pub x: f32,
#[soa_attr(SliceMut, deprecated)]
#[soa_attr(Ptr, deprecated)]
pub y: f32,
#[soa_attr(Vec, serde(skip))]
#[soa_attr(Ref, deprecated)]
#[soa_attr(PtrMut, deprecated)]
pub meta: bool
}

#[test]
fn serde_skip_test() -> Result<(), serde_json::Error> {
let mut soa = PointVec::new();
soa.push(Point { x: 1.0, y: 2.0, meta: true });
soa.push(Point { x: 3.0, y: 4.0, meta: false });


let json = serde_json::to_string(&soa)?;
assert_eq!(json, r#"{"x":[1.0,3.0],"y":[2.0,4.0]}"#);
let soa2: PointVec = serde_json::from_str(&json)?;
assert_eq!(&soa2, &PointVec {
x: vec![1.0, 3.0],
y: vec![2.0, 4.0],
meta: vec![]
});

{
let slice = soa.as_slice();
let _ = slice.x[0]; // Should have a deprecate warning
let _ = slice.y[0];
let _ = slice.meta[0];

let ref_ = slice.get(1).unwrap();
let _ = ref_.x;
let _ = ref_.y;
let _ = ref_.meta; // Should have a deprecate warning

let ptr = ref_.as_ptr();
let _ = ptr.x;
let _ = ptr.y; // Should have a deprecate warning
let _ = ptr.meta;

}
{
let mut slice = soa.as_mut_slice();
let _ = slice.x[0];
let _ = slice.y[0]; // Should have a deprecate warning
let _ = slice.meta[0];

let mut ref_mut = slice.get_mut(1).unwrap();
let _ = ref_mut.x; // Should have a deprecate warning
let _ = ref_mut.y;
let _ = ref_mut.meta;

let ptr_mut = ref_mut.as_mut_ptr();
let _ = ptr_mut.x;
let _ = ptr_mut.y;
let _ = ptr_mut.meta; // Should have a deprecate warning
}
Ok(())
}