From a6115d93302bf1700b091e026493e5453d6ebc92 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 24 Apr 2023 16:52:09 +0200 Subject: [PATCH] Remove `UpdateModel` trait Co-Authored-By: Nathan Sobo --- crates/gpui/src/app.rs | 106 +++++++----------------- crates/gpui/src/app/test_app_context.rs | 14 +--- crates/gpui/src/app/window.rs | 14 +--- 3 files changed, 33 insertions(+), 101 deletions(-) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 315c3048a3efa1a54581e540616aa67ed4a1fddd..34b9b13af9daf8ff26ea09eaf0378aee0cea58d1 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 UpdateModel { - fn update_model( - &mut self, - handle: &ModelHandle, - update: &mut dyn FnMut(&mut T, &mut ModelContext) -> O, - ) -> O; -} - pub trait ReadViewWith { fn read_view_with( &self, @@ -396,16 +388,6 @@ impl BorrowAppContext for AsyncAppContext { } } -impl UpdateModel for AsyncAppContext { - fn update_model( - &mut self, - handle: &ModelHandle, - update: &mut dyn FnMut(&mut E, &mut ModelContext) -> O, - ) -> O { - self.0.borrow_mut().update_model(handle, update) - } -} - impl UpdateView for AsyncAppContext { type Output = Result; @@ -1243,6 +1225,29 @@ impl AppContext { } } + fn update_model( + &mut self, + handle: &ModelHandle, + update: &mut dyn FnMut(&mut T, &mut ModelContext) -> V, + ) -> V { + if let Some(mut model) = self.models.remove(&handle.model_id) { + self.update(|this| { + let mut cx = ModelContext::new(this, handle.model_id); + let result = update( + model + .as_any_mut() + .downcast_mut() + .expect("downcast is type safe"), + &mut cx, + ); + this.models.insert(handle.model_id, model); + result + }) + } else { + panic!("circular model update"); + } + } + fn upgrade_model_handle( &self, handle: &WeakModelHandle, @@ -2102,31 +2107,6 @@ impl BorrowAppContext for AppContext { } } -impl UpdateModel for AppContext { - fn update_model( - &mut self, - handle: &ModelHandle, - update: &mut dyn FnMut(&mut T, &mut ModelContext) -> V, - ) -> V { - if let Some(mut model) = self.models.remove(&handle.model_id) { - self.update(|this| { - let mut cx = ModelContext::new(this, handle.model_id); - let result = update( - model - .as_any_mut() - .downcast_mut() - .expect("downcast is type safe"), - &mut cx, - ); - this.models.insert(handle.model_id, model); - result - }) - } else { - panic!("circular model update"); - } - } -} - #[derive(Debug)] pub enum ParentId { View(usize), @@ -2822,16 +2802,6 @@ impl BorrowAppContext for ModelContext<'_, M> { } } -impl UpdateModel for ModelContext<'_, M> { - fn update_model( - &mut self, - handle: &ModelHandle, - update: &mut dyn FnMut(&mut T, &mut ModelContext) -> V, - ) -> V { - self.app.update_model(handle, update) - } -} - impl Deref for ModelContext<'_, M> { type Target = AppContext; @@ -3387,16 +3357,6 @@ impl BorrowAppContext for ViewContext<'_, '_, V> { } } -impl UpdateModel for ViewContext<'_, '_, V> { - fn update_model( - &mut self, - handle: &ModelHandle, - update: &mut dyn FnMut(&mut T, &mut ModelContext) -> O, - ) -> O { - self.window_context.update_model(handle, update) - } -} - impl UpdateView for ViewContext<'_, '_, V> { type Output = S; @@ -3454,16 +3414,6 @@ impl BorrowAppContext for EventContext<'_, '_, '_, V> { } } -impl UpdateModel for EventContext<'_, '_, '_, V> { - fn update_model( - &mut self, - handle: &ModelHandle, - update: &mut dyn FnMut(&mut T, &mut ModelContext) -> O, - ) -> O { - self.view_context.update_model(handle, update) - } -} - impl UpdateView for EventContext<'_, '_, '_, V> { type Output = S; @@ -3597,13 +3547,15 @@ impl ModelHandle { pub fn update(&self, cx: &mut C, update: F) -> S where - C: UpdateModel, + C: BorrowAppContext, F: FnOnce(&mut T, &mut ModelContext) -> S, { let mut update = Some(update); - cx.update_model(self, &mut |model, cx| { - let update = update.take().unwrap(); - update(model, cx) + cx.update(|cx| { + cx.update_model(self, &mut |model, cx| { + let update = update.take().unwrap(); + update(model, cx) + }) }) } } diff --git a/crates/gpui/src/app/test_app_context.rs b/crates/gpui/src/app/test_app_context.rs index fdc93319cbedc39607ae05150f43e8c46b9c8ef1..1f18d28164aeb471cfcd8f36e3f095077df4d5d6 100644 --- a/crates/gpui/src/app/test_app_context.rs +++ b/crates/gpui/src/app/test_app_context.rs @@ -23,8 +23,8 @@ use crate::{ platform, platform::{Event, InputHandler, KeyDownEvent, Platform}, Action, AnyViewHandle, AppContext, BorrowAppContext, Entity, FontCache, Handle, ModelContext, - ModelHandle, ReadViewWith, Subscription, Task, UpdateModel, UpdateView, View, ViewContext, - ViewHandle, WeakHandle, WindowContext, + ModelHandle, ReadViewWith, Subscription, Task, UpdateView, View, ViewContext, ViewHandle, + WeakHandle, WindowContext, }; use collections::BTreeMap; @@ -391,16 +391,6 @@ impl BorrowAppContext for TestAppContext { } } -impl UpdateModel for TestAppContext { - fn update_model( - &mut self, - handle: &ModelHandle, - update: &mut dyn FnMut(&mut T, &mut ModelContext) -> O, - ) -> O { - self.cx.borrow_mut().update_model(handle, update) - } -} - impl UpdateView for TestAppContext { type Output = S; diff --git a/crates/gpui/src/app/window.rs b/crates/gpui/src/app/window.rs index a8af427f7e6ddce34836a306445c807f086fe9a4..c2ce119d8892b843031dd36e5586a581e8249b08 100644 --- a/crates/gpui/src/app/window.rs +++ b/crates/gpui/src/app/window.rs @@ -14,8 +14,8 @@ use crate::{ text_layout::TextLayoutCache, util::post_inc, Action, AnyView, AnyViewHandle, AppContext, BorrowAppContext, Effect, Element, Entity, Handle, - ModelContext, ModelHandle, MouseRegion, MouseRegionId, ParentId, SceneBuilder, Subscription, - UpdateModel, UpdateView, View, ViewContext, ViewHandle, WindowInvalidation, + MouseRegion, MouseRegionId, ParentId, SceneBuilder, Subscription, UpdateView, View, + ViewContext, ViewHandle, WindowInvalidation, }; use anyhow::{anyhow, bail, Result}; use collections::{HashMap, HashSet}; @@ -141,16 +141,6 @@ impl BorrowAppContext for WindowContext<'_> { } } -impl UpdateModel for WindowContext<'_> { - fn update_model( - &mut self, - handle: &ModelHandle, - update: &mut dyn FnMut(&mut T, &mut ModelContext) -> R, - ) -> R { - self.app_context.update_model(handle, update) - } -} - impl UpdateView for WindowContext<'_> { type Output = S;