Remove `ReadModelWith` trait

Antonio Scandurra and Nathan Sobo created

Co-Authored-By: Nathan Sobo <nathan@zed.dev>

Change summary

crates/gpui/src/app.rs                  | 28 +-------------------------
crates/gpui/src/app/test_app_context.rs | 26 +++++++++++-------------
2 files changed, 14 insertions(+), 40 deletions(-)

Detailed changes

crates/gpui/src/app.rs 🔗

@@ -126,14 +126,6 @@ pub trait BorrowAppContext {
     fn update<T, F: FnOnce(&mut AppContext) -> T>(&mut self, f: F) -> T;
 }
 
-pub trait ReadModelWith {
-    fn read_model_with<E: Entity, T>(
-        &self,
-        handle: &ModelHandle<E>,
-        read: &mut dyn FnMut(&E, &AppContext) -> T,
-    ) -> T;
-}
-
 pub trait UpdateModel {
     fn update_model<T: Entity, O>(
         &mut self,
@@ -414,18 +406,6 @@ impl UpdateModel for AsyncAppContext {
     }
 }
 
-impl ReadModelWith for AsyncAppContext {
-    fn read_model_with<E: Entity, T>(
-        &self,
-        handle: &ModelHandle<E>,
-        read: &mut dyn FnMut(&E, &AppContext) -> T,
-    ) -> T {
-        let cx = self.0.borrow();
-        let cx = &*cx;
-        read(handle.read(cx), cx)
-    }
-}
-
 impl UpdateView for AsyncAppContext {
     type Output<S> = Result<S>;
 
@@ -3609,14 +3589,10 @@ impl<T: Entity> ModelHandle<T> {
 
     pub fn read_with<C, F, S>(&self, cx: &C, read: F) -> S
     where
-        C: ReadModelWith,
+        C: BorrowAppContext,
         F: FnOnce(&T, &AppContext) -> S,
     {
-        let mut read = Some(read);
-        cx.read_model_with(self, &mut |model, cx| {
-            let read = read.take().unwrap();
-            read(model, cx)
-        })
+        cx.read_with(|cx| read(self.read(cx), cx))
     }
 
     pub fn update<C, F, S>(&self, cx: &mut C, update: F) -> S

crates/gpui/src/app/test_app_context.rs 🔗

@@ -22,8 +22,8 @@ use crate::{
     keymap_matcher::Keystroke,
     platform,
     platform::{Event, InputHandler, KeyDownEvent, Platform},
-    Action, AnyViewHandle, AppContext, Entity, FontCache, Handle, ModelContext, ModelHandle,
-    ReadModelWith, ReadViewWith, Subscription, Task, UpdateModel, UpdateView, View, ViewContext,
+    Action, AnyViewHandle, AppContext, BorrowAppContext, Entity, FontCache, Handle, ModelContext,
+    ModelHandle, ReadViewWith, Subscription, Task, UpdateModel, UpdateView, View, ViewContext,
     ViewHandle, WeakHandle, WindowContext,
 };
 use collections::BTreeMap;
@@ -381,6 +381,16 @@ impl TestAppContext {
     }
 }
 
+impl BorrowAppContext for TestAppContext {
+    fn read_with<T, F: FnOnce(&AppContext) -> T>(&self, f: F) -> T {
+        self.cx.borrow().read_with(f)
+    }
+
+    fn update<T, F: FnOnce(&mut AppContext) -> T>(&mut self, f: F) -> T {
+        self.cx.borrow_mut().update(f)
+    }
+}
+
 impl UpdateModel for TestAppContext {
     fn update_model<T: Entity, O>(
         &mut self,
@@ -391,18 +401,6 @@ impl UpdateModel for TestAppContext {
     }
 }
 
-impl ReadModelWith for TestAppContext {
-    fn read_model_with<E: Entity, T>(
-        &self,
-        handle: &ModelHandle<E>,
-        read: &mut dyn FnMut(&E, &AppContext) -> T,
-    ) -> T {
-        let cx = self.cx.borrow();
-        let cx = &*cx;
-        read(handle.read(cx), cx)
-    }
-}
-
 impl UpdateView for TestAppContext {
     type Output<S> = S;