diff --git a/src/types/array.rs b/src/types/array.rs index 974272ee6..29e5c6efd 100644 --- a/src/types/array.rs +++ b/src/types/array.rs @@ -11,7 +11,6 @@ use std::{ }; use crate::types::iterator::IterKey; -use crate::types::ZendLong; use crate::{ boxed::{ZBox, ZBoxable}, convert::{FromZval, IntoZval}, @@ -622,15 +621,9 @@ impl<'a> Iterator for Iter<'a> { ) }; - let r = match key.is_long() { - true => ( - IterKey::Long(key.long().unwrap_or(self.current_num as ZendLong) as u64), - value, - ), - false => match key.try_into() { - Ok(key) => (IterKey::String(key), value), - Err(_) => (IterKey::Long(self.current_num), value), - }, + let r = match IterKey::from_zval(&key) { + Some(key) => (key, value), + None => (IterKey::Long(self.current_num), value), }; unsafe { @@ -686,15 +679,9 @@ impl<'a> DoubleEndedIterator for Iter<'a> { ) }; - let r = match key.is_long() { - true => ( - IterKey::Long(key.long().unwrap_or(self.current_num as ZendLong) as u64), - value, - ), - false => match key.try_into() { - Ok(key) => (IterKey::String(key), value), - Err(_) => (IterKey::Long(self.current_num), value), - }, + let r = match IterKey::from_zval(&key) { + Some(key) => (key, value), + None => (IterKey::Long(self.current_num), value), }; unsafe { diff --git a/src/types/iterator.rs b/src/types/iterator.rs index f3a4a2908..559807635 100644 --- a/src/types/iterator.rs +++ b/src/types/iterator.rs @@ -1,8 +1,7 @@ -use crate::convert::FromZvalMut; +use crate::convert::{FromZval, FromZvalMut}; use crate::ffi::zend_object_iterator; use crate::flags::DataType; -use crate::types::{ZendLong, Zval}; -use std::convert::TryInto; +use crate::types::Zval; use std::fmt::{Debug, Display, Formatter}; /// A PHP Iterator. @@ -93,6 +92,17 @@ impl Display for IterKey { } } +impl FromZval<'_> for IterKey { + const TYPE: DataType = DataType::String; + + fn from_zval(zval: &Zval) -> Option { + match zval.long() { + Some(key) => Some(IterKey::Long(key as u64)), + None => zval.string().map(|key| IterKey::String(key)), + } + } +} + pub struct Iter<'a> { zi: &'a mut ZendIterator, } @@ -117,15 +127,9 @@ impl<'a> Iterator for Iter<'a> { let real_index = self.zi.index - 1; Some(match key { - Some(key) => match key.is_long() { - false => match key.try_into() { - Ok(key) => (IterKey::String(key), value), - Err(_) => (IterKey::Long(real_index), value), - }, - true => ( - IterKey::Long(key.long().unwrap_or(real_index as ZendLong) as u64), - value, - ), + Some(key) => match IterKey::from_zval(&key) { + Some(key) => (key, value), + None => (IterKey::Long(real_index), value), }, None => (IterKey::Long(real_index), value), })