From 5f500d34b28fa0ff9dd7cb8a09a9d3f30a4d91f6 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 24 Apr 2023 16:40:30 +0200 Subject: [PATCH] Remove `UpgradeModelHandle` trait Co-Authored-By: Nathan Sobo --- crates/gpui/src/app.rs | 174 +++++++++++----------------------- crates/gpui/src/app/window.rs | 24 +---- crates/project/src/project.rs | 6 +- 3 files changed, 60 insertions(+), 144 deletions(-) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 26303e6d79..ae71042e54 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -142,23 +142,6 @@ pub trait UpdateModel { ) -> O; } -pub trait UpgradeModelHandle { - fn upgrade_model_handle( - &self, - handle: &WeakModelHandle, - ) -> Option>; - - fn model_handle_is_upgradable(&self, handle: &WeakModelHandle) -> bool; - - fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option; -} - -// pub trait UpgradeViewHandle { -// fn upgrade_view_handle(&self, handle: &WeakViewHandle) -> Option>; - -// fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option; -// } - pub trait ReadViewWith { fn read_view_with( &self, @@ -431,23 +414,6 @@ impl UpdateModel for AsyncAppContext { } } -impl UpgradeModelHandle for AsyncAppContext { - fn upgrade_model_handle( - &self, - handle: &WeakModelHandle, - ) -> Option> { - self.0.borrow().upgrade_model_handle(handle) - } - - fn model_handle_is_upgradable(&self, handle: &WeakModelHandle) -> bool { - self.0.borrow().model_handle_is_upgradable(handle) - } - - fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option { - self.0.borrow().upgrade_any_model_handle(handle) - } -} - impl ReadModelWith for AsyncAppContext { fn read_model_with( &self, @@ -1297,6 +1263,33 @@ impl AppContext { } } + fn upgrade_model_handle( + &self, + handle: &WeakModelHandle, + ) -> Option> { + if self.ref_counts.lock().is_entity_alive(handle.model_id) { + Some(ModelHandle::new(handle.model_id, &self.ref_counts)) + } else { + None + } + } + + fn model_handle_is_upgradable(&self, handle: &WeakModelHandle) -> bool { + self.ref_counts.lock().is_entity_alive(handle.model_id) + } + + fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option { + if self.ref_counts.lock().is_entity_alive(handle.model_id) { + Some(AnyModelHandle::new( + handle.model_id, + handle.model_type, + self.ref_counts.clone(), + )) + } else { + None + } + } + pub fn add_window( &mut self, window_options: WindowOptions, @@ -2154,35 +2147,6 @@ impl UpdateModel for AppContext { } } -impl UpgradeModelHandle for AppContext { - fn upgrade_model_handle( - &self, - handle: &WeakModelHandle, - ) -> Option> { - if self.ref_counts.lock().is_entity_alive(handle.model_id) { - Some(ModelHandle::new(handle.model_id, &self.ref_counts)) - } else { - None - } - } - - fn model_handle_is_upgradable(&self, handle: &WeakModelHandle) -> bool { - self.ref_counts.lock().is_entity_alive(handle.model_id) - } - - fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option { - if self.ref_counts.lock().is_entity_alive(handle.model_id) { - Some(AnyModelHandle::new( - handle.model_id, - handle.model_type, - self.ref_counts.clone(), - )) - } else { - None - } - } -} - #[derive(Debug)] pub enum ParentId { View(usize), @@ -2868,6 +2832,16 @@ impl AsMut for ModelContext<'_, M> { } } +impl BorrowAppContext for ModelContext<'_, M> { + fn read_with T>(&self, f: F) -> T { + self.app.read_with(f) + } + + fn update T>(&mut self, f: F) -> T { + self.app.update(f) + } +} + impl UpdateModel for ModelContext<'_, M> { fn update_model( &mut self, @@ -2878,23 +2852,6 @@ impl UpdateModel for ModelContext<'_, M> { } } -impl UpgradeModelHandle for ModelContext<'_, M> { - fn upgrade_model_handle( - &self, - handle: &WeakModelHandle, - ) -> Option> { - self.app.upgrade_model_handle(handle) - } - - fn model_handle_is_upgradable(&self, handle: &WeakModelHandle) -> bool { - self.app.model_handle_is_upgradable(handle) - } - - fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option { - self.app.upgrade_any_model_handle(handle) - } -} - impl Deref for ModelContext<'_, M> { type Target = AppContext; @@ -3450,23 +3407,6 @@ impl BorrowAppContext for ViewContext<'_, '_, V> { } } -impl UpgradeModelHandle for ViewContext<'_, '_, V> { - fn upgrade_model_handle( - &self, - handle: &WeakModelHandle, - ) -> Option> { - self.window_context.upgrade_model_handle(handle) - } - - fn model_handle_is_upgradable(&self, handle: &WeakModelHandle) -> bool { - self.window_context.model_handle_is_upgradable(handle) - } - - fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option { - self.window_context.upgrade_any_model_handle(handle) - } -} - impl UpdateModel for ViewContext<'_, '_, V> { fn update_model( &mut self, @@ -3524,6 +3464,16 @@ impl DerefMut for EventContext<'_, '_, '_, V> { } } +impl BorrowAppContext for EventContext<'_, '_, '_, V> { + fn read_with T>(&self, f: F) -> T { + self.view_context.read_with(f) + } + + fn update T>(&mut self, f: F) -> T { + self.view_context.update(f) + } +} + impl UpdateModel for EventContext<'_, '_, '_, V> { fn update_model( &mut self, @@ -3549,23 +3499,6 @@ impl UpdateView for EventContext<'_, '_, '_, V> { } } -impl UpgradeModelHandle for EventContext<'_, '_, '_, V> { - fn upgrade_model_handle( - &self, - handle: &WeakModelHandle, - ) -> Option> { - self.view_context.upgrade_model_handle(handle) - } - - fn model_handle_is_upgradable(&self, handle: &WeakModelHandle) -> bool { - self.view_context.model_handle_is_upgradable(handle) - } - - fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option { - self.view_context.upgrade_any_model_handle(handle) - } -} - pub(crate) enum Reference<'a, T> { Immutable(&'a T), Mutable(&'a mut T), @@ -3808,12 +3741,12 @@ impl WeakModelHandle { self.model_id } - pub fn is_upgradable(&self, cx: &impl UpgradeModelHandle) -> bool { - cx.model_handle_is_upgradable(self) + pub fn is_upgradable(&self, cx: &impl BorrowAppContext) -> bool { + cx.read_with(|cx| cx.model_handle_is_upgradable(self)) } - pub fn upgrade(&self, cx: &impl UpgradeModelHandle) -> Option> { - cx.upgrade_model_handle(self) + pub fn upgrade(&self, cx: &impl BorrowAppContext) -> Option> { + cx.read_with(|cx| cx.upgrade_model_handle(self)) } } @@ -4189,9 +4122,10 @@ pub struct AnyWeakModelHandle { } impl AnyWeakModelHandle { - pub fn upgrade(&self, cx: &impl UpgradeModelHandle) -> Option { - cx.upgrade_any_model_handle(self) + pub fn upgrade(&self, cx: &impl BorrowAppContext) -> Option { + cx.read_with(|cx| cx.upgrade_any_model_handle(self)) } + pub fn model_type(&self) -> TypeId { self.model_type } diff --git a/crates/gpui/src/app/window.rs b/crates/gpui/src/app/window.rs index 605a46bf9e..a8af427f7e 100644 --- a/crates/gpui/src/app/window.rs +++ b/crates/gpui/src/app/window.rs @@ -13,10 +13,9 @@ use crate::{ }, text_layout::TextLayoutCache, util::post_inc, - Action, AnyModelHandle, AnyView, AnyViewHandle, AnyWeakModelHandle, AppContext, - BorrowAppContext, Effect, Element, Entity, Handle, ModelContext, ModelHandle, MouseRegion, - MouseRegionId, ParentId, SceneBuilder, Subscription, UpdateModel, UpdateView, - UpgradeModelHandle, View, ViewContext, ViewHandle, WeakModelHandle, WindowInvalidation, + Action, AnyView, AnyViewHandle, AppContext, BorrowAppContext, Effect, Element, Entity, Handle, + ModelContext, ModelHandle, MouseRegion, MouseRegionId, ParentId, SceneBuilder, Subscription, + UpdateModel, UpdateView, View, ViewContext, ViewHandle, WindowInvalidation, }; use anyhow::{anyhow, bail, Result}; use collections::{HashMap, HashSet}; @@ -176,23 +175,6 @@ impl UpdateView for WindowContext<'_> { } } -impl UpgradeModelHandle for WindowContext<'_> { - fn upgrade_model_handle( - &self, - handle: &WeakModelHandle, - ) -> Option> { - self.app_context.upgrade_model_handle(handle) - } - - fn model_handle_is_upgradable(&self, handle: &WeakModelHandle) -> bool { - self.app_context.model_handle_is_upgradable(handle) - } - - fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option { - self.app_context.upgrade_any_model_handle(handle) - } -} - impl<'a> WindowContext<'a> { pub fn mutable( app_context: &'a mut AppContext, diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index afd0b3bbae..e5fc1e1b3b 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -19,8 +19,8 @@ use futures::{ AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt, }; use gpui::{ - AnyModelHandle, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, Task, - UpgradeModelHandle, WeakModelHandle, + AnyModelHandle, AppContext, AsyncAppContext, BorrowAppContext, Entity, ModelContext, + ModelHandle, Task, WeakModelHandle, }; use language::{ point_to_lsp, @@ -6356,7 +6356,7 @@ impl WorktreeHandle { } impl OpenBuffer { - pub fn upgrade(&self, cx: &impl UpgradeModelHandle) -> Option> { + pub fn upgrade(&self, cx: &impl BorrowAppContext) -> Option> { match self { OpenBuffer::Strong(handle) => Some(handle.clone()), OpenBuffer::Weak(handle) => handle.upgrade(cx),