Skip to content

Commit bc8af85

Browse files
committed
Simplify unit_variants impl
1 parent d3fef54 commit bc8af85

File tree

1 file changed

+25
-54
lines changed

1 file changed

+25
-54
lines changed

fixed-map-derive/src/unit_variants.rs

Lines changed: 25 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use proc_macro2::{Span, TokenStream};
2-
use quote::quote;
2+
use quote::{format_ident, quote};
33
use syn::{DataEnum, Ident};
44

55
use crate::context::Ctxt;
@@ -44,51 +44,17 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
4444
Span::call_site(),
4545
);
4646

47-
let mut len = Vec::new();
48-
let mut is_empty = Vec::new();
49-
let mut pattern = Vec::new();
50-
let mut names = Vec::new();
51-
let mut fields = Vec::new();
52-
let mut field_inits = Vec::new();
53-
let mut contains_key = Vec::new();
54-
let mut get = Vec::new();
55-
let mut get_mut = Vec::new();
56-
let mut insert = Vec::new();
57-
let mut remove = Vec::new();
58-
let mut retain = Vec::new();
59-
let mut keys_iter_init = Vec::new();
60-
let mut iter_init = Vec::new();
61-
let mut entry = Vec::new();
47+
let count = en.variants.len();
48+
let mut variants = Vec::with_capacity(count);
49+
let mut names = Vec::with_capacity(count);
50+
let mut field_inits = Vec::with_capacity(count);
6251

6352
for (index, variant) in en.variants.iter().enumerate() {
64-
let var = &variant.ident;
65-
let name = Ident::new(&format!("f{}", index), Span::call_site());
66-
67-
len.push(quote!(usize::from(#option::is_some(#name))));
68-
is_empty.push(quote!(#option::is_none(#name)));
6953
field_inits.push(quote!(#option::None));
70-
fields.push(quote!(#option<V>));
71-
pattern.push(quote!(#ident::#var));
72-
contains_key.push(quote!(#option::is_some(#name)));
73-
get.push(quote!(#option::as_ref(#name)));
74-
get_mut.push(quote!(#option::as_mut(#name)));
75-
insert.push(quote!(#mem::replace(#name, #option::Some(value))));
76-
remove.push(quote!(#mem::take(#name)));
77-
retain.push(quote! {
78-
if let Some(val) = #option::as_mut(#name) {
79-
if !func(#ident::#var, val) {
80-
*#name = None;
81-
}
82-
}
83-
});
84-
keys_iter_init.push(quote!(if #name.is_some() { Some(#ident::#var) } else { None }));
85-
iter_init.push(quote!((#ident::#var, #name)));
86-
names.push(name.clone());
87-
entry.push(quote!(option_to_entry(#name, key)));
54+
variants.push(&variant.ident);
55+
names.push(format_ident!("_{}", index));
8856
}
8957

90-
let count = en.variants.len();
91-
9258
let entry_impl = if cfg!(feature = "entry") {
9359
quote! {
9460
#vis struct VacantEntry<#lt, V> {
@@ -165,7 +131,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
165131
let [#(#names),*] = &mut self.data;
166132

167133
match key {
168-
#(#pattern => #entry,)*
134+
#(#ident::#variants => option_to_entry(#names, key),)*
169135
}
170136
}
171137
}
@@ -310,21 +276,21 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
310276
#[inline]
311277
fn len(&self) -> usize {
312278
let [#(#names),*] = &self.data;
313-
0 #(+ #len)*
279+
0 #(+ usize::from(#option::is_some(#names)))*
314280
}
315281

316282
#[inline]
317283
fn is_empty(&self) -> bool {
318284
let [#(#names),*] = &self.data;
319-
true #(&& #is_empty)*
285+
true #(&& #option::is_none(#names))*
320286
}
321287

322288
#[inline]
323289
fn insert(&mut self, key: #ident, value: V) -> #option<V> {
324290
let [#(#names),*] = &mut self.data;
325291

326292
match key {
327-
#(#pattern => #insert,)*
293+
#(#ident::#variants => #mem::replace(#names, #option::Some(value)),)*
328294
}
329295
}
330296

@@ -333,7 +299,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
333299
let [#(#names),*] = &self.data;
334300

335301
match value {
336-
#(#pattern => #contains_key,)*
302+
#(#ident::#variants => #option::is_some(#names),)*
337303
}
338304
}
339305

@@ -342,7 +308,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
342308
let [#(#names),*] = &self.data;
343309

344310
match value {
345-
#(#pattern => #get,)*
311+
#(#ident::#variants => #option::as_ref(#names),)*
346312
}
347313
}
348314

@@ -351,7 +317,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
351317
let [#(#names),*] = &mut self.data;
352318

353319
match value {
354-
#(#pattern => #get_mut,)*
320+
#(#ident::#variants => #option::as_mut(#names),)*
355321
}
356322
}
357323

@@ -360,7 +326,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
360326
let [#(#names),*] = &mut self.data;
361327

362328
match value {
363-
#(#pattern => #remove,)*
329+
#(#ident::#variants => #mem::take(#names),)*
364330
}
365331
}
366332

@@ -370,7 +336,12 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
370336
F: FnMut(#ident, &mut V) -> bool
371337
{
372338
let [#(#names),*] = &mut self.data;
373-
#(#retain)*
339+
340+
#(if let Some(val) = #option::as_mut(#names) {
341+
if !func(#ident::#variants, val) {
342+
*#names = None;
343+
}
344+
})*
374345
}
375346

376347
#[inline]
@@ -381,13 +352,13 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
381352
#[inline]
382353
fn iter(&self) -> Self::Iter<'_> {
383354
let [#(#names),*] = &self.data;
384-
#iterator::flat_map(#into_iter([#(#iter_init),*]), |(k, v)| #option::Some((k, #option::as_ref(v)?)))
355+
#iterator::flat_map(#into_iter([#((#ident::#variants, #names)),*]), |(k, v)| #option::Some((k, #option::as_ref(v)?)))
385356
}
386357

387358
#[inline]
388359
fn keys(&self) -> Self::Keys<'_> {
389360
let [#(#names),*] = &self.data;
390-
#iterator::flatten(#into_iter([#(#keys_iter_init),*]))
361+
#iterator::flatten(#into_iter([#(if #names.is_some() { Some(#ident::#variants) } else { None }),*]))
391362
}
392363

393364
#[inline]
@@ -398,7 +369,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
398369
#[inline]
399370
fn iter_mut(&mut self) -> Self::IterMut<'_> {
400371
let [#(#names),*] = &mut self.data;
401-
#iterator::flat_map(#into_iter([#(#iter_init),*]), |(k, v)| #option::Some((k, #option::as_mut(v)?)))
372+
#iterator::flat_map(#into_iter([#((#ident::#variants, #names)),*]), |(k, v)| #option::Some((k, #option::as_mut(v)?)))
402373
}
403374

404375
#[inline]
@@ -409,7 +380,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
409380
#[inline]
410381
fn into_iter(self) -> Self::IntoIter {
411382
let [#(#names),*] = self.data;
412-
#iterator::flat_map(#into_iter([#(#iter_init),*]), |(k, v)| #option::Some((k, v?)))
383+
#iterator::flat_map(#into_iter([#((#ident::#variants, #names)),*]), |(k, v)| #option::Some((k, v?)))
413384
}
414385
}
415386

0 commit comments

Comments
 (0)