From a9417f3d2e824ae24f001568fa646d27bd2b5fea Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 24 Apr 2023 16:44:54 +0200 Subject: [PATCH] Remove `ReadModelWith` trait Co-Authored-By: Nathan Sobo --- crates/gpui/src/app.rs | 28 ++----------------------- crates/gpui/src/app/test_app_context.rs | 26 +++++++++++------------ 2 files changed, 14 insertions(+), 40 deletions(-) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index ae71042e54..315c3048a3 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -126,14 +126,6 @@ pub trait BorrowAppContext { fn update T>(&mut self, f: F) -> T; } -pub trait ReadModelWith { - fn read_model_with( - &self, - handle: &ModelHandle, - read: &mut dyn FnMut(&E, &AppContext) -> T, - ) -> T; -} - pub trait UpdateModel { fn update_model( &mut self, @@ -414,18 +406,6 @@ impl UpdateModel for AsyncAppContext { } } -impl ReadModelWith for AsyncAppContext { - fn read_model_with( - &self, - handle: &ModelHandle, - 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 = Result; @@ -3609,14 +3589,10 @@ impl ModelHandle { pub fn read_with(&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(&self, cx: &mut C, update: F) -> S diff --git a/crates/gpui/src/app/test_app_context.rs b/crates/gpui/src/app/test_app_context.rs index 5a367b15ba..fdc93319cb 100644 --- a/crates/gpui/src/app/test_app_context.rs +++ b/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>(&self, f: F) -> T { + self.cx.borrow().read_with(f) + } + + fn update T>(&mut self, f: F) -> T { + self.cx.borrow_mut().update(f) + } +} + impl UpdateModel for TestAppContext { fn update_model( &mut self, @@ -391,18 +401,6 @@ impl UpdateModel for TestAppContext { } } -impl ReadModelWith for TestAppContext { - fn read_model_with( - &self, - handle: &ModelHandle, - 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;