ext_php_rs/zend/
linked_list.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::marker::PhantomData;

use crate::ffi::{zend_llist, zend_llist_element, zend_llist_get_next_ex};

pub type ZendLinkedList = zend_llist;

impl ZendLinkedList {
    pub fn iter<T>(&self) -> ZendLinkedListIterator<T> {
        ZendLinkedListIterator::new(self)
    }
}

pub struct ZendLinkedListIterator<'a, T> {
    list: &'a zend_llist,
    position: *mut zend_llist_element,
    _marker: PhantomData<T>,
}

impl<'a, T> ZendLinkedListIterator<'a, T> {
    fn new(list: &'a ZendLinkedList) -> Self {
        ZendLinkedListIterator {
            list,
            position: list.head,
            _marker: PhantomData,
        }
    }
}

impl<'a, T: 'a> Iterator for ZendLinkedListIterator<'a, T> {
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        if self.position.is_null() {
            return None;
        }
        let ptr = unsafe { (*self.position).data.as_mut_ptr() };
        let value = unsafe { &*(ptr as *const T as *mut T) };
        unsafe {
            zend_llist_get_next_ex(
                self.list as *const ZendLinkedList as *mut ZendLinkedList,
                &mut self.position,
            )
        };
        Some(value)
    }
}