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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use std::borrow::Cow;

use super::*;

/// An iterator over the keys of a map.
#[derive(Debug, Clone, Copy)]
pub struct Keys<'map, 'elem: 'map> {
    map: &'map Map<'elem>,
    count: usize,
    index: usize,
}

impl<'map, 'elem> Keys<'map, 'elem> {
    #[inline]
    pub(crate) fn new(map: &'map Map<'elem>) -> Self {
        Self {
            map,
            count: map.key_count(),
            index: 0,
        }
    }
}

impl<'map, 'elem> Iterator for Keys<'map, 'elem> {
    type Item = &'map str;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.index == self.count {
            return None;
        }

        let key = self.map.key(self.index);
        self.index += 1;
        Some(key)
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.count - self.index;
        (len, Some(len))
    }
}

impl<'map, 'elem> ExactSizeIterator for Keys<'map, 'elem> {}

/// An iterator over the values associated with a certain key of a map.
#[derive(Debug, Clone)]
pub struct ValueIter<'map, 'elem: 'map, T> {
    map: &'map Map<'elem>,
    key: CString,
    count: i32,
    index: i32,
    _variance: PhantomData<fn() -> T>,
}

macro_rules! impl_value_iter {
    ($value_type:path, $type:ty, $func:ident) => {
        impl<'map, 'elem> ValueIter<'map, 'elem, $type> {
            /// Creates a `ValueIter` from the given `map` and `key`.
            ///
            /// # Safety
            /// The caller must ensure `key` is valid.
            #[inline]
            pub(crate) unsafe fn new(map: &'map Map<'elem>, key: CString) -> Result<Self> {
                // Check if the value type is correct.
                match map.value_type_raw_unchecked(&key)? {
                    $value_type => {}
                    _ => return Err(Error::WrongValueType),
                };

                let count = map.value_count_raw_unchecked(&key)? as i32;
                Ok(Self {
                    map,
                    key,
                    count,
                    index: 0,
                    _variance: PhantomData,
                })
            }
        }

        impl<'map, 'elem> Iterator for ValueIter<'map, 'elem, $type> {
            type Item = $type;

            #[inline]
            fn next(&mut self) -> Option<Self::Item> {
                if self.index == self.count {
                    return None;
                }

                let value = unsafe { self.map.$func(&self.key, self.index).unwrap() };
                self.index += 1;

                Some(value)
            }

            #[inline]
            fn size_hint(&self) -> (usize, Option<usize>) {
                let len = (self.count - self.index) as usize;
                (len, Some(len))
            }
        }

        impl<'map, 'elem> ExactSizeIterator for ValueIter<'map, 'elem, $type> {}
    };
}

impl_value_iter!(ValueType::Int, i64, get_int_raw_unchecked);
impl_value_iter!(ValueType::Float, f64, get_float_raw_unchecked);
impl_value_iter!(ValueType::Data, &'map [u8], get_data_raw_unchecked);
impl_value_iter!(ValueType::Node, Node<'elem>, get_node_raw_unchecked);
impl_value_iter!(ValueType::Frame, FrameRef<'elem>, get_frame_raw_unchecked);
impl_value_iter!(
    ValueType::Function,
    Function<'elem>,
    get_function_raw_unchecked
);