Remove UpgradeModelHandle trait

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2023-04-24 16:40:30 +02:00
parent b8fab6fde9
commit 5f500d34b2
3 changed files with 60 additions and 144 deletions

View File

@ -142,23 +142,6 @@ pub trait UpdateModel {
) -> O; ) -> O;
} }
pub trait UpgradeModelHandle {
fn upgrade_model_handle<T: Entity>(
&self,
handle: &WeakModelHandle<T>,
) -> Option<ModelHandle<T>>;
fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool;
fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle>;
}
// pub trait UpgradeViewHandle {
// fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>>;
// fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option<AnyViewHandle>;
// }
pub trait ReadViewWith { pub trait ReadViewWith {
fn read_view_with<V, T>( fn read_view_with<V, T>(
&self, &self,
@ -431,23 +414,6 @@ impl UpdateModel for AsyncAppContext {
} }
} }
impl UpgradeModelHandle for AsyncAppContext {
fn upgrade_model_handle<T: Entity>(
&self,
handle: &WeakModelHandle<T>,
) -> Option<ModelHandle<T>> {
self.0.borrow().upgrade_model_handle(handle)
}
fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
self.0.borrow().model_handle_is_upgradable(handle)
}
fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
self.0.borrow().upgrade_any_model_handle(handle)
}
}
impl ReadModelWith for AsyncAppContext { impl ReadModelWith for AsyncAppContext {
fn read_model_with<E: Entity, T>( fn read_model_with<E: Entity, T>(
&self, &self,
@ -1297,6 +1263,33 @@ impl AppContext {
} }
} }
fn upgrade_model_handle<T: Entity>(
&self,
handle: &WeakModelHandle<T>,
) -> Option<ModelHandle<T>> {
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<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
self.ref_counts.lock().is_entity_alive(handle.model_id)
}
fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
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<V, F>( pub fn add_window<V, F>(
&mut self, &mut self,
window_options: WindowOptions, window_options: WindowOptions,
@ -2154,35 +2147,6 @@ impl UpdateModel for AppContext {
} }
} }
impl UpgradeModelHandle for AppContext {
fn upgrade_model_handle<T: Entity>(
&self,
handle: &WeakModelHandle<T>,
) -> Option<ModelHandle<T>> {
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<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
self.ref_counts.lock().is_entity_alive(handle.model_id)
}
fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
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)] #[derive(Debug)]
pub enum ParentId { pub enum ParentId {
View(usize), View(usize),
@ -2868,6 +2832,16 @@ impl<M> AsMut<AppContext> for ModelContext<'_, M> {
} }
} }
impl<M> BorrowAppContext for ModelContext<'_, M> {
fn read_with<T, F: FnOnce(&AppContext) -> T>(&self, f: F) -> T {
self.app.read_with(f)
}
fn update<T, F: FnOnce(&mut AppContext) -> T>(&mut self, f: F) -> T {
self.app.update(f)
}
}
impl<M> UpdateModel for ModelContext<'_, M> { impl<M> UpdateModel for ModelContext<'_, M> {
fn update_model<T: Entity, V>( fn update_model<T: Entity, V>(
&mut self, &mut self,
@ -2878,23 +2852,6 @@ impl<M> UpdateModel for ModelContext<'_, M> {
} }
} }
impl<M> UpgradeModelHandle for ModelContext<'_, M> {
fn upgrade_model_handle<T: Entity>(
&self,
handle: &WeakModelHandle<T>,
) -> Option<ModelHandle<T>> {
self.app.upgrade_model_handle(handle)
}
fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
self.app.model_handle_is_upgradable(handle)
}
fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
self.app.upgrade_any_model_handle(handle)
}
}
impl<M> Deref for ModelContext<'_, M> { impl<M> Deref for ModelContext<'_, M> {
type Target = AppContext; type Target = AppContext;
@ -3450,23 +3407,6 @@ impl<V> BorrowAppContext for ViewContext<'_, '_, V> {
} }
} }
impl<V> UpgradeModelHandle for ViewContext<'_, '_, V> {
fn upgrade_model_handle<T: Entity>(
&self,
handle: &WeakModelHandle<T>,
) -> Option<ModelHandle<T>> {
self.window_context.upgrade_model_handle(handle)
}
fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
self.window_context.model_handle_is_upgradable(handle)
}
fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
self.window_context.upgrade_any_model_handle(handle)
}
}
impl<V: View> UpdateModel for ViewContext<'_, '_, V> { impl<V: View> UpdateModel for ViewContext<'_, '_, V> {
fn update_model<T: Entity, O>( fn update_model<T: Entity, O>(
&mut self, &mut self,
@ -3524,6 +3464,16 @@ impl<V: View> DerefMut for EventContext<'_, '_, '_, V> {
} }
} }
impl<V: View> BorrowAppContext for EventContext<'_, '_, '_, V> {
fn read_with<T, F: FnOnce(&AppContext) -> T>(&self, f: F) -> T {
self.view_context.read_with(f)
}
fn update<T, F: FnOnce(&mut AppContext) -> T>(&mut self, f: F) -> T {
self.view_context.update(f)
}
}
impl<V: View> UpdateModel for EventContext<'_, '_, '_, V> { impl<V: View> UpdateModel for EventContext<'_, '_, '_, V> {
fn update_model<T: Entity, O>( fn update_model<T: Entity, O>(
&mut self, &mut self,
@ -3549,23 +3499,6 @@ impl<V: View> UpdateView for EventContext<'_, '_, '_, V> {
} }
} }
impl<V: View> UpgradeModelHandle for EventContext<'_, '_, '_, V> {
fn upgrade_model_handle<T: Entity>(
&self,
handle: &WeakModelHandle<T>,
) -> Option<ModelHandle<T>> {
self.view_context.upgrade_model_handle(handle)
}
fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
self.view_context.model_handle_is_upgradable(handle)
}
fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
self.view_context.upgrade_any_model_handle(handle)
}
}
pub(crate) enum Reference<'a, T> { pub(crate) enum Reference<'a, T> {
Immutable(&'a T), Immutable(&'a T),
Mutable(&'a mut T), Mutable(&'a mut T),
@ -3808,12 +3741,12 @@ impl<T: Entity> WeakModelHandle<T> {
self.model_id self.model_id
} }
pub fn is_upgradable(&self, cx: &impl UpgradeModelHandle) -> bool { pub fn is_upgradable(&self, cx: &impl BorrowAppContext) -> bool {
cx.model_handle_is_upgradable(self) cx.read_with(|cx| cx.model_handle_is_upgradable(self))
} }
pub fn upgrade(&self, cx: &impl UpgradeModelHandle) -> Option<ModelHandle<T>> { pub fn upgrade(&self, cx: &impl BorrowAppContext) -> Option<ModelHandle<T>> {
cx.upgrade_model_handle(self) cx.read_with(|cx| cx.upgrade_model_handle(self))
} }
} }
@ -4189,9 +4122,10 @@ pub struct AnyWeakModelHandle {
} }
impl AnyWeakModelHandle { impl AnyWeakModelHandle {
pub fn upgrade(&self, cx: &impl UpgradeModelHandle) -> Option<AnyModelHandle> { pub fn upgrade(&self, cx: &impl BorrowAppContext) -> Option<AnyModelHandle> {
cx.upgrade_any_model_handle(self) cx.read_with(|cx| cx.upgrade_any_model_handle(self))
} }
pub fn model_type(&self) -> TypeId { pub fn model_type(&self) -> TypeId {
self.model_type self.model_type
} }

View File

@ -13,10 +13,9 @@ use crate::{
}, },
text_layout::TextLayoutCache, text_layout::TextLayoutCache,
util::post_inc, util::post_inc,
Action, AnyModelHandle, AnyView, AnyViewHandle, AnyWeakModelHandle, AppContext, Action, AnyView, AnyViewHandle, AppContext, BorrowAppContext, Effect, Element, Entity, Handle,
BorrowAppContext, Effect, Element, Entity, Handle, ModelContext, ModelHandle, MouseRegion, ModelContext, ModelHandle, MouseRegion, MouseRegionId, ParentId, SceneBuilder, Subscription,
MouseRegionId, ParentId, SceneBuilder, Subscription, UpdateModel, UpdateView, UpdateModel, UpdateView, View, ViewContext, ViewHandle, WindowInvalidation,
UpgradeModelHandle, View, ViewContext, ViewHandle, WeakModelHandle, WindowInvalidation,
}; };
use anyhow::{anyhow, bail, Result}; use anyhow::{anyhow, bail, Result};
use collections::{HashMap, HashSet}; use collections::{HashMap, HashSet};
@ -176,23 +175,6 @@ impl UpdateView for WindowContext<'_> {
} }
} }
impl UpgradeModelHandle for WindowContext<'_> {
fn upgrade_model_handle<T: Entity>(
&self,
handle: &WeakModelHandle<T>,
) -> Option<ModelHandle<T>> {
self.app_context.upgrade_model_handle(handle)
}
fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
self.app_context.model_handle_is_upgradable(handle)
}
fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
self.app_context.upgrade_any_model_handle(handle)
}
}
impl<'a> WindowContext<'a> { impl<'a> WindowContext<'a> {
pub fn mutable( pub fn mutable(
app_context: &'a mut AppContext, app_context: &'a mut AppContext,

View File

@ -19,8 +19,8 @@ use futures::{
AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt, AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt,
}; };
use gpui::{ use gpui::{
AnyModelHandle, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, Task, AnyModelHandle, AppContext, AsyncAppContext, BorrowAppContext, Entity, ModelContext,
UpgradeModelHandle, WeakModelHandle, ModelHandle, Task, WeakModelHandle,
}; };
use language::{ use language::{
point_to_lsp, point_to_lsp,
@ -6356,7 +6356,7 @@ impl WorktreeHandle {
} }
impl OpenBuffer { impl OpenBuffer {
pub fn upgrade(&self, cx: &impl UpgradeModelHandle) -> Option<ModelHandle<Buffer>> { pub fn upgrade(&self, cx: &impl BorrowAppContext) -> Option<ModelHandle<Buffer>> {
match self { match self {
OpenBuffer::Strong(handle) => Some(handle.clone()), OpenBuffer::Strong(handle) => Some(handle.clone()),
OpenBuffer::Weak(handle) => handle.upgrade(cx), OpenBuffer::Weak(handle) => handle.upgrade(cx),