Skip to content

Commit 404a443

Browse files
committed
Change the syntax for soa_derive to be close to derive
1 parent dc81701 commit 404a443

File tree

9 files changed

+50
-34
lines changed

9 files changed

+50
-34
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "soa_derive"
3-
version = "0.10.0"
3+
version = "0.11.0"
44
edition = "2018"
55
authors = ["Guillaume Fraux <[email protected]>"]
66
license = "MIT/Apache-2.0"
@@ -19,7 +19,7 @@ members = [
1919
]
2020

2121
[dependencies]
22-
soa_derive_internal = {path = "soa-derive-internal", version = "0.10"}
22+
soa_derive_internal = {path = "soa-derive-internal", version = "0.11"}
2323

2424
[dev-dependencies]
2525
bencher = "0.1"

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ You can use `<Cheese as StructOfArray>::Type` instead of the explicitly named ty
4040

4141
Add `#[derive(StructOfArray)]` to each struct you want to derive a struct of
4242
array version. If you need the helper structs to derive additional traits (such
43-
as `Debug` or `PartialEq`), you can add an attribute `#[soa_derive = "Debug,
44-
PartialEq"]` to the struct declaration.
43+
as `Debug` or `PartialEq`), you can add an attribute `#[soa_derive(Debug,
44+
PartialEq)]` to the struct declaration.
4545

4646
```rust
4747
#[derive(Debug, PartialEq, StructOfArray)]
48-
#[soa_derive = "Debug, PartialEq"]
48+
#[soa_derive(Debug, PartialEq)]
4949
pub struct Cheese {
5050
pub smell: f64,
5151
pub color: (f64, f64, f64),
@@ -54,9 +54,9 @@ pub struct Cheese {
5454
}
5555
```
5656

57-
If you want to add attribute to a specific generated struct(such as
57+
If you want to add attribute to a specific generated struct(such as
5858
`#[cfg_attr(test, derive(PartialEq))]` on `CheeseVec`), you can add an
59-
attribute `#[soa_attr(Vec, cfg_attr(test, derive(PartialEq)))]` to the
59+
attribute `#[soa_attr(Vec, cfg_attr(test, derive(PartialEq)))]` to the
6060
struct declaration.
6161

6262
```rust
@@ -70,7 +70,7 @@ pub struct Cheese {
7070
}
7171
```
7272

73-
Mappings for first argument of ``soa_attr`` to the generated struct for ``Cheese``:
73+
Mappings for first argument of ``soa_attr`` to the generated struct for ``Cheese``:
7474
* `Vec` => `CheeseVec`
7575
* `Slice` => `CheeseSlice`
7676
* `SliceMut` => `CheeseSliceMut`

benches/soa.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::needless_return)]
2+
13
use soa_derive::StructOfArray;
24

35
use bencher::{Bencher, benchmark_group, benchmark_main};

example/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extern crate soa_derive;
4242

