• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

davidcole1340 / ext-php-rs / 16431384382

22 Jul 2025 12:30AM UTC coverage: 27.022% (-0.1%) from 27.144%
16431384382

Pull #535

github

web-flow
Merge ccce30c62 into 6869625f4
Pull Request #535: feat(array): introducing BTreeMap conversion and refactoring HashMap conversion

7 of 36 new or added lines in 4 files covered. (19.44%)

78 existing lines in 1 file now uncovered.

1126 of 4167 relevant lines covered (27.02%)

5.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

17.21
/src/types/array/mod.rs
1
//! Represents an array in PHP. As all arrays in PHP are associative arrays,
2
//! they are represented by hash tables.
3

4
use std::{convert::TryFrom, ffi::CString, fmt::Debug, ptr, str::FromStr};
5

6
use crate::{
7
    boxed::{ZBox, ZBoxable},
8
    convert::{FromZval, IntoZval},
9
    error::Result,
10
    ffi::zend_ulong,
11
    ffi::{
12
        _zend_new_array, zend_array_count, zend_array_destroy, zend_array_dup, zend_hash_clean,
13
        zend_hash_index_del, zend_hash_index_find, zend_hash_index_update,
14
        zend_hash_next_index_insert, zend_hash_str_del, zend_hash_str_find, zend_hash_str_update,
15
        HT_MIN_SIZE,
16
    },
17
    flags::DataType,
18
    types::Zval,
19
};
20

21
mod array_key;
22
mod conversions;
23
mod iterators;
24

25
pub use array_key::ArrayKey;
26
pub use iterators::{Iter, Values};
27

28
/// A PHP hashtable.
29
///
30
/// In PHP, arrays are represented as hashtables. This allows you to push values
31
/// onto the end of the array like a vector, while also allowing you to insert
32
/// at arbitrary string key indexes.
33
///
34
/// A PHP hashtable stores values as [`Zval`]s. This allows you to insert
35
/// different types into the same hashtable. Types must implement [`IntoZval`]
36
/// to be able to be inserted into the hashtable.
37
///
38
/// # Examples
39
///
40
/// ```no_run
41
/// use ext_php_rs::types::ZendHashTable;
42
///
43
/// let mut ht = ZendHashTable::new();
44
/// ht.push(1);
45
/// ht.push("Hello, world!");
46
/// ht.insert("Like", "Hashtable");
47
///
48
/// assert_eq!(ht.len(), 3);
49
/// assert_eq!(ht.get_index(0).and_then(|zv| zv.long()), Some(1));
50
/// ```
51
pub type ZendHashTable = crate::ffi::HashTable;
52

