|
1 | 1 | // Copyright (c) 2022-present INESC-ID. |
2 | 2 | // Distributed under the MIT license that can be found in the LICENSE file. |
3 | 3 |
|
4 | | -use crate::{CChar, PostfixDec, PostfixInc, PrefixDec, PrefixInc, Ptr, Value}; |
5 | | -use std::cell::RefCell; |
| 4 | +use crate::{AsPointer, CChar, PostfixDec, PostfixInc, PrefixDec, PrefixInc, Ptr, Value}; |
6 | 5 | use std::collections::BTreeMap; |
7 | 6 | use std::ops::Bound; |
8 | | -use std::rc::Rc; |
9 | 7 |
|
10 | 8 | pub trait MapAccess: Clone + Default { |
11 | 9 | type Key: Ord + Clone; |
@@ -45,8 +43,8 @@ pub struct MapIter<K, MapRef> { |
45 | 43 | key: Option<K>, |
46 | 44 | } |
47 | 45 |
|
48 | | -pub type RefcountMapIter<K, V> = MapIter<K, Ptr<BTreeMap<K, Value<V>>>>; |
49 | | -pub type UnsafeMapIterator<K, V> = MapIter<K, *const BTreeMap<K, Box<V>>>; |
| 46 | +pub type RefcountMapIter<K, V> = MapIter<K, Ptr<BTreeMap<K, Value<(Value<K>, Value<V>)>>>>; |
| 47 | +pub type UnsafeMapIterator<K, V> = MapIter<K, *const BTreeMap<K, Box<(K, V)>>>; |
50 | 48 |
|
51 | 49 | impl<K: Clone, MapRef: Clone> Clone for MapIter<K, MapRef> { |
52 | 50 | fn clone(&self) -> Self { |
@@ -141,44 +139,31 @@ impl<K: Ord + Clone, MapRef: MapAccess<Key = K>> MapIter<K, MapRef> { |
141 | 139 | } |
142 | 140 |
|
143 | 141 | pub trait MapIterator { |
144 | | - type First; |
145 | | - type Second; |
146 | | - fn first(&self) -> Self::First; |
147 | | - fn second(&self) -> Self::Second; |
| 142 | + type Entry; |
| 143 | + fn deref(&self) -> Self::Entry; |
148 | 144 | } |
149 | 145 |
|
150 | 146 | impl<K: Ord + Clone + 'static, V: 'static> MapIterator for RefcountMapIter<K, V> { |
151 | | - type First = Value<K>; |
152 | | - type Second = Value<V>; |
| 147 | + type Entry = Ptr<(Value<K>, Value<V>)>; |
153 | 148 |
|
154 | | - fn first(&self) -> Value<K> { |
155 | | - let key = self.key.as_ref().expect("ub: dereference of end iterator"); |
156 | | - Rc::new(RefCell::new(key.clone())) |
157 | | - } |
158 | | - |
159 | | - fn second(&self) -> Value<V> { |
| 149 | + fn deref(&self) -> Ptr<(Value<K>, Value<V>)> { |
160 | 150 | let key = self.key.as_ref().expect("ub: dereference of end iterator"); |
161 | 151 | self.map |
162 | | - .with(|m| m.get(key).expect("ub: key not found in map").clone()) |
| 152 | + .with(|m| m.get(key).expect("ub: key not found in map").as_pointer()) |
163 | 153 | } |
164 | 154 | } |
165 | 155 |
|
166 | 156 | impl<K: Ord + Clone, V> MapIterator for UnsafeMapIterator<K, V> { |
167 | | - type First = *const K; |
168 | | - type Second = *mut V; |
169 | | - |
170 | | - fn first(&self) -> *const K { |
171 | | - self.key.as_ref().expect("ub: dereference of end iterator") as *const K |
172 | | - } |
| 157 | + type Entry = *mut (K, V); |
173 | 158 |
|
174 | | - fn second(&self) -> *mut V { |
| 159 | + fn deref(&self) -> *mut (K, V) { |
175 | 160 | unsafe { |
176 | | - let map_mut = self.map as *mut BTreeMap<K, Box<V>>; |
| 161 | + let map_mut = self.map as *mut BTreeMap<K, Box<(K, V)>>; |
177 | 162 | let key = self.key.as_ref().expect("ub: dereference of end iterator"); |
178 | 163 | (*map_mut) |
179 | 164 | .get_mut(key) |
180 | 165 | .expect("ub: key not found in map") |
181 | | - .as_mut() as *mut V |
| 166 | + .as_mut() as *mut (K, V) |
182 | 167 | } |
183 | 168 | } |
184 | 169 | } |
|
0 commit comments