4343
/// A basic Particle type
4444
#[derive(Debug, PartialEq, StructOfArray)]
45-
#[soa_derive = "Debug, PartialEq"]
45+
#[soa_derive(Debug, PartialEq)]
4646
pub struct Particle {
4747
/// Mass of the particle
4848
pub mass: f64,

soa-derive-internal/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "soa_derive_internal"
3-
version = "0.10.1"
3+
version = "0.11.0"
44
edition = "2018"
55
authors = ["Guillaume Fraux <[email protected]>"]
66
license = "MIT/Apache-2.0"

soa-derive-internal/src/input.rs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::convert::TryInto;
33
use proc_macro2::Span;
44
use quote::quote;
55

6-
use syn::{Data, DeriveInput, Field, Ident, Lit, Path, Visibility};
7-
use syn::{Meta, MetaList, MetaNameValue, NestedMeta};
6+
use syn::{Data, DeriveInput, Field, Ident, Path, Visibility};
7+
use syn::{Meta, MetaList, NestedMeta};
88

99
/// Representing the struct we are deriving
1010
pub struct Input {
@@ -106,11 +106,19 @@ impl ExtraAttributes {
106106
}
107107

108108
/// Add a single trait from `#[soa_derive]`
109-
fn add_derive(&mut self, trait_: &str) {
110-
static EXCEPTIONS: &[&str] = &["Clone", "Deserialize", "Serialize"];
109+
fn add_derive(&mut self, path: &Path) {
110+
let derive_only_vec = |path: &Path| {
111+
static EXCEPTIONS: &[&str] = &["Clone", "Deserialize", "Serialize"];
112+
for exception in EXCEPTIONS {
113+
if path.is_ident(exception) {
114+
return true;
115+
}
116+
}
117+
return false;
118+
};
111119

112-
let derive = create_derive_meta(trait_);
113-
if !EXCEPTIONS.contains(&trait_) {
120+
let derive = create_derive_meta(path.clone());
121+
if !derive_only_vec(path) {
114122
self.slice.push(derive.clone());
115123
self.slice_mut.push(derive.clone());
116124
self.ref_.push(derive.clone());
@@ -122,17 +130,15 @@ impl ExtraAttributes {
122130
// always add this derive to the Vec struct
123131
self.vec.push(derive);
124132

125-
if trait_ == "Clone" {
133+
if path.is_ident("Clone") {
126134
self.derive_clone = true;
127135
}
128136
}
129137
}
130138

131-
fn create_derive_meta(name: &str) -> Meta {
139+
fn create_derive_meta(path: Path) -> Meta {
132140
let mut nested = syn::punctuated::Punctuated::new();
133-
nested.push(NestedMeta::Meta(Meta::Path(Path::from(
134-
Ident::new(name, Span::call_site())
135-
))));
141+
nested.push(NestedMeta::Meta(Meta::Path(path)));
136142

137143
Meta::List(MetaList {
138144
path: Path::from(Ident::new("derive", Span::call_site())),
@@ -158,20 +164,28 @@ impl Input {
158164
if let Ok(meta) = attr.parse_meta() {
159165
if meta.path().is_ident("soa_derive") {
160166
match meta {
161-
Meta::NameValue(MetaNameValue {
162-
lit: Lit::Str(string),
163-
..
164-
}) => {
165-
for value in string.value().split(',') {
166-
let value = value.trim();
167-
if value == "Copy" {
168-
panic!("can not derive Copy for SoA vectors");
167+
Meta::List(ref list) => {
168+
for element in &list.nested {
169+
match element {
170+
NestedMeta::Meta(meta) => {
171+
let path = meta.path();
172+
if path.is_ident("Copy") {
173+
panic!("can not derive Copy for SoA vectors");
174+
}
175+
extra_attrs.add_derive(path);
176+
}
177+
NestedMeta::Lit(_) => {
178+
panic!(
179+
"expected #[soa_derive(Traits, To, Derive)], got #[{}]",
180+
quote!(#meta)
181+
);
182+
}
169183
}
170-
extra_attrs.add_derive(value);
171184
}
185+
172186
}
173187
_ => panic!(
174-
"expected #[soa_derive = \"Traits, To, Derive\"], got #[{}]",
188+
"expected #[soa_derive(Traits, To, Derive)], got #[{}]",
175189
quote!(#meta)
176190
),
177191
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
//! # #[macro_use] extern crate soa_derive;
4343
//! # fn main() {
4444
//! #[derive(Debug, PartialEq, StructOfArray)]
45-
//! #[soa_derive = "Debug, PartialEq"]
45+
//! #[soa_derive(Debug, PartialEq)]
4646
//! pub struct Cheese {
4747
//! pub smell: f64,
4848
//! pub color: (f64, f64, f64),

tests/particles/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use soa_derive::StructOfArray;
22

33
#[derive(Debug, Clone, PartialEq, StructOfArray)]
4-
#[soa_derive = "Debug, Clone, PartialEq"]
4+
#[soa_derive(Debug, Clone, PartialEq)]
55
pub struct Particle {
66
pub name: String,
77
pub mass: f64,

tests/serde.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
22
use soa_derive::StructOfArray;
33

44
#[derive(Debug, Clone, PartialEq, StructOfArray)]
5-
#[soa_derive = "Debug, Clone, PartialEq, Serialize, Deserialize"]
5+
#[soa_derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
66
pub struct Particle {
77
pub name: String,
88
pub mass: f64,

0 commit comments

Comments
 (0)