53
// Clippy complains about there being no `is_empty` function when implementing
54
// on the alias `ZendStr` :( <https://github.com/rust-lang/rust-clippy/issues/7702>
55
#[allow(clippy::len_without_is_empty)]
56
impl ZendHashTable {
57
    /// Creates a new, empty, PHP hashtable, returned inside a [`ZBox`].
58
    ///
59
    /// # Example
60
    ///
61
    /// ```no_run
62
    /// use ext_php_rs::types::ZendHashTable;
63
    ///
64
    /// let ht = ZendHashTable::new();
65
    /// ```
66
    ///
67
    /// # Panics
68
    ///
69
    /// Panics if memory for the hashtable could not be allocated.
70
    #[must_use]
71
    pub fn new() -> ZBox<Self> {
9✔
72
        Self::with_capacity(HT_MIN_SIZE)
9✔
73
    }
74

75
    /// Creates a new, empty, PHP hashtable with an initial size, returned
76
    /// inside a [`ZBox`].
77
    ///
78
    /// # Parameters
79
    ///
80
    /// * `size` - The size to initialize the array with.
81
    ///
82
    /// # Example
83
    ///
84
    /// ```no_run
85
    /// use ext_php_rs::types::ZendHashTable;
86
    ///
87
    /// let ht = ZendHashTable::with_capacity(10);
88
    /// ```
89
    ///
90
    /// # Panics
91
    ///
92
    /// Panics if memory for the hashtable could not be allocated.
93
    #[must_use]
94
    pub fn with_capacity(size: u32) -> ZBox<Self> {
13✔
95
        unsafe {
96
            // SAFETY: PHP allocator handles the creation of the array.
97
            #[allow(clippy::used_underscore_items)]
98
            let ptr = _zend_new_array(size);
39✔
99

100
            // SAFETY: `as_mut()` checks if the pointer is null, and panics if it is not.
101
            ZBox::from_raw(
102
                ptr.as_mut()
39✔
103
                    .expect("Failed to allocate memory for hashtable"),
13✔
104
            )
105
        }
106
    }
107

108
    /// Returns the current number of elements in the array.
109
    ///
110
    /// # Example
111
    ///
112
    /// ```no_run
113
    /// use ext_php_rs::types::ZendHashTable;
114
    ///
115
    /// let mut ht = ZendHashTable::new();
116
    ///
117
    /// ht.push(1);
118
    /// ht.push("Hello, world");
119
    ///
120
    /// assert_eq!(ht.len(), 2);
121
    /// ```
122
    #[must_use]
123
    pub fn len(&self) -> usize {
22✔
124
        unsafe { zend_array_count(ptr::from_ref(self).cast_mut()) as usize }
66✔
125
    }
126

127
    /// Returns whether the hash table is empty.
128
    ///
129
    /// # Example
130
    ///
131
    /// ```no_run
132
    /// use ext_php_rs::types::ZendHashTable;
133
    ///
134
    /// let mut ht = ZendHashTable::new();
135
    ///
136
    /// assert_eq!(ht.is_empty(), true);
137
    ///
138
    /// ht.push(1);
139
    /// ht.push("Hello, world");
140
    ///
141
    /// assert_eq!(ht.is_empty(), false);
142
    /// ```
143
    #[must_use]
144
    pub fn is_empty(&self) -> bool {
×
145
        self.len() == 0
×
146
    }
147

148
    /// Clears the hash table, removing all values.
149
    ///
150
    /// # Example
151
    ///
152
    /// ```no_run
153
    /// use ext_php_rs::types::ZendHashTable;
154
    ///
155
    /// let mut ht = ZendHashTable::new();
156
    ///
157
    /// ht.insert("test", "hello world");
158
    /// assert_eq!(ht.is_empty(), false);
159
    ///
160
    /// ht.clear();
161
    /// assert_eq!(ht.is_empty(), true);
162
    /// ```
163
    pub fn clear(&mut self) {
×
164
        unsafe { zend_hash_clean(self) }
×
165
    }
166

167
    /// Attempts to retrieve a value from the hash table with a string key.
168
    ///
169
    /// # Parameters
170
    ///
171
    /// * `key` - The key to search for in the hash table.
172
    ///
173
    /// # Returns
174
    ///
175
    /// * `Some(&Zval)` - A reference to the zval at the position in the hash
176
    ///   table.
177
    /// * `None` - No value at the given position was found.
178
    ///
179
    /// # Example
180
    ///
181
    /// ```no_run
182
    /// use ext_php_rs::types::ZendHashTable;
183
    ///
184
    /// let mut ht = ZendHashTable::new();
185
    ///
186
    /// ht.insert("test", "hello world");
187
    /// assert_eq!(ht.get("test").and_then(|zv| zv.str()), Some("hello world"));
188
    /// ```
189
    #[must_use]
190
    pub fn get<'a, K>(&self, key: K) -> Option<&Zval>
12✔
191
    where
192
        K: Into<ArrayKey<'a>>,
193
    {
194
        match key.into() {
12✔
195
            ArrayKey::Long(index) => unsafe {
196
                #[allow(clippy::cast_sign_loss)]
197
                zend_hash_index_find(self, index as zend_ulong).as_ref()
×
198
            },
199
            ArrayKey::String(key) => {
×
200
                if let Ok(index) = i64::from_str(key.as_str()) {
×
201
                    #[allow(clippy::cast_sign_loss)]
×
202
                    unsafe {
203
                        zend_hash_index_find(self, index as zend_ulong).as_ref()
×
204
                    }
205
                } else {
206
                    unsafe {
207
                        zend_hash_str_find(
208
                            self,
×
209
                            CString::new(key.as_str()).ok()?.as_ptr(),
×
UNCOV
210
                            key.len() as _,
×
211
                        )
212
                        .as_ref()
213
                    }
214
                }
215
            }
216
            ArrayKey::Str(key) => {
6✔
217
                if let Ok(index) = i64::from_str(key) {
6✔
UNCOV
218
                    #[allow(clippy::cast_sign_loss)]
×
219
                    unsafe {
UNCOV
220
                        zend_hash_index_find(self, index as zend_ulong).as_ref()
×
221
                    }
222
                } else {
223
                    unsafe {
224
                        zend_hash_str_find(self, CString::new(key).ok()?.as_ptr(), key.len() as _)
30✔
225
                            .as_ref()
226
                    }
227
                }
228
            }
229
        }
230
    }
231

232
    /// Attempts to retrieve a value from the hash table with a string key.
233
    ///
234
    /// # Parameters
235
    ///
236
    /// * `key` - The key to search for in the hash table.
237
    ///
238
    /// # Returns
239
    ///
240
    /// * `Some(&Zval)` - A reference to the zval at the position in the hash
241
    ///   table.
242
    /// * `None` - No value at the given position was found.
243
    ///
244
    /// # Example
245
    ///
246
    /// ```no_run
247
    /// use ext_php_rs::types::ZendHashTable;
248
    ///
249
    /// let mut ht = ZendHashTable::new();
250
    ///
251
    /// ht.insert("test", "hello world");
252
    /// assert_eq!(ht.get("test").and_then(|zv| zv.str()), Some("hello world"));
253
    /// ```
254
    // TODO: Verify if this is safe to use, as it allows mutating the
255
    // hashtable while only having a reference to it. #461
256
    #[allow(clippy::mut_from_ref)]
257
    #[must_use]
258
    pub fn get_mut<'a, K>(&self, key: K) -> Option<&mut Zval>
×
259
    where
260
        K: Into<ArrayKey<'a>>,
261
    {
UNCOV
262
        match key.into() {
×
263
            ArrayKey::Long(index) => unsafe {
264
                #[allow(clippy::cast_sign_loss)]
UNCOV
265
                zend_hash_index_find(self, index as zend_ulong).as_mut()
×
266
            },
UNCOV
267
            ArrayKey::String(key) => {
×
UNCOV
268
                if let Ok(index) = i64::from_str(key.as_str()) {
×
UNCOV
269
                    #[allow(clippy::cast_sign_loss)]
×
270
                    unsafe {
UNCOV
271
                        zend_hash_index_find(self, index as zend_ulong).as_mut()
×
272
                    }
273
                } else {
274
                    unsafe {
275
                        zend_hash_str_find(
UNCOV
276
                            self,
×
UNCOV
277
                            CString::new(key.as_str()).ok()?.as_ptr(),
×
UNCOV
278
                            key.len() as _,
×
279
                        )
280
                        .as_mut()
281
                    }
282
                }
283
            }
UNCOV
284
            ArrayKey::Str(key) => {
×
285
                if let Ok(index) = i64::from_str(key) {
×
UNCOV
286
                    #[allow(clippy::cast_sign_loss)]
×
287
                    unsafe {
288
                        zend_hash_index_find(self, index as zend_ulong).as_mut()
×
289
                    }
290
                } else {
291
                    unsafe {
UNCOV
292
                        zend_hash_str_find(self, CString::new(key).ok()?.as_ptr(), key.len() as _)
×
293
                            .as_mut()
294
                    }
295
                }
296
            }
297
        }
298
    }
299

300
    /// Attempts to retrieve a value from the hash table with an index.
301
    ///
302
    /// # Parameters
303
    ///
304
    /// * `key` - The key to search for in the hash table.
305
    ///
306
    /// # Returns
307
    ///
308
    /// * `Some(&Zval)` - A reference to the zval at the position in the hash
309
    ///   table.
310
    /// * `None` - No value at the given position was found.
311
    ///
312
    /// # Example
313
    ///
314
    /// ```no_run
315
    /// use ext_php_rs::types::ZendHashTable;
316
    ///
317
    /// let mut ht = ZendHashTable::new();
318
    ///
319
    /// ht.push(100);
320
    /// assert_eq!(ht.get_index(0).and_then(|zv| zv.long()), Some(100));
321
    /// ```
322
    #[must_use]
UNCOV
323
    pub fn get_index(&self, key: i64) -> Option<&Zval> {
×
324
        #[allow(clippy::cast_sign_loss)]
325
        unsafe {
UNCOV
326
            zend_hash_index_find(self, key as zend_ulong).as_ref()
×
327
        }
328
    }
329

330
    /// Attempts to retrieve a value from the hash table with an index.
331
    ///
332
    /// # Parameters
333
    ///
334
    /// * `key` - The key to search for in the hash table.
335
    ///
336
    /// # Returns
337
    ///
338
    /// * `Some(&Zval)` - A reference to the zval at the position in the hash
339
    ///   table.
340
    /// * `None` - No value at the given position was found.
341
    ///
342
    /// # Example
343
    ///
344
    /// ```no_run
345
    /// use ext_php_rs::types::ZendHashTable;
346
    ///
347
    /// let mut ht = ZendHashTable::new();
348
    ///
349
    /// ht.push(100);
350
    /// assert_eq!(ht.get_index(0).and_then(|zv| zv.long()), Some(100));
351
    /// ```
352
    // TODO: Verify if this is safe to use, as it allows mutating the
353
    // hashtable while only having a reference to it. #461
354
    #[allow(clippy::mut_from_ref)]
355
    #[must_use]
356
    pub fn get_index_mut(&self, key: i64) -> Option<&mut Zval> {
×
357
        unsafe {
358
            #[allow(clippy::cast_sign_loss)]
359
            zend_hash_index_find(self, key as zend_ulong).as_mut()
×
360
        }
361
    }
362

363
    /// Attempts to remove a value from the hash table with a string key.
364
    ///
365
    /// # Parameters
366
    ///
367
    /// * `key` - The key to remove from the hash table.
368
    ///
369
    /// # Returns
370
    ///
371
    /// * `Some(())` - Key was successfully removed.
372
    /// * `None` - No key was removed, did not exist.
373
    ///
374
    /// # Example
375
    ///
376
    /// ```no_run
377
    /// use ext_php_rs::types::ZendHashTable;
378
    ///
379
    /// let mut ht = ZendHashTable::new();
380
    ///
381
    /// ht.insert("test", "hello world");
382
    /// assert_eq!(ht.len(), 1);
383
    ///
384
    /// ht.remove("test");
385
    /// assert_eq!(ht.len(), 0);
386
    /// ```
UNCOV
387
    pub fn remove<'a, K>(&mut self, key: K) -> Option<()>
×
388
    where
389
        K: Into<ArrayKey<'a>>,
390
    {
UNCOV
391
        let result = match key.into() {
×
392
            ArrayKey::Long(index) => unsafe {
393
                #[allow(clippy::cast_sign_loss)]
UNCOV
394
                zend_hash_index_del(self, index as zend_ulong)
×
395
            },
UNCOV
396
            ArrayKey::String(key) => {
×
UNCOV
397
                if let Ok(index) = i64::from_str(key.as_str()) {
×
UNCOV
398
                    #[allow(clippy::cast_sign_loss)]
×
399
                    unsafe {
UNCOV
400
                        zend_hash_index_del(self, index as zend_ulong)
×
401
                    }
402
                } else {
403
                    unsafe {
404
                        zend_hash_str_del(
UNCOV
405
                            self,
×
UNCOV
406
                            CString::new(key.as_str()).ok()?.as_ptr(),
×
407
                            key.len() as _,
×
408
                        )
409
                    }
410
                }
411
            }
UNCOV
412
            ArrayKey::Str(key) => {
×
UNCOV
413
                if let Ok(index) = i64::from_str(key) {
×
UNCOV
414
                    #[allow(clippy::cast_sign_loss)]
×
415
                    unsafe {
UNCOV
416
                        zend_hash_index_del(self, index as zend_ulong)
×
417
                    }
418
                } else {
419
                    unsafe {
UNCOV
420
                        zend_hash_str_del(self, CString::new(key).ok()?.as_ptr(), key.len() as _)
×
421
                    }
422
                }
423
            }
424
        };
425

UNCOV
426
        if result < 0 {
×
UNCOV
427
            None
×
428
        } else {
UNCOV
429
            Some(())
×
430
        }
431
    }
432

433
    /// Attempts to remove a value from the hash table with a string key.
434
    ///
435
    /// # Parameters
436
    ///
437
    /// * `key` - The key to remove from the hash table.
438
    ///
439
    /// # Returns
440
    ///
441
    /// * `Ok(())` - Key was successfully removed.
442
    /// * `None` - No key was removed, did not exist.
443
    ///
444
    /// # Example
445
    ///
446
    /// ```no_run
447
    /// use ext_php_rs::types::ZendHashTable;
448
    ///
449
    /// let mut ht = ZendHashTable::new();
450
    ///
451
    /// ht.push("hello");
452
    /// assert_eq!(ht.len(), 1);
453
    ///
454
    /// ht.remove_index(0);
455
    /// assert_eq!(ht.len(), 0);
456
    /// ```
457
    pub fn remove_index(&mut self, key: i64) -> Option<()> {
×
458
        let result = unsafe {
459
            #[allow(clippy::cast_sign_loss)]
460
            zend_hash_index_del(self, key as zend_ulong)
×
461
        };
462

463
        if result < 0 {
×
464
            None
×
465
        } else {
UNCOV
466
            Some(())
×
467
        }
468
    }
469

470
    /// Attempts to insert an item into the hash table, or update if the key
471
    /// already exists. Returns nothing in a result if successful.
472
    ///
473
    /// # Parameters
474
    ///
475
    /// * `key` - The key to insert the value at in the hash table.
476
    /// * `value` - The value to insert into the hash table.
477
    ///
478
    /// # Returns
479
    ///
480
    /// Returns nothing in a result on success.
481
    ///
482
    /// # Errors
483
    ///
484
    /// Returns an error if the key could not be converted into a [`CString`],
485
    /// or converting the value into a [`Zval`] failed.
486
    ///
487
    /// # Example
488
    ///
489
    /// ```no_run
490
    /// use ext_php_rs::types::ZendHashTable;
491
    ///
492
    /// let mut ht = ZendHashTable::new();
493
    ///
494
    /// ht.insert("a", "A");
495
    /// ht.insert("b", "B");
496
    /// ht.insert("c", "C");
497
    /// assert_eq!(ht.len(), 3);
498
    /// ```
499
    pub fn insert<'a, K, V>(&mut self, key: K, val: V) -> Result<()>
35✔
500
    where
501
        K: Into<ArrayKey<'a>>,
502
        V: IntoZval,
503
    {
504
        let mut val = val.into_zval(false)?;
105✔
UNCOV
505
        match key.into() {
×
506
            ArrayKey::Long(index) => {
19✔
507
                unsafe {
508
                    #[allow(clippy::cast_sign_loss)]
UNCOV
509
                    zend_hash_index_update(self, index as zend_ulong, &raw mut val)
×
510
                };
511
            }
UNCOV
512
            ArrayKey::String(key) => {
×
513
                unsafe {
514
                    zend_hash_str_update(
NEW
UNCOV
515
                        self,
×
NEW
UNCOV
516
                        CString::new(key.as_str())?.as_ptr(),
×
NEW
UNCOV
517
                        key.len(),
×
NEW
UNCOV
518
                        &raw mut val,
×
519
                    )
520
                };
521
            }
522
            ArrayKey::Str(key) => {
16✔
523
                unsafe {
524
                    zend_hash_str_update(self, CString::new(key)?.as_ptr(), key.len(), &raw mut val)
64✔
525
                };
526
            }
527
        }
528
        val.release();
35✔
UNCOV
529
        Ok(())
×
530
    }
531

532
    /// Inserts an item into the hash table at a specified index, or updates if
533
    /// the key already exists. Returns nothing in a result if successful.
534
    ///
535
    /// # Parameters
536
    ///
537
    /// * `key` - The index at which the value should be inserted.
538
    /// * `val` - The value to insert into the hash table.
539
    ///
540
    /// # Returns
541
    ///
542
    /// Returns nothing in a result on success.
543
    ///
544
    /// # Errors
545
    ///
546
    /// Returns an error if converting the value into a [`Zval`] failed.
547
    ///
548
    /// # Example
549
    ///
550
    /// ```no_run
551
    /// use ext_php_rs::types::ZendHashTable;
552
    ///
553
    /// let mut ht = ZendHashTable::new();
554
    ///
555
    /// ht.insert_at_index(0, "A");
556
    /// ht.insert_at_index(5, "B");
557
    /// ht.insert_at_index(0, "C"); // notice overriding index 0
558
    /// assert_eq!(ht.len(), 2);
559
    /// ```
UNCOV
560
    pub fn insert_at_index<V>(&mut self, key: i64, val: V) -> Result<()>
×
561
    where
562
        V: IntoZval,
563
    {
UNCOV
564
        let mut val = val.into_zval(false)?;
×
565
        unsafe {
566
            #[allow(clippy::cast_sign_loss)]
UNCOV
567
            zend_hash_index_update(self, key as zend_ulong, &raw mut val)
×
568
        };
UNCOV
569
        val.release();
×
UNCOV
570
        Ok(())
×
571
    }
572

573
    /// Pushes an item onto the end of the hash table. Returns a result
574
    /// containing nothing if the element was successfully inserted.
575
    ///
576
    /// # Parameters
577
    ///
578
    /// * `val` - The value to insert into the hash table.
579
    ///
580
    /// # Returns
581
    ///
582
    /// Returns nothing in a result on success.
583
    ///
584
    /// # Errors
585
    ///
586
    /// Returns an error if converting the value into a [`Zval`] failed.
587
    ///
588
    /// # Example
589
    ///
590
    /// ```no_run
591
    /// use ext_php_rs::types::ZendHashTable;
592
    ///
593
    /// let mut ht = ZendHashTable::new();
594
    ///
595
    /// ht.push("a");
596
    /// ht.push("b");
597
    /// ht.push("c");
598
    /// assert_eq!(ht.len(), 3);
599
    /// ```
UNCOV
600
    pub fn push<V>(&mut self, val: V) -> Result<()>
×
601
    where
602
        V: IntoZval,
603
    {
UNCOV
604
        let mut val = val.into_zval(false)?;
×
UNCOV
605
        unsafe { zend_hash_next_index_insert(self, &raw mut val) };
×
UNCOV
606
        val.release();
×
607

608
        Ok(())
×
609
    }
610

611
    /// Checks if the hashtable only contains numerical keys.
612
    ///
613
    /// # Returns
614
    ///
615
    /// True if all keys on the hashtable are numerical.
616
    ///
617
    /// # Example
618
    ///
619
    /// ```no_run
620
    /// use ext_php_rs::types::ZendHashTable;
621
    ///
622
    /// let mut ht = ZendHashTable::new();
623
    ///
624
    /// ht.push(0);
625
    /// ht.push(3);
626
    /// ht.push(9);
627
    /// assert!(ht.has_numerical_keys());
628
    ///
629
    /// ht.insert("obviously not numerical", 10);
630
    /// assert!(!ht.has_numerical_keys());
631
    /// ```
632
    #[must_use]
UNCOV
633
    pub fn has_numerical_keys(&self) -> bool {
×
UNCOV
634
        !self.into_iter().any(|(k, _)| !k.is_long())
×
635
    }
636

637
    /// Checks if the hashtable has numerical, sequential keys.
638
    ///
639
    /// # Returns
640
    ///
641
    /// True if all keys on the hashtable are numerical and are in sequential
642
    /// order (i.e. starting at 0 and not skipping any keys).
643
    ///
644
    /// # Panics
645
    ///
646
    /// Panics if the number of elements in the hashtable exceeds `i64::MAX`.
647
    ///
648
    /// # Example
649
    ///
650
    /// ```no_run
651
    /// use ext_php_rs::types::ZendHashTable;
652
    ///
653
    /// let mut ht = ZendHashTable::new();
654
    ///
655
    /// ht.push(0);
656
    /// ht.push(3);
657
    /// ht.push(9);
658
    /// assert!(ht.has_sequential_keys());
659
    ///
660
    /// ht.insert_at_index(90, 10);
661
    /// assert!(!ht.has_sequential_keys());
662
    /// ```
663
    #[must_use]
UNCOV
664
    pub fn has_sequential_keys(&self) -> bool {
×
UNCOV
665
        !self
×
UNCOV
666
            .into_iter()
×
UNCOV
667
            .enumerate()
×
UNCOV
668
            .any(|(i, (k, _))| ArrayKey::Long(i64::try_from(i).expect("Integer overflow")) != k)
×
669
    }
670

671
    /// Returns an iterator over the values contained inside the hashtable, as
672
    /// if it was a set or list.
673
    ///
674
    /// # Example
675
    ///
676
    /// ```no_run
677
    /// use ext_php_rs::types::ZendHashTable;
678
    ///
679
    /// let mut ht = ZendHashTable::new();
680
    ///
681
    /// for val in ht.values() {
682
    ///     dbg!(val);
683
    /// }
684
    #[inline]
685
    #[must_use]
UNCOV
686
    pub fn values(&self) -> Values<'_> {
×
687
        Values::new(self)
×
688
    }
