Skip to content

Commit 80fbf60

Browse files
committed
Simplify token declarations and lazily build paths
1 parent bc8af85 commit 80fbf60

File tree

4 files changed

+242
-200
lines changed

4 files changed

+242
-200
lines changed

fixed-map-derive/src/any_variants.rs

Lines changed: 79 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
1010
let ident = &cx.ast.ident;
1111
let lt = cx.lt;
1212

13-
let clone = &cx.toks.clone;
14-
let copy = &cx.toks.copy;
15-
let default = &cx.toks.default;
16-
let eq = &cx.toks.eq;
17-
let key_trait = &cx.toks.key_trait;
18-
let mem = &cx.toks.mem;
19-
let option = &cx.toks.option;
20-
let partial_eq = &cx.toks.partial_eq;
21-
let storage_trait = &cx.toks.storage_trait;
13+
let clone_t = cx.toks.clone_t();
14+
let copy_t = cx.toks.copy_t();
15+
let default_t = cx.toks.default_t();
16+
let eq_t = cx.toks.eq_t();
17+
let key_t = cx.toks.key_t();
18+
let mem = cx.toks.mem();
19+
let option = cx.toks.option();
20+
let partial_eq_t = cx.toks.partial_eq_t();
21+
let storage_t = cx.toks.storage_t();
2222

