use std::borrow::Borrow; use std::hash::Hash; use hashlink::{linked_hash_map::IterMut, LinkedHashMap}; pub struct LinkedHashMapView<'a, K, V> { hash_map: &'a mut LinkedHashMap, ignore_key: K, } impl<'a, K: Eq + Hash, V> LinkedHashMapView<'a, K, V> { pub fn get(&self, key: &Q) -> Option<&V> where K: Borrow, Q: Hash + Eq + ?Sized, { if self.ignore_key.borrow().eq(key) { None } else { self.hash_map.get(key) } } pub fn get_mut(&mut self, key: &Q) -> Option<&mut V> where K: Borrow, Q: Hash + Eq + ?Sized, { if self.ignore_key.borrow().eq(key) { None } else { self.hash_map.get_mut(key) } } pub fn iter(&self) -> impl Iterator { self.hash_map.iter().filter(|(k, _)| **k != self.ignore_key) } pub fn iter_mut(&mut self) -> impl Iterator { self.hash_map .iter_mut() .filter(|(k, _)| **k != self.ignore_key) } } pub struct LinkedHashMapIterExt<'a, K, V> { hash_map: *mut LinkedHashMap, it: IterMut<'a, K, V>, } impl<'a, K, V> LinkedHashMapIterExt<'a, K, V> { pub fn new(hash_map: &'a mut LinkedHashMap) -> Self { let hash_map_ptr = hash_map as _; let it = hash_map.iter_mut(); Self { hash_map: hash_map_ptr, it, } } } impl<'a, K: Copy + Clone, V> Iterator for LinkedHashMapIterExt<'a, K, V> { type Item = (&'a K, (&'a mut V, LinkedHashMapView<'a, K, V>)); fn next(&mut self) -> Option { self.it.next().map(|(k, v)| { ( k, ( v, LinkedHashMapView { hash_map: unsafe { &mut *self.hash_map }, ignore_key: *k, }, ), ) }) } }