689

690
    /// Returns an iterator over the key(s) and value contained inside the
691
    /// hashtable.
692
    ///
693
    /// # Example
694
    ///
695
    /// ```no_run
696
    /// use ext_php_rs::types::{ZendHashTable, ArrayKey};
697
    ///
698
    /// let mut ht = ZendHashTable::new();
699
    ///
700
    /// for (key, val) in ht.iter() {
701
    ///     match &key {
702
    ///         ArrayKey::Long(index) => {
703
    ///         }
704
    ///         ArrayKey::String(key) => {
705
    ///         }
706
    ///         ArrayKey::Str(key) => {
707
    ///         }
708
    ///     }
709
    ///     dbg!(key, val);
710
    /// }
711
    #[inline]
712
    #[must_use]
UNCOV
713
    pub fn iter(&self) -> Iter<'_> {
×
UNCOV
714
        self.into_iter()
×
715
    }
716
}
717

718
unsafe impl ZBoxable for ZendHashTable {
719
    fn free(&mut self) {
7✔
720
        // SAFETY: ZBox has immutable access to `self`.
721
        unsafe { zend_array_destroy(self) }
14✔
722
    }
723
}
724

725
impl Debug for ZendHashTable {
UNCOV
726
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
×
UNCOV
727
        f.debug_map()
×
UNCOV
728
            .entries(self.into_iter().map(|(k, v)| (k.to_string(), v)))
×
729
            .finish()
730
    }
