Skip to content

Commit 6e1789e

Browse files
committed
feat(array): get_mut() now uses ArrayKey
1 parent 86348f4 commit 6e1789e

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/types/array.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,26 @@ impl ZendHashTable {
234234
/// assert_eq!(ht.get("test").and_then(|zv| zv.str()), Some("hello world"));
235235
/// ```
236236
#[must_use]
237-
pub fn get_mut(&self, key: &'_ str) -> Option<&mut Zval> {
238-
let str = CString::new(key).ok()?;
239-
unsafe { zend_hash_str_find(self, str.as_ptr(), key.len() as _).as_mut() }
237+
pub fn get_mut<'a, K>(&self, key: K) -> Option<&mut Zval> where K: Into<ArrayKey<'a>>{
238+
match key.into() {
239+
ArrayKey::Long(index) => {
240+
unsafe { zend_hash_index_find(self, index as zend_ulong).as_mut() }
241+
}
242+
ArrayKey::String(key) => {
243+
if let Ok(index) = i64::from_str(key.as_str()) {
244+
unsafe { zend_hash_index_find(self, index as zend_ulong).as_mut() }
245+
} else {
246+
unsafe { zend_hash_str_find(self, CString::new(key.as_str()).ok()?.as_ptr(), key.len() as _).as_mut() }
247+
}
248+
}
249+
ArrayKey::Str(key) => {
250+
if let Ok(index) = i64::from_str(key) {
251+
unsafe { zend_hash_index_find(self, index as zend_ulong).as_mut() }
252+
} else {
253+
unsafe { zend_hash_str_find(self, CString::new(key).ok()?.as_ptr(), key.len() as _).as_mut() }
254+
}
255+
}
256+
}
240257
}
241258

242259
/// Attempts to retrieve a value from the hash table with an index.
@@ -662,7 +679,7 @@ pub struct Iter<'a> {
662679
}
663680

664681
/// Represents the key of a PHP array, which can be either a long or a string.
665-
#[derive(Debug, PartialEq)]
682+
#[derive(Debug, Clone, PartialEq)]
666683
pub enum ArrayKey<'a> {
667684
/// A numerical key.
668685
Long(i64),

0 commit comments

Comments
 (0)