cxxbridge_macro/syntax/
map.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use std::borrow::Borrow;
use std::hash::Hash;
use std::ops::Index;
use std::slice;

pub(crate) use self::ordered::OrderedMap;
pub(crate) use self::unordered::UnorderedMap;
pub(crate) use std::collections::hash_map::Entry;

mod ordered {
    use super::{Entry, Iter, UnorderedMap};
    use std::borrow::Borrow;
    use std::hash::Hash;
    use std::mem;

    pub(crate) struct OrderedMap<K, V> {
        map: UnorderedMap<K, usize>,
        vec: Vec<(K, V)>,
    }

    impl<K, V> OrderedMap<K, V> {
        pub(crate) fn new() -> Self {
            OrderedMap {
                map: UnorderedMap::new(),
                vec: Vec::new(),
            }
        }

        pub(crate) fn iter(&self) -> Iter<K, V> {
            Iter(self.vec.iter())
        }

        #[allow(dead_code)] // only used by cxx-build, not cxxbridge-macro
        pub(crate) fn keys(&self) -> impl Iterator<Item = &K> {
            self.vec.iter().map(|(k, _v)| k)
        }
    }

    impl<K, V> OrderedMap<K, V>
    where
        K: Copy + Hash + Eq,
    {
        pub(crate) fn insert(&mut self, key: K, value: V) -> Option<V> {
            match self.map.entry(key) {
                Entry::Occupied(entry) => {
                    let i = &mut self.vec[*entry.get()];
                    Some(mem::replace(&mut i.1, value))
                }
                Entry::Vacant(entry) => {
                    entry.insert(self.vec.len());
                    self.vec.push((key, value));
                    None
                }
            }
        }

        pub(crate) fn contains_key<Q>(&self, key: &Q) -> bool
        where
            K: Borrow<Q>,
            Q: ?Sized + Hash + Eq,
        {
            self.map.contains_key(key)
        }
    }

    impl<'a, K, V> IntoIterator for &'a OrderedMap<K, V> {
        type Item = (&'a K, &'a V);
        type IntoIter = Iter<'a, K, V>;
        fn into_iter(self) -> Self::IntoIter {
            self.iter()
        }
    }
}

mod unordered {
    use crate::syntax::set::UnorderedSet;
    use std::borrow::Borrow;
    use std::collections::hash_map::{Entry, HashMap};
    use std::hash::Hash;

    // Wrapper prohibits accidentally introducing iteration over the map, which
    // could lead to nondeterministic generated code.
    pub(crate) struct UnorderedMap<K, V>(HashMap<K, V>);

    impl<K, V> UnorderedMap<K, V> {
        pub(crate) fn new() -> Self {
            UnorderedMap(HashMap::new())
        }
    }

    impl<K, V> UnorderedMap<K, V>
    where
        K: Hash + Eq,
    {
        pub(crate) fn insert(&mut self, key: K, value: V) -> Option<V> {
            self.0.insert(key, value)
        }

        pub(crate) fn contains_key<Q>(&self, key: &Q) -> bool
        where
            K: Borrow<Q>,
            Q: ?Sized + Hash + Eq,
        {
            self.0.contains_key(key)
        }

        pub(crate) fn get<Q>(&self, key: &Q) -> Option<&V>
        where
            K: Borrow<Q>,
            Q: ?Sized + Hash + Eq,
        {
            self.0.get(key)
        }

        pub(crate) fn entry(&mut self, key: K) -> Entry<K, V> {
            self.0.entry(key)
        }

        #[allow(dead_code)] // only used by cxx-build, not cxxbridge-macro
        pub(crate) fn remove<Q>(&mut self, key: &Q) -> Option<V>
        where
            K: Borrow<Q>,
            Q: ?Sized + Hash + Eq,
        {
            self.0.remove(key)
        }

        pub(crate) fn keys(&self) -> UnorderedSet<K>
        where
            K: Copy,
        {
            let mut set = UnorderedSet::new();
            for key in self.0.keys() {
                set.insert(*key);
            }
            set
        }
    }
}

pub(crate) struct Iter<'a, K, V>(slice::Iter<'a, (K, V)>);

impl<'a, K, V> Iterator for Iter<'a, K, V> {
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        let (k, v) = self.0.next()?;
        Some((k, v))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}

impl<K, V> Default for UnorderedMap<K, V> {
    fn default() -> Self {
        UnorderedMap::new()
    }
}

impl<Q, K, V> Index<&Q> for UnorderedMap<K, V>
where
    K: Borrow<Q> + Hash + Eq,
    Q: ?Sized + Hash + Eq,
{
    type Output = V;

    fn index(&self, key: &Q) -> &V {
        self.get(key).unwrap()
    }
}