Skip to content

Commit acdef03

Browse files
committed
Dereference matched value
This ensures patterns are exhaustive even in the case of an enum without variants. Resolves #16
1 parent 46b561c commit acdef03

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

enum-iterator-derive/src/lib.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,12 @@ where
236236
|(variant, next_variants)| {
237237
let next = init_enum(ty, next_variants, direction);
238238
let id = &variant.ident;
239+
let destructuring = field_bindings(&variant.fields);
239240
let assignments = field_assignments(&variant.fields);
240241
let bindings = bindings().take(variant.fields.len()).collect::<Vec<_>>();
241242
let tuple = advance_tuple(&bindings, direction);
242243
quote! {
243-
#ty::#id { #assignments } => {
244+
#ty::#id { #destructuring } => {
244245
#tuple
245246
.map(|(#(#bindings,)*)| #ty::#id { #assignments })
246247
.or_else(|| #next)
@@ -249,7 +250,7 @@ where
249250
},
250251
);
251252
quote! {
252-
match self {
253+
match *self {
253254
#(#arms,)*
254255
}
255256
}
@@ -308,6 +309,21 @@ where
308309
.collect()
309310
}
310311

312+
fn field_bindings<'a, I>(fields: I) -> TokenStream
313+
where
314+
I: IntoIterator<Item = &'a Field>,
315+
{
316+
fields
317+
.into_iter()
318+
.enumerate()
319+
.zip(bindings())
320+
.map(|((i, field), binding)| {
321+
let field_id = field_id(field, i);
322+
quote! { #field_id: ref #binding, }
323+
})
324+
.collect()
325+
}
326+
311327
fn bindings() -> impl Iterator<Item = Ident> {
312328
(0..).map(|i| Ident::new(&format!("x{i}"), Span::call_site()))
313329
}

enum-iterator/tests/derive.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,21 @@ fn reverse_all_works() {
145145
};
146146
assert_eq!(reverse_all::<Move>().collect::<Vec<_>>(), expected);
147147
}
148+
149+
#[derive(Debug, PartialEq, Sequence)]
150+
enum Empty {}
151+
152+
#[test]
153+
fn empty_cadinality_is_zero() {
154+
assert_eq!(cardinality::<Empty>(), 0);
155+
}
156+
157+
#[test]
158+
fn all_values_of_empty_are_yielded() {
159+
assert_eq!(all::<Empty>().collect::<Vec<_>>(), Vec::new());
160+
}
161+
162+
#[test]
163+
fn all_values_of_empty_are_yielded_in_reverse() {
164+
assert_eq!(reverse_all::<Empty>().collect::<Vec<_>>(), Vec::new());
165+
}

0 commit comments

Comments
 (0)