Fix gpui2 bugs

Max Brunsfeld and Conrad created

* Compile error for tests that take StdRng
* Dynamic type dowcasting error when emitting events
* Slot error when dropping handles

Co-authored-by: Conrad <conrad@zed.dev>

Change summary

crates/gpui2/src/app.rs               |  2 +-
crates/gpui2/src/app/entity_map.rs    | 10 +++++++---
crates/gpui2/src/app/model_context.rs |  2 +-
crates/gpui2/src/test.rs              |  4 ++--
4 files changed, 11 insertions(+), 7 deletions(-)

Detailed changes

crates/gpui2/src/app.rs 🔗

@@ -403,7 +403,7 @@ impl AppContext {
     fn apply_emit_effect(&mut self, emitter: EntityId, event: Box<dyn Any>) {
         self.event_listeners
             .clone()
-            .retain(&emitter, |handler| handler(&event, self));
+            .retain(&emitter, |handler| handler(event.as_ref(), self));
     }
 
     fn apply_focus_changed_effect(&mut self, window_id: WindowId, focused: Option<FocusId>) {

crates/gpui2/src/app/entity_map.rs 🔗

@@ -100,10 +100,15 @@ impl EntityMap {
     }
 
     pub fn take_dropped(&mut self) -> Vec<(EntityId, AnyBox)> {
-        let dropped_entity_ids = mem::take(&mut self.ref_counts.write().dropped_entity_ids);
+        let mut ref_counts = self.ref_counts.write();
+        let dropped_entity_ids = mem::take(&mut ref_counts.dropped_entity_ids);
+
         dropped_entity_ids
             .into_iter()
-            .map(|entity_id| (entity_id, self.entities.remove(entity_id).unwrap()))
+            .map(|entity_id| {
+                ref_counts.counts.remove(entity_id);
+                (entity_id, self.entities.remove(entity_id).unwrap())
+            })
             .collect()
     }
 }
@@ -212,7 +217,6 @@ impl Drop for AnyHandle {
             if prev_count == 1 {
                 // We were the last reference to this entity, so we can remove it.
                 let mut entity_map = RwLockUpgradableReadGuard::upgrade(entity_map);
-                entity_map.counts.remove(self.entity_id);
                 entity_map.dropped_entity_ids.push(self.entity_id);
             }
         }

crates/gpui2/src/app/model_context.rs 🔗

@@ -79,7 +79,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
         self.app.event_listeners.insert(
             handle.entity_id,
             Box::new(move |event, cx| {
-                let event = event.downcast_ref().expect("invalid event type");
+                let event: &E::Event = event.downcast_ref().expect("invalid event type");
                 if let Some((this, handle)) = this.upgrade().zip(handle.upgrade()) {
                     this.update(cx, |this, cx| on_event(this, handle, event, cx));
                     true

crates/gpui2/src/test.rs 🔗

@@ -8,7 +8,7 @@ use std::{
 pub fn run_test(
     mut num_iterations: u64,
     max_retries: usize,
-    test_fn: &mut (dyn RefUnwindSafe + Fn(TestDispatcher)),
+    test_fn: &mut (dyn RefUnwindSafe + Fn(TestDispatcher, u64)),
     on_fail_fn: Option<fn()>,
     _fn_name: String, // todo!("re-enable fn_name")
 ) {
@@ -28,7 +28,7 @@ pub fn run_test(
             }
             let result = panic::catch_unwind(|| {
                 let dispatcher = TestDispatcher::new(StdRng::seed_from_u64(seed));
-                test_fn(dispatcher);
+                test_fn(dispatcher, seed);
             });
 
             match result {