1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use syn::{
token::{Brace, Paren},
FieldPat, FieldsNamed, FieldsUnnamed, Ident, Pat, PatIdent, PatStruct, PatTuple,
PatTupleStruct, Path, Result, Token,
};
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
use {std::iter, syn::punctuated::Punctuated, syn::PatRest};
use crate::{DeriveWhere, Field, Skip, Trait};
#[cfg_attr(test, derive(Debug))]
pub struct Fields<'a> {
pub self_pattern: Pat,
pub other_pattern: Pat,
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
pub other_pattern_skip: Pat,
pub fields: Vec<Field<'a>>,
}
impl<'a> Fields<'a> {
pub fn from_named(
derive_wheres: &[DeriveWhere],
skip_inner: &Skip,
path: Path,
fields: &'a FieldsNamed,
) -> Result<Self> {
let fields = Field::from_named(derive_wheres, skip_inner, fields)?;
let self_pattern = Self::struct_pattern(path.clone(), &fields, |field| &field.self_ident);
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
let other_pattern_skip = Pat::Struct(PatStruct {
attrs: Vec::new(),
path: path.clone(),
brace_token: Brace::default(),
fields: Punctuated::new(),
dot2_token: Some(<Token![..]>::default()),
});
let other_pattern = Self::struct_pattern(path, &fields, |field| &field.other_ident);
Ok(Self {
self_pattern,
other_pattern,
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
other_pattern_skip,
fields,
})
}
pub fn from_unnamed(
derive_wheres: &[DeriveWhere],
skip_inner: &Skip,
path: Path,
fields: &'a FieldsUnnamed,
) -> Result<Self> {
let fields = Field::from_unnamed(derive_wheres, skip_inner, fields)?;
let self_pattern = Self::tuple_pattern(path.clone(), &fields, |field| &field.self_ident);
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
let other_pattern_skip = Pat::TupleStruct(PatTupleStruct {
attrs: Vec::new(),
path: path.clone(),
pat: PatTuple {
attrs: Vec::new(),
paren_token: Paren::default(),
elems: iter::once(Pat::Rest(PatRest {
attrs: Vec::new(),
dot2_token: <Token![..]>::default(),
}))
.collect(),
},
});
let other_pattern = Self::tuple_pattern(path, &fields, |field| &field.other_ident);
Ok(Self {
self_pattern,
other_pattern,
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
other_pattern_skip,
fields,
})
}
fn struct_pattern(
path: Path,
fields: &[Field],
field_ident: impl for<'b> Fn(&'b Field) -> &'b Ident,
) -> Pat {
Pat::Struct(PatStruct {
attrs: Vec::new(),
path,
brace_token: Brace::default(),
fields: fields
.iter()
.map(|field| FieldPat {
attrs: Vec::new(),
member: field.to_member(),
colon_token: Some(<Token![:]>::default()),
pat: Box::new(Pat::Ident(PatIdent {
attrs: Vec::new(),
by_ref: Some(<Token![ref]>::default()),
mutability: None,
ident: field_ident(field).clone(),
subpat: None,
})),
})
.collect(),
dot2_token: None,
})
}
fn tuple_pattern(
path: Path,
fields: &[Field],
field_ident: impl for<'b> Fn(&'b Field) -> &'b Ident,
) -> Pat {
Pat::TupleStruct(PatTupleStruct {
attrs: Vec::new(),
path,
pat: PatTuple {
attrs: Vec::new(),
paren_token: Paren::default(),
elems: fields
.iter()
.map(|field| {
Pat::Ident(PatIdent {
attrs: Vec::new(),
by_ref: Some(<Token![ref]>::default()),
mutability: None,
ident: field_ident(field).clone(),
subpat: None,
})
})
.collect(),
},
})
}
#[cfg(feature = "zeroize")]
pub fn self_pattern_mut(&self) -> Pat {
let mut pattern = self.self_pattern.clone();
match &mut pattern {
Pat::Struct(pattern) => {
for field in &mut pattern.fields {
if let Pat::Ident(pattern) = &mut *field.pat {
pattern.mutability = Some(<Token![mut]>::default());
} else {
unreachable!("unexpected pattern")
}
}
}
Pat::TupleStruct(pattern) => {
for field in &mut pattern.pat.elems {
if let Pat::Ident(pattern) = &mut *field {
pattern.mutability = Some(<Token![mut]>::default());
} else {
unreachable!("unexpected pattern")
}
}
}
_ => unreachable!("unexpected pattern"),
}
pattern
}
pub fn any_skip_trait(&self, trait_: &Trait) -> bool {
self.fields.iter().any(|field| field.skip(trait_))
}
pub fn skip(&self, trait_: &Trait) -> bool {
self.fields.iter().all(|field| field.skip(trait_))
}
}