731
}
732

733
impl ToOwned for ZendHashTable {
734
    type Owned = ZBox<ZendHashTable>;
735

UNCOV
736
    fn to_owned(&self) -> Self::Owned {
×
737
        unsafe {
738
            // SAFETY: FFI call does not modify `self`, returns a new hashtable.
UNCOV
739
            let ptr = zend_array_dup(ptr::from_ref(self).cast_mut());
×
740

741
            // SAFETY: `as_mut()` checks if the pointer is null, and panics if it is not.
742
            ZBox::from_raw(
UNCOV
743
                ptr.as_mut()
×
UNCOV
744
                    .expect("Failed to allocate memory for hashtable"),
×
745
            )
746
        }
747
    }
748
}
749

750
impl Default for ZBox<ZendHashTable> {
UNCOV
751
    fn default() -> Self {
×
UNCOV
752
        ZendHashTable::new()
×
753
    }
754
}
755

756
impl Clone for ZBox<ZendHashTable> {
UNCOV
757
    fn clone(&self) -> Self {
×
UNCOV
758
        (**self).to_owned()
×
759
    }
760
}
761

762
impl IntoZval for ZBox<ZendHashTable> {
763
    const TYPE: DataType = DataType::Array;
764
    const NULLABLE: bool = false;
765

UNCOV
766
    fn set_zval(self, zv: &mut Zval, _: bool) -> Result<()> {
×
UNCOV
767
        zv.set_hashtable(self);
×
UNCOV
768
        Ok(())
×
769
    }
770
}
771

772
impl<'a> FromZval<'a> for &'a ZendHashTable {
773
    const TYPE: DataType = DataType::Array;
774

UNCOV
775
    fn from_zval(zval: &'a Zval) -> Option<Self> {
×
UNCOV
776
        zval.array()
×
777
    }
778
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc