Merge pull request #600 from solson/rustup

Expose `AllocId`s for priroda
This commit is contained in:
Ralf Jung 2019-01-22 19:32:06 +01:00 committed by GitHub
commit 91c96b4cd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,8 +18,16 @@ use crate::AllocMap;
pub struct MonoHashMap<K: Hash + Eq, V>(RefCell<FxHashMap<K, Box<V>>>);
impl<K: Hash + Eq, V> MonoHashMap<K, V> {
pub fn values<T>(&self, f: impl FnOnce(&mut dyn Iterator<Item=&V>) -> T) -> T {
f(&mut self.0.borrow().values().map(|v| &**v))
/// This function exists for priroda to be able to iterate over all evaluator memory
///
/// The function is somewhat roundabout with the closure argument because internally the
/// `MonoHashMap` uses a `RefCell`. When iterating over the `HashMap` inside the `RefCell`,
/// we need to keep a borrow to the `HashMap` inside the iterator. The borrow is only alive
/// as long as the `Ref` returned by `RefCell::borrow()` is alive. So we can't return the
/// iterator, as that would drop the `Ref`. We can't return both, as it's not possible in Rust
/// to have a struct/tuple with a field that refers to another field.
pub fn iter<T>(&self, f: impl FnOnce(&mut dyn Iterator<Item=(&K, &V)>) -> T) -> T {
f(&mut self.0.borrow().iter().map(|(k, v)| (k, &**v)))
}
}