1
0
mirror of https://github.com/danog/strum.git synced 2024-11-26 12:04:38 +01:00

Fix EnumIter for enums named Option (#300)

Co-authored-by: Horaci Macias <hmacias@avaya.com>
This commit is contained in:
horacimacias 2023-10-16 00:08:35 +02:00 committed by GitHub
parent 0199235bb0
commit 42c16664a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 5 deletions

View File

@ -91,7 +91,7 @@ pub fn enum_iter_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
}
impl #impl_generics #iter_name #ty_generics #where_clause {
fn get(&self, idx: usize) -> Option<#name #ty_generics> {
fn get(&self, idx: usize) -> ::core::option::Option<#name #ty_generics> {
match idx {
#(#arms),*
}
@ -112,16 +112,16 @@ pub fn enum_iter_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
impl #impl_generics Iterator for #iter_name #ty_generics #where_clause {
type Item = #name #ty_generics;
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
fn next(&mut self) -> ::core::option::Option<<Self as Iterator>::Item> {
self.nth(0)
}
fn size_hint(&self) -> (usize, Option<usize>) {
fn size_hint(&self) -> (usize, ::core::option::Option<usize>) {
let t = if self.idx + self.back_idx >= #variant_count { 0 } else { #variant_count - self.idx - self.back_idx };
(t, Some(t))
}
fn nth(&mut self, n: usize) -> Option<<Self as Iterator>::Item> {
fn nth(&mut self, n: usize) -> ::core::option::Option<<Self as Iterator>::Item> {
let idx = self.idx + n + 1;
if idx + self.back_idx > #variant_count {
// We went past the end of the iterator. Freeze idx at #variant_count
@ -143,7 +143,7 @@ pub fn enum_iter_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
}
impl #impl_generics DoubleEndedIterator for #iter_name #ty_generics #where_clause {
fn next_back(&mut self) -> Option<<Self as Iterator>::Item> {
fn next_back(&mut self) -> ::core::option::Option<<Self as Iterator>::Item> {
let back_idx = self.back_idx + 1;
if self.idx + back_idx > #variant_count {

View File

@ -211,3 +211,16 @@ fn crate_module_path_test() {
assert_eq!(expected, results);
}
#[test]
fn enum_iter_option() {
#[derive(Debug, Eq, PartialEq, EnumIter)]
enum Option {
BluePill,
RedPill,
}
let results = Option::iter().collect::<Vec<_>>();
let expected = vec![Option::BluePill, Option::RedPill];
assert_eq!(expected, results);
}