Skip to content

Commit dfe39c0

Browse files
committed
[derive] Simplify code, remove obsolete features
Clean up the implementation, especially in `fn impl_block`. Make the following notable changes: - Previously, `syn` didn't support parsing macro invocations in const generics without the `full` feature enabled. To avoid the compile-time overhead of that feature, we worked around it by constructing AST nodes manually. `syn` has since added support for this without requiring the `full` feature, so we make use of it. - We used to need to split types into those that transatively depended upon type generics (like `[T; 2]`) and those that didn't (like `[u8; 2]`). We made a change in #119 that made this distinction irrelevant, but we never removed the code to perform the split. In this commit, we remove that code. That code was the only reason we needed to enable `syn`'s `visit` feature, so we are also able to remove that feature dependency.
1 parent bfb5bf5 commit dfe39c0

File tree

3 files changed

+101
-200
lines changed

3 files changed

+101
-200
lines changed

zerocopy-derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ proc-macro = true
2020
[dependencies]
2121
proc-macro2 = "1.0.1"
2222
quote = "1.0.10"
23-
syn = { version = "2", features = ["visit"] }
23+
syn = "2.0.31"
2424

2525
[dev-dependencies]
2626
rustversion = "1.0"

zerocopy-derive/src/ext.rs

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,39 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
use syn::{Data, DataEnum, DataStruct, DataUnion, Field, Fields, Type};
5+
use syn::{Data, DataEnum, DataStruct, DataUnion, Type};
66

77
pub trait DataExt {
8-
fn nested_types(&self) -> Vec<&Type>;
8+
/// Extract the types of all fields. For enums, extract the types of fields
9+
/// from each variant.
10+
fn field_types(&self) -> Vec<&Type>;
911
}
1012

1113
impl DataExt for Data {
12-
fn nested_types(&self) -> Vec<&Type> {
14+
fn field_types(&self) -> Vec<&Type> {
1315
match self {
14-
Data::Struct(strc) => strc.nested_types(),
15-
Data::Enum(enm) => enm.nested_types(),
16-
Data::Union(un) => un.nested_types(),
16+
Data::Struct(strc) => strc.field_types(),
17+
Data::Enum(enm) => enm.field_types(),
18+
Data::Union(un) => un.field_types(),
1719
}
1820
}
1921
}
2022

2123
impl DataExt for DataStruct {
22-
fn nested_types(&self) -> Vec<&Type> {
23-
fields_to_types(&self.fields)
24+
fn field_types(&self) -> Vec<&Type> {
25+
self.fields.iter().map(|f| &f.ty).collect()
2426
}
2527
}
2628

2729
impl DataExt for DataEnum {
28-
fn nested_types(&self) -> Vec<&Type> {
29-
self.variants.iter().map(|var| fields_to_types(&var.fields)).fold(Vec::new(), |mut a, b| {
30-
a.extend(b);
31-
a
32-
})
30+
fn field_types(&self) -> Vec<&Type> {
31+
self.variants.iter().flat_map(|var| &var.fields).map(|f| &f.ty).collect()
32+
}
33+
}
34+
35+
impl DataExt for DataUnion {
36+
fn field_types(&self) -> Vec<&Type> {
37+
self.fields.named.iter().map(|f| &f.ty).collect()
3338
}
3439
}
3540

@@ -39,24 +44,6 @@ pub trait EnumExt {
3944

4045
impl EnumExt for DataEnum {
4146
fn is_c_like(&self) -> bool {
42-
self.nested_types().is_empty()
47+
self.field_types().is_empty()
4348
}
4449
}
45-
46-
impl DataExt for DataUnion {
47-
fn nested_types(&self) -> Vec<&Type> {
48-
field_iter_to_types(&self.fields.named)
49-
}
50-
}
51-
52-
fn fields_to_types(fields: &Fields) -> Vec<&Type> {
53-
match fields {
54-
Fields::Named(named) => field_iter_to_types(&named.named),
55-
Fields::Unnamed(unnamed) => field_iter_to_types(&unnamed.unnamed),
56-
Fields::Unit => Vec::new(),
57-
}
58-
}
59-
60-
fn field_iter_to_types<'a, I: IntoIterator<Item = &'a Field>>(fields: I) -> Vec<&'a Type> {
61-
fields.into_iter().map(|f| &f.ty).collect()
62-
}

0 commit comments

Comments
 (0)