Skip to content

Commit b12add9

Browse files
authored
Add some forwarding impls Hash, Ord, PartialOrd (#37)
* Clean up and simplify macros
1 parent e363d04 commit b12add9

File tree

6 files changed

+757
-220
lines changed

6 files changed

+757
-220
lines changed

fixed-map-derive/src/any_variants.rs

Lines changed: 92 additions & 71 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),
@@ -32,6 +32,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
3232
let mut field_inits = Vec::new();
3333
let mut field_clones = Vec::new();
3434
let mut field_partial_eqs = Vec::new();
35+
let mut field_partial_not_eqs = Vec::new();
3536
let mut contains_key = Vec::new();
3637
let mut get = Vec::new();
3738
let mut get_mut = Vec::new();
@@ -46,14 +47,21 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
4647
let var = &variant.ident;
4748
let name = Ident::new(&format!("f{}", index), Span::call_site());
4849

49-
field_inits.push(quote!(#name: #default::default()));
50-
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)));
52+
5153
field_partial_eqs.push(quote! {
52-
if self.#name != other.#name {
54+
if #partial_eq_t::ne(&self.#name, &other.#name) {
5355
return false;
5456
}
5557
});
5658

59+
field_partial_not_eqs.push(quote! {
60+
if #partial_eq_t::ne(&self.#name, &other.#name) {
61+
return true;
62+
}
63+
});
64+
5765
let kind = match &variant.fields {
5866
Fields::Unit => {
5967
len.push(quote!(usize::from(#option::is_some(&self.#name))));
@@ -67,7 +75,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
6775
insert.push(quote!(#mem::replace(&mut self.#name, #option::Some(value))));
6876
remove.push(quote!(#mem::replace(&mut self.#name, #option::None)));
6977
retain.push(quote! {
70-
if let Some(val) = #option::as_mut(&mut self.#name) {
78+
if let #option::Some(val) = #option::as_mut(&mut self.#name) {
7179
if !func(#ident::#var, val) {
7280
self.#name = None;
7381
}
@@ -86,8 +94,8 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
8694
}
8795

8896
let element = unnamed.unnamed.first().expect("Expected one element");
89-
let storage = quote!(<#element as #key_trait>::Storage::<V>);
90-
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>>);
9199

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

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

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

133141
for FieldSpec { name, .. } in &field_specs {
134-
iter_clone.push(quote!(#name: #clone::clone(&self.#name)));
142+
iter_clone.push(quote!(#name: #clone_t::clone(&self.#name)));
135143
}
136144

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

159167
#[automatically_derived]
160-
impl<V> #clone for Storage<V> where V: #clone {
168+
impl<V> #clone_t for Storage<V> where V: #clone_t {
161169
#[inline]
162170
fn clone(&self) -> Storage<V> {
163171
Storage {
@@ -167,22 +175,28 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
167175
}
168176

169177
#[automatically_derived]
170-
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,)* {}
171179

172180
#[automatically_derived]
173-
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 {
174182
#[inline]
175183
fn eq(&self, other: &Storage<V>) -> bool {
176184
#(#field_partial_eqs;)*
177185
true
178186
}
187+
188+
#[inline]
189+
fn ne(&self, other: &Storage<V>) -> bool {
190+
#(#field_partial_not_eqs;)*
191+
false
192+
}
179193
}
180194

181195
#[automatically_derived]
182-
impl<V> #eq for Storage<V> where V: #eq {}
196+
impl<V> #eq_t for Storage<V> where V: #eq_t {}
183197

184198
#[automatically_derived]
185-
impl<V> #default for Storage<V> {
199+
impl<V> #default_t for Storage<V> {
186200
#[inline]
187201
fn default() -> Self {
188202
Self {
@@ -192,7 +206,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
192206
}
193207

194208
#[automatically_derived]
195-
impl<V> #storage_trait<#ident, V> for Storage<V> {
209+
impl<V> #storage_t<#ident, V> for Storage<V> {
196210
type Iter<#lt> = Iter<#lt, V> where V: #lt;
197211
type Keys<#lt> = Keys<#lt, V> where V: #lt;
198212
type Values<#lt> = Values<#lt, V> where V: #lt;
@@ -314,7 +328,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
314328
}
315329

316330
#[automatically_derived]
317-
impl #key_trait for #ident {
331+
impl #key_t for #ident {
318332
type Storage<V> = Storage<V>;
319333
}
320334

@@ -339,9 +353,9 @@ fn build_iter_next(
339353
assoc_type: &Ident,
340354
lt: Option<&syn::Lifetime>,
341355
) -> Result<(), ()> {
342-
let option = &cx.toks.option;
343-
let iterator_t = &cx.toks.iterator_t;
344-
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();
345359
let ident = &cx.ast.ident;
346360

347361
for FieldSpec {
@@ -417,13 +431,14 @@ fn build_iter_impl(
417431
let type_name = syn::Ident::new(id, Span::call_site());
418432

419433
let lt = cx.lt;
420-
let option = &cx.toks.option;
421-
let iterator_t = &cx.toks.iterator_t;
422-
let double_ended_iterator_t = &cx.toks.double_ended_iterator_t;
423-
let clone = &cx.toks.clone;
424434
let ident = &cx.ast.ident;
425435
let vis = &cx.ast.vis;
426436

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+
427442
let mut step_forward = IteratorNext::default();
428443
let mut step_backward = IteratorNextBack::default();
429444

@@ -441,7 +456,7 @@ fn build_iter_impl(
441456
)?;
442457

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

446461
match kind {
447462
FieldKind::Simple => {
@@ -470,7 +485,7 @@ fn build_iter_impl(
470485
}
471486

472487
#[automatically_derived]
473-
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 {
474489
#[inline]
475490
fn clone(&self) -> Self {
476491
Self {
@@ -514,15 +529,16 @@ fn build_keys_impl(
514529
let type_name = syn::Ident::new(id, Span::call_site());
515530

516531
let lt = cx.lt;
517-
let option = &cx.toks.option;
518-
let iterator_t = &cx.toks.iterator_t;
519-
let double_ended_iterator_t = &cx.toks.double_ended_iterator_t;
520-
let clone = &cx.toks.clone;
521-
let bool_type = &cx.toks.bool_type;
522-
let mem = &cx.toks.mem;
523532
let ident = &cx.ast.ident;
524533
let vis = &cx.ast.vis;
525534

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+
526542
let mut step_forward = IteratorNext::default();
527543
let mut step_backward = IteratorNextBack::default();
528544

@@ -539,7 +555,7 @@ fn build_keys_impl(
539555
..
540556
} in field_specs
541557
{
542-
iter_clone.push(quote!(#name: #clone::clone(&self.#name)));
558+
iter_clone.push(quote!(#name: #clone_t::clone(&self.#name)));
543559

544560
match kind {
545561
FieldKind::Simple => {
@@ -608,7 +624,7 @@ fn build_keys_impl(
608624
}
609625

610626
#[automatically_derived]
611-
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 {
612628
#[inline]
613629
fn clone(&self) -> Self {
614630
Self {
@@ -652,12 +668,13 @@ fn build_values_impl(
652668
let type_name = syn::Ident::new(id, Span::call_site());
653669

654670
let lt = cx.lt;
655-
let option = &cx.toks.option;
656-
let iterator_t = &cx.toks.iterator_t;
657-
let double_ended_iterator_t = &cx.toks.double_ended_iterator_t;
658-
let clone = &cx.toks.clone;
659671
let vis = &cx.ast.vis;
660672

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+
661678
let mut step_forward = IteratorNext::default();
662679
let mut step_backward = IteratorNextBack::default();
663680

@@ -673,7 +690,7 @@ fn build_values_impl(
673690
..
674691
} in field_specs
675692
{
676-
iter_clone.push(quote!(#name: #clone::clone(&self.#name)));
693+
iter_clone.push(quote!(#name: #clone_t::clone(&self.#name)));
677694

678695
match kind {
679696
FieldKind::Simple => {
@@ -742,7 +759,7 @@ fn build_values_impl(
742759
}
743760

744761
#[automatically_derived]
745-
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 {
746763
#[inline]
747764
fn clone(&self) -> Self {
748765
Self {
@@ -785,12 +802,13 @@ fn build_iter_mut_impl(
785802
) -> Result<(TokenStream, Vec<TokenStream>), ()> {
786803
let type_name = syn::Ident::new(id, Span::call_site());
787804

788-
let lt = cx.lt;
789805
let ident = &cx.ast.ident;
806+
let lt = cx.lt;
790807
let vis = &cx.ast.vis;
791-
let iterator_t = &cx.toks.iterator_t;
792-
let double_ended_iterator_t = &cx.toks.double_ended_iterator_t;
793-
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();
794812

795813
let mut step_forward = IteratorNext::default();
796814
let mut step_backward = IteratorNextBack::default();
@@ -871,11 +889,12 @@ fn build_values_mut_impl(
871889
let type_name = syn::Ident::new(id, Span::call_site());
872890

873891
let lt = cx.lt;
874-
let option = &cx.toks.option;
875-
let iterator_t = &cx.toks.iterator_t;
876-
let clone = &cx.toks.clone;
877892
let vis = &cx.ast.vis;
878-
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();
879898

880899
let mut step_forward = IteratorNext::default();
881900
let mut step_backward = IteratorNextBack::default();
@@ -892,7 +911,7 @@ fn build_values_mut_impl(
892911
..
893912
} in field_specs
894913
{
895-
iter_clone.push(quote!(#name: #clone::clone(&self.#name)));
914+
iter_clone.push(quote!(#name: #clone_t::clone(&self.#name)));
896915

897916
match kind {
898917
FieldKind::Simple => {
@@ -994,10 +1013,11 @@ fn build_into_iter_impl(
9941013

9951014
let ident = &cx.ast.ident;
9961015
let vis = &cx.ast.vis;
997-
let option = &cx.toks.option;
998-
let clone = &cx.toks.clone;
999-
let iterator_t = &cx.toks.iterator_t;
1000-
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();
10011021

10021022
let mut step_forward = IteratorNext::default();
10031023
let mut step_backward = IteratorNextBack::default();
@@ -1017,7 +1037,7 @@ fn build_into_iter_impl(
10171037
)?;
10181038

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

10221042
match kind {
10231043
FieldKind::Simple => {
@@ -1031,7 +1051,7 @@ fn build_into_iter_impl(
10311051
} => {
10321052
field_decls.push(quote!(#name: #as_storage::IntoIter));
10331053
init.push(quote!(#name: #storage::into_iter(self.#name)));
1034-
clone_bounds.push(quote!(#as_storage::IntoIter: #clone));
1054+
clone_bounds.push(quote!(#as_storage::IntoIter: #clone_t));
10351055
}
10361056
}
10371057
}
@@ -1046,7 +1066,7 @@ fn build_into_iter_impl(
10461066
}
10471067

10481068
#[automatically_derived]
1049-
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,)* {
10501070
#[inline]
10511071
fn clone(&self) -> Self {
10521072
Self {
@@ -1143,14 +1163,15 @@ fn build_entry_impl(cx: &Ctxt<'_>, field_specs: &[FieldSpec<'_>]) -> Result<Toke
11431163
let ident = &cx.ast.ident;
11441164
let vis = &cx.ast.vis;
11451165
let lt = cx.lt;
1146-
let option = &cx.toks.option;
1147-
let storage_entry_trait = &cx.toks.storage_entry_trait;
1148-
let occupied_entry_trait = &cx.toks.occupied_entry_trait;
1149-
let vacant_entry_trait = &cx.toks.vacant_entry_trait;
1150-
let entry_enum = &cx.toks.entry_enum;
1151-
let option_bucket_option = &cx.toks.option_bucket_option;
1152-
let option_bucket_some = &cx.toks.option_bucket_some;
1153-
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();
11541175

11551176
let mut init = Vec::new();
11561177
let mut occupied_variant = Vec::new();

0 commit comments

Comments
 (0)