From 7269a884c91d539c6e6ec275a6d9ef6d6bd5f142 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Tue, 22 Jan 2019 16:46:05 +0100 Subject: [PATCH 1/2] Expose `AllocId`s for priroda --- src/mono_hash_map.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/mono_hash_map.rs b/src/mono_hash_map.rs index 278bbd9cf2b1..56d6e1400f3e 100644 --- a/src/mono_hash_map.rs +++ b/src/mono_hash_map.rs @@ -18,8 +18,11 @@ use crate::AllocMap; pub struct MonoHashMap(RefCell>>); impl MonoHashMap { - pub fn values(&self, f: impl FnOnce(&mut dyn Iterator) -> 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 memory of constants does not show up in this list. + pub fn iter(&self, f: impl FnOnce(&mut dyn Iterator) -> T) -> T { + f(&mut self.0.borrow().iter().map(|(k, v)| (k, &**v))) } } From 9f06cdc87a450b486a5bfea209acf958de92dfc8 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Tue, 22 Jan 2019 17:19:19 +0100 Subject: [PATCH 2/2] Explain Ref problems --- src/mono_hash_map.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/mono_hash_map.rs b/src/mono_hash_map.rs index 56d6e1400f3e..ec1a5fdb4288 100644 --- a/src/mono_hash_map.rs +++ b/src/mono_hash_map.rs @@ -20,7 +20,12 @@ pub struct MonoHashMap(RefCell>>); impl MonoHashMap { /// This function exists for priroda to be able to iterate over all evaluator memory /// - /// The memory of constants does not show up in this list. + /// 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(&self, f: impl FnOnce(&mut dyn Iterator) -> T) -> T { f(&mut self.0.borrow().iter().map(|(k, v)| (k, &**v))) }