Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented DoubleEndedIterator for EnumIter. #60

Merged
merged 7 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions strum_macros/src/enum_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,24 @@ pub fn enum_iter_inner(ast: &syn::DeriveInput) -> TokenStream {
#[allow(missing_docs)]
#vis struct #iter_name #ty_generics {
idx: usize,
back_idx: usize,
marker: ::std::marker::PhantomData #phantom_data,
}

impl #impl_generics #iter_name #ty_generics #where_clause {
fn get(&self, idx: usize) -> Option<#name #ty_generics> {
match idx {
#(#arms),*
}
}
}

impl #impl_generics ::strum::IntoEnumIterator for #name #ty_generics #where_clause {
type Iterator = #iter_name #ty_generics;
fn iter() -> #iter_name #ty_generics {
#iter_name {
idx:0,
idx: 0,
back_idx: 0,
Nevsor marked this conversation as resolved.
Show resolved Hide resolved
marker: ::std::marker::PhantomData,
}
}
Expand All @@ -79,9 +89,7 @@ pub fn enum_iter_inner(ast: &syn::DeriveInput) -> TokenStream {
type Item = #name #ty_generics;

fn next(&mut self) -> Option<#name #ty_generics> {
let output = match self.idx {
#(#arms),*
};
let output = self.get(self.idx);
Nevsor marked this conversation as resolved.
Show resolved Hide resolved

self.idx += 1;
output
Expand All @@ -99,10 +107,24 @@ pub fn enum_iter_inner(ast: &syn::DeriveInput) -> TokenStream {
}
}

impl #impl_generics DoubleEndedIterator for #iter_name #ty_generics #where_clause {
fn next_back(&mut self) -> Option<#name #ty_generics> {
if self.back_idx >= #variant_count {
Nevsor marked this conversation as resolved.
Show resolved Hide resolved
return None
}

let output = self.get(#variant_count - self.back_idx - 1);

self.back_idx += 1;
output
}
}

impl #impl_generics Clone for #iter_name #ty_generics #where_clause {
fn clone(&self) -> #iter_name #ty_generics {
#iter_name {
idx: self.idx,
back_idx: self.back_idx,
marker: self.marker.clone(),
}
}
Expand Down
15 changes: 15 additions & 0 deletions strum_tests/tests/enum_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,18 @@ fn cycle_test() {
];
assert_eq!(expected, results);
}

Nevsor marked this conversation as resolved.
Show resolved Hide resolved
#[test]
fn reverse_test() {
let results = Week::iter().rev().collect::<Vec<_>>();
let expected = vec![
Week::Saturday,
Week::Friday,
Week::Thursday,
Week::Wednesday,
Week::Tuesday,
Week::Monday,
Week::Sunday,
];
assert_eq!(expected, results);
}