2323
let const_wrapper = Ident::new(
2424
&format!("__IMPL_KEY_FOR_{}", cx.ast.ident),
@@ -47,17 +47,17 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
4747
let var = &variant.ident;
4848
let name = Ident::new(&format!("f{}", index), Span::call_site());
4949

50-
field_inits.push(quote!(#name: #default::default()));
51-
field_clones.push(quote!(#name: #clone::clone(&self.#name)));
50+
field_inits.push(quote!(#name: #default_t::default()));
51+
field_clones.push(quote!(#name: #clone_t::clone(&self.#name)));
5252

5353
field_partial_eqs.push(quote! {
54-
if #partial_eq::ne(&self.#name, &other.#name) {
54+
if #partial_eq_t::ne(&self.#name, &other.#name) {
5555
return false;
5656
}
5757
});
5858

5959
field_partial_not_eqs.push(quote! {
60-
if #partial_eq::ne(&self.#name, &other.#name) {
60+
if #partial_eq_t::ne(&self.#name, &other.#name) {
6161
return true;
6262
}
6363
});
@@ -75,7 +75,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
7575
insert.push(quote!(#mem::replace(&mut self.#name, #option::Some(value))));
7676
remove.push(quote!(#mem::replace(&mut self.#name, #option::None)));
7777
retain.push(quote! {
78-
if let Some(val) = #option::as_mut(&mut self.#name) {
78+
if let #option::Some(val) = #option::as_mut(&mut self.#name) {
7979
if !func(#ident::#var, val) {
8080
self.#name = None;
8181
}
@@ -94,8 +94,8 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
9494
}
9595

9696
let element = unnamed.unnamed.first().expect("Expected one element");
97-
let storage = quote!(<#element as #key_trait>::Storage::<V>);
98-
let as_storage = quote!(<#storage as #storage_trait<#element, V>>);
97+
let storage = quote!(<#element as #key_t>::Storage::<V>);
98+
let as_storage = quote!(<#storage as #storage_t<#element, V>>);
9999

100100
len.push(quote!(#as_storage::len(&self.#name)));
101101
is_empty.push(quote!(#as_storage::is_empty(&self.#name)));
@@ -113,7 +113,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
113113
#as_storage::retain(&mut self.#name, |k, v| func(#ident::#var(k), v));
114114
});
115115

116-
copy_bounds.push(quote!(#storage: #copy));
116+
copy_bounds.push(quote!(#storage: #copy_t));
117117

118118
FieldKind::Complex {
119119
element: quote!(#element),
@@ -139,7 +139,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
139139
let mut iter_clone = Vec::new();
140140

141141
for FieldSpec { name, .. } in &field_specs {
142-
iter_clone.push(quote!(#name: #clone::clone(&self.#name)));
142+
iter_clone.push(quote!(#name: #clone_t::clone(&self.#name)));
143143
}
144144

145145
let pattern = &pattern;
@@ -165,7 +165,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
165165
}
166166

167167
#[automatically_derived]
168-
impl<V> #clone for Storage<V> where V: #clone {
168+
impl<V> #clone_t for Storage<V> where V: #clone_t {
169169
#[inline]
170170
fn clone(&self) -> Storage<V> {
171171
Storage {
@@ -175,10 +175,10 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
175175
}
176176

177177
#[automatically_derived]
178-
impl<V> #copy for Storage<V> where V: #copy, #(#copy_bounds,)* {}
178+
impl<V> #copy_t for Storage<V> where V: #copy_t, #(#copy_bounds,)* {}
179179

180180
#[automatically_derived]
181-
impl<V> #partial_eq for Storage<V> where V: #partial_eq {
181+
impl<V> #partial_eq_t for Storage<V> where V: #partial_eq_t {
182182
#[inline]
183183
fn eq(&self, other: &Storage<V>) -> bool {
184184
#(#field_partial_eqs;)*
@@ -193,10 +193,10 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
193193
}
194194

195195
#[automatically_derived]
196-
impl<V> #eq for Storage<V> where V: #eq {}
196+
impl<V> #eq_t for Storage<V> where V: #eq_t {}
197197

198198
#[automatically_derived]
199-
impl<V> #default for Storage<V> {
199+
impl<V> #default_t for Storage<V> {
200200
#[inline]
201201
fn default() -> Self {
202202
Self {
@@ -206,7 +206,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
206206
}
207207

208208
#[automatically_derived]
209-
impl<V> #storage_trait<#ident, V> for Storage<V> {
209+
impl<V> #storage_t<#ident, V> for Storage<V> {
210210
type Iter<#lt> = Iter<#lt, V> where V: #lt;
211211
type Keys<#lt> = Keys<#lt, V> where V: #lt;
212212
type Values<#lt> = Values<#lt, V> where V: #lt;
@@ -328,7 +328,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
328328
}
329329

330330
#[automatically_derived]
331-
impl #key_trait for #ident {
331+
impl #key_t for #ident {
332332
type Storage<V> = Storage<V>;
333333
}
334334

@@ -353,9 +353,9 @@ fn build_iter_next(
353353
assoc_type: &Ident,
354354
lt: Option<&syn::Lifetime>,
355355
) -> Result<(), ()> {
356-
let option = &cx.toks.option;
357-
let iterator_t = &cx.toks.iterator_t;
358-
let double_ended_iterator_t = &cx.toks.double_ended_iterator_t;
356+
let option = cx.toks.option();
357+
let iterator_t = cx.toks.iterator_t();
358+
let double_ended_iterator_t = cx.toks.double_ended_iterator_t();
359359
let ident = &cx.ast.ident;
360360

361361
for FieldSpec {
@@ -431,13 +431,14 @@ fn build_iter_impl(
431431
let type_name = syn::Ident::new(id, Span::call_site());
432432

433433
let lt = cx.lt;
434-
let option = &cx.toks.option;
435-
let iterator_t = &cx.toks.iterator_t;
436-
let double_ended_iterator_t = &cx.toks.double_ended_iterator_t;
437-
let clone = &cx.toks.clone;
438434
let ident = &cx.ast.ident;
439435
let vis = &cx.ast.vis;
440436

437+
let option = cx.toks.option();
438+
let iterator_t = cx.toks.iterator_t();
439+
let double_ended_iterator_t = cx.toks.double_ended_iterator_t();
440+
let clone_t = cx.toks.clone_t();
441+
441442
let mut step_forward = IteratorNext::default();
442443
let mut step_backward = IteratorNextBack::default();
443444

@@ -455,7 +456,7 @@ fn build_iter_impl(
455456
)?;
456457

457458
for FieldSpec { name, kind, .. } in field_specs {
458-
iter_clone.push(quote!(#name: #clone::clone(&self.#name)));
459+
iter_clone.push(quote!(#name: #clone_t::clone(&self.#name)));
459460

460461
match kind {
461462
FieldKind::Simple => {
@@ -484,7 +485,7 @@ fn build_iter_impl(
484485
}
485486

486487
#[automatically_derived]
487-
impl<#lt, V> #clone for #type_name<#lt, V> where V: #lt {
488+
impl<#lt, V> #clone_t for #type_name<#lt, V> where V: #lt {
488489
#[inline]
489490
fn clone(&self) -> Self {
490491
Self {
@@ -528,15 +529,16 @@ fn build_keys_impl(
528529
let type_name = syn::Ident::new(id, Span::call_site());
529530

530531
let lt = cx.lt;
531-
let option = &cx.toks.option;
532-
let iterator_t = &cx.toks.iterator_t;
533-
let double_ended_iterator_t = &cx.toks.double_ended_iterator_t;
534-
let clone = &cx.toks.clone;
535-
let bool_type = &cx.toks.bool_type;
536-
let mem = &cx.toks.mem;
537532
let ident = &cx.ast.ident;
538533
let vis = &cx.ast.vis;
539534

535+
let bool_type = cx.toks.bool_type();
536+
let clone_t = cx.toks.clone_t();
537+
let double_ended_iterator_t = cx.toks.double_ended_iterator_t();
538+
let iterator_t = cx.toks.iterator_t();
539+
let mem = cx.toks.mem();
540+
let option = cx.toks.option();
541+
540542
let mut step_forward = IteratorNext::default();
541543
let mut step_backward = IteratorNextBack::default();
542544

@@ -553,7 +555,7 @@ fn build_keys_impl(
553555
..
554556
} in field_specs
555557
{
556-
iter_clone.push(quote!(#name: #clone::clone(&self.#name)));
558+
iter_clone.push(quote!(#name: #clone_t::clone(&self.#name)));
557559

558560
match kind {
559561
FieldKind::Simple => {
@@ -622,7 +624,7 @@ fn build_keys_impl(
622624
}
623625

624626
#[automatically_derived]
625-
impl<#lt, V> #clone for #type_name<#lt, V> where V: #lt {
627+
impl<#lt, V> #clone_t for #type_name<#lt, V> where V: #lt {
626628
#[inline]
627629
fn clone(&self) -> Self {
628630
Self {
@@ -666,12 +668,13 @@ fn build_values_impl(
666668
let type_name = syn::Ident::new(id, Span::call_site());
667669

668670
let lt = cx.lt;
669-
let option = &cx.toks.option;
670-
let iterator_t = &cx.toks.iterator_t;
671-
let double_ended_iterator_t = &cx.toks.double_ended_iterator_t;
672-
let clone = &cx.toks.clone;
673671
let vis = &cx.ast.vis;
674672

673+
let clone_t = cx.toks.clone_t();
674+
let double_ended_iterator_t = cx.toks.double_ended_iterator_t();
675+
let iterator_t = cx.toks.iterator_t();
676+
let option = cx.toks.option();
677+
675678
let mut step_forward = IteratorNext::default();
676679
let mut step_backward = IteratorNextBack::default();
677680

@@ -687,7 +690,7 @@ fn build_values_impl(
687690
..
688691
} in field_specs
689692
{
690-
iter_clone.push(quote!(#name: #clone::clone(&self.#name)));
693+
iter_clone.push(quote!(#name: #clone_t::clone(&self.#name)));
691694

692695
match kind {
693696
FieldKind::Simple => {
@@ -756,7 +759,7 @@ fn build_values_impl(
756759
}
757760

758761
#[automatically_derived]
759-
impl<#lt, V> #clone for #type_name<#lt, V> where V: #lt {
762+
impl<#lt, V> #clone_t for #type_name<#lt, V> where V: #lt {
760763
#[inline]
761764
fn clone(&self) -> Self {
762765
Self {
@@ -799,12 +802,13 @@ fn build_iter_mut_impl(
799802
) -> Result<(TokenStream, Vec<TokenStream>), ()> {
800803
let type_name = syn::Ident::new(id, Span::call_site());
801804

802-
let lt = cx.lt;
803805
let ident = &cx.ast.ident;
806+
let lt = cx.lt;
804807
let vis = &cx.ast.vis;
805-
let iterator_t = &cx.toks.iterator_t;
806-
let double_ended_iterator_t = &cx.toks.double_ended_iterator_t;
807-
let option = &cx.toks.option;
808+
809+
let double_ended_iterator_t = cx.toks.double_ended_iterator_t();
810+
let iterator_t = cx.toks.iterator_t();
811+
let option = cx.toks.option();
808812

809813
let mut step_forward = IteratorNext::default();
810814
let mut step_backward = IteratorNextBack::default();
@@ -885,11 +889,12 @@ fn build_values_mut_impl(
885889
let type_name = syn::Ident::new(id, Span::call_site());
886890

887891
let lt = cx.lt;
888-
let option = &cx.toks.option;
889-
let iterator_t = &cx.toks.iterator_t;
890-
let clone = &cx.toks.clone;
891892
let vis = &cx.ast.vis;
892-
let double_ended_iterator_t = &cx.toks.double_ended_iterator_t;
893+
894+
let option = cx.toks.option();
895+
let iterator_t = cx.toks.iterator_t();
896+
let clone_t = cx.toks.clone_t();
897+
let double_ended_iterator_t = cx.toks.double_ended_iterator_t();
893898

894899
let mut step_forward = IteratorNext::default();
895900
let mut step_backward = IteratorNextBack::default();
@@ -906,7 +911,7 @@ fn build_values_mut_impl(
906911
..
907912
} in field_specs
908913
{
909-
iter_clone.push(quote!(#name: #clone::clone(&self.#name)));
914+
iter_clone.push(quote!(#name: #clone_t::clone(&self.#name)));
910915

911916
match kind {
912917
FieldKind::Simple => {
@@ -1008,10 +1013,11 @@ fn build_into_iter_impl(
10081013

10091014
let ident = &cx.ast.ident;
10101015
let vis = &cx.ast.vis;
1011-
let option = &cx.toks.option;
1012-
let clone = &cx.toks.clone;
1013-
let iterator_t = &cx.toks.iterator_t;
1014-
let double_ended_iterator_t = &cx.toks.double_ended_iterator_t;
1016+
1017+
let option = cx.toks.option();
1018+
let clone_t = cx.toks.clone_t();
1019+
let iterator_t = cx.toks.iterator_t();
1020+
let double_ended_iterator_t = cx.toks.double_ended_iterator_t();
10151021

10161022
let mut step_forward = IteratorNext::default();
10171023
let mut step_backward = IteratorNextBack::default();
@@ -1031,7 +1037,7 @@ fn build_into_iter_impl(
10311037
)?;
10321038

10331039
for FieldSpec { name, kind, .. } in field_specs {
1034-
field_clone.push(quote!(#name: #clone::clone(&self.#name)));
1040+
field_clone.push(quote!(#name: #clone_t::clone(&self.#name)));
10351041

10361042
match kind {
10371043
FieldKind::Simple => {
@@ -1045,7 +1051,7 @@ fn build_into_iter_impl(
10451051
} => {
10461052
field_decls.push(quote!(#name: #as_storage::IntoIter));
10471053
init.push(quote!(#name: #storage::into_iter(self.#name)));
1048-
clone_bounds.push(quote!(#as_storage::IntoIter: #clone));
1054+
clone_bounds.push(quote!(#as_storage::IntoIter: #clone_t));
10491055
}
10501056
}
10511057
}
@@ -1060,7 +1066,7 @@ fn build_into_iter_impl(
10601066
}
10611067

10621068
#[automatically_derived]
1063-
impl<V> #clone for #type_name<V> where V: Clone, #(#clone_bounds,)* {
1069+
impl<V> #clone_t for #type_name<V> where V: Clone, #(#clone_bounds,)* {
10641070
#[inline]
10651071
fn clone(&self) -> Self {
10661072
Self {
@@ -1157,14 +1163,15 @@ fn build_entry_impl(cx: &Ctxt<'_>, field_specs: &[FieldSpec<'_>]) -> Result<Toke
11571163
let ident = &cx.ast.ident;
11581164
let vis = &cx.ast.vis;
11591165
let lt = cx.lt;
1160-
let option = &cx.toks.option;
1161-
let storage_entry_trait = &cx.toks.storage_entry_trait;
1162-
let occupied_entry_trait = &cx.toks.occupied_entry_trait;
1163-
let vacant_entry_trait = &cx.toks.vacant_entry_trait;
1164-
let entry_enum = &cx.toks.entry_enum;
1165-
let option_bucket_option = &cx.toks.option_bucket_option;
1166-
let option_bucket_some = &cx.toks.option_bucket_some;
1167-
let option_bucket_none = &cx.toks.option_bucket_none;
1166+
1167+
let entry_enum = cx.toks.entry_enum();
1168+
let occupied_entry_trait = cx.toks.occupied_entry_t();
1169+
let option = cx.toks.option();
1170+
let option_bucket_none = cx.toks.option_bucket_none();
1171+
let option_bucket_option = cx.toks.option_bucket_option();
1172+
let option_bucket_some = cx.toks.option_bucket_some();
1173+
let storage_entry_trait = cx.toks.storage_entry_t();
1174+
let vacant_entry_trait = cx.toks.vacant_entry_t();
11681175

11691176
let mut init = Vec::new();
11701177
let mut occupied_variant = Vec::new();

0 commit comments

Comments
 (0)