Skip to content

Commit 26e06ad

Browse files
committed
reduce duplicated code for iter key
1 parent e5b1ebd commit 26e06ad

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

src/types/array.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use std::{
1111
};
1212

1313
use crate::types::iterator::IterKey;
14-
use crate::types::ZendLong;
1514
use crate::{
1615
boxed::{ZBox, ZBoxable},
1716
convert::{FromZval, IntoZval},
@@ -622,15 +621,9 @@ impl<'a> Iterator for Iter<'a> {
622621
)
623622
};
624623

625-
let r = match key.is_long() {
626-
true => (
627-
IterKey::Long(key.long().unwrap_or(self.current_num as ZendLong) as u64),
628-
value,
629-
),
630-
false => match key.try_into() {
631-
Ok(key) => (IterKey::String(key), value),
632-
Err(_) => (IterKey::Long(self.current_num), value),
633-
},
624+
let r = match IterKey::from_zval(&key) {
625+
Some(key) => (key, value),
626+
None => (IterKey::Long(self.current_num), value),
634627
};
635628

636629
unsafe {
@@ -686,15 +679,9 @@ impl<'a> DoubleEndedIterator for Iter<'a> {
686679
)
687680
};
688681

689-
let r = match key.is_long() {
690-
true => (
691-
IterKey::Long(key.long().unwrap_or(self.current_num as ZendLong) as u64),
692-
value,
693-
),
694-
false => match key.try_into() {
695-
Ok(key) => (IterKey::String(key), value),
696-
Err(_) => (IterKey::Long(self.current_num), value),
697-
},
682+
let r = match IterKey::from_zval(&key) {
683+
Some(key) => (key, value),
684+
None => (IterKey::Long(self.current_num), value),
698685
};
699686

700687
unsafe {

src/types/iterator.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use crate::convert::FromZvalMut;
1+
use crate::convert::{FromZval, FromZvalMut};
22
use crate::ffi::zend_object_iterator;
33
use crate::flags::DataType;
4-
use crate::types::{ZendLong, Zval};
5-
use std::convert::TryInto;
4+
use crate::types::Zval;
65
use std::fmt::{Debug, Display, Formatter};
76

87
/// A PHP Iterator.
@@ -93,6 +92,17 @@ impl Display for IterKey {
9392
}
9493
}
9594

95+
impl FromZval<'_> for IterKey {
96+
const TYPE: DataType = DataType::String;
97+
98+
fn from_zval(zval: &Zval) -> Option<Self> {
99+
match zval.long() {
100+
Some(key) => Some(IterKey::Long(key as u64)),
101+
None => zval.string().map(|key| IterKey::String(key)),
102+
}
103+
}
104+
}
105+
96106
pub struct Iter<'a> {
97107
zi: &'a mut ZendIterator,
98108
}
@@ -117,15 +127,9 @@ impl<'a> Iterator for Iter<'a> {
117127
let real_index = self.zi.index - 1;
118128

119129
Some(match key {
120-
Some(key) => match key.is_long() {
121-
false => match key.try_into() {
122-
Ok(key) => (IterKey::String(key), value),
123-
Err(_) => (IterKey::Long(real_index), value),
124-
},
125-
true => (
126-
IterKey::Long(key.long().unwrap_or(real_index as ZendLong) as u64),
127-
value,
128-
),
130+
Some(key) => match IterKey::from_zval(&key) {
131+
Some(key) => (key, value),
132+
None => (IterKey::Long(real_index), value),
129133
},
130134
None => (IterKey::Long(real_index), value),
131135
})

0 commit comments

Comments
 (0)