diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 525f9d6ac0..dfd5594773 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -505,8 +505,8 @@ impl App { self.new_observer( entity_id, Box::new(move |cx| { - if let Some(handle) = Entity::::upgrade_from(&handle) { - on_notify(handle, cx) + if let Some(entity) = handle.upgrade() { + on_notify(entity, cx) } else { false } @@ -550,15 +550,15 @@ impl App { Evt: 'static, { let entity_id = entity.entity_id(); - let entity = entity.downgrade(); + let handle = entity.downgrade(); self.new_subscription( entity_id, ( TypeId::of::(), Box::new(move |event, cx| { let event: &Evt = event.downcast_ref().expect("invalid event type"); - if let Some(handle) = Entity::::upgrade_from(&entity) { - on_event(handle, event, cx) + if let Some(entity) = handle.upgrade() { + on_event(entity, event, cx) } else { false } diff --git a/crates/gpui/src/app/context.rs b/crates/gpui/src/app/context.rs index 89b92231be..fcdc6ca97d 100644 --- a/crates/gpui/src/app/context.rs +++ b/crates/gpui/src/app/context.rs @@ -212,7 +212,7 @@ impl<'a, T: 'static> Context<'a, T> { /// Convenience method for accessing view state in an event callback. /// - /// Many GPUI callbacks take the form of `Fn(&E, &mut Window, &mut AppContext)`, + /// Many GPUI callbacks take the form of `Fn(&E, &mut Window, &mut App)`, /// but it's often useful to be able to access view state in these /// callbacks. This method provides a convenient way to do so. pub fn listener( @@ -608,8 +608,8 @@ impl<'a, T: 'static> Context<'a, T> { } /// Schedule a future to be run asynchronously. - /// The given callback is invoked with a [`WeakEntity`] to avoid leaking the view for a long-running process. - /// It's also given an [`AsyncWindowContext`], which can be used to access the state of the view across await points. + /// The given callback is invoked with a [`WeakEntity`] to avoid leaking the entity for a long-running process. + /// It's also given an [`AsyncWindowContext`], which can be used to access the state of the entity across await points. /// The returned future will be polled on the main thread. #[track_caller] pub fn spawn_in(&self, window: &Window, f: AsyncFn) -> Task diff --git a/crates/gpui/src/app/entity_map.rs b/crates/gpui/src/app/entity_map.rs index ebcf95dbbb..fead30842d 100644 --- a/crates/gpui/src/app/entity_map.rs +++ b/crates/gpui/src/app/entity_map.rs @@ -409,17 +409,6 @@ impl Entity { } } - /// Upgrade the given weak pointer to a retaining pointer, if it still exists - pub fn upgrade_from(weak: &WeakEntity) -> Option - where - Self: Sized, - { - Some(Entity { - any_entity: weak.any_entity.upgrade()?, - entity_type: weak.entity_type, - }) - } - /// Convert this into a dynamically typed entity. pub fn into_any(self) -> AnyEntity { self.any_entity @@ -440,32 +429,22 @@ impl Entity { } /// Updates the entity referenced by this handle with the given function. - /// - /// The update function receives a context appropriate for its environment. - /// When updating in an `App`, it receives a `Context`. - /// When updating in a `Window`, it receives a `Window` and a `Context`. - pub fn update( + pub fn update( &self, cx: &mut C, update: impl FnOnce(&mut T, &mut Context) -> R, - ) -> C::Result - where - C: AppContext, - { + ) -> C::Result { cx.update_entity(self, update) } /// Updates the entity referenced by this handle with the given function if /// the referenced entity still exists, within a visual context that has a window. /// Returns an error if the entity has been released. - pub fn update_in( + pub fn update_in( &self, cx: &mut C, update: impl FnOnce(&mut T, &mut Window, &mut Context) -> R, - ) -> C::Result - where - C: VisualContext, - { + ) -> C::Result { cx.update_window_entity(self, update) } } @@ -669,8 +648,10 @@ impl Clone for WeakEntity { impl WeakEntity { /// Upgrade this weak entity reference into a strong entity reference pub fn upgrade(&self) -> Option> { - // Delegate to the trait implementation to keep behavior in one place. - Entity::upgrade_from(self) + Some(Entity { + any_entity: self.any_entity.upgrade()?, + entity_type: self.entity_type, + }) } /// Updates the entity referenced by this handle with the given function if diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs index bfb0f46161..a100ac21ca 100644 --- a/crates/gpui/src/window.rs +++ b/crates/gpui/src/window.rs @@ -1222,7 +1222,7 @@ impl Window { Evt: 'static, { let entity_id = entity.entity_id(); - let entity = entity.downgrade(); + let handle = entity.downgrade(); let window_handle = self.handle; cx.new_subscription( entity_id, @@ -1231,9 +1231,9 @@ impl Window { Box::new(move |event, cx| { window_handle .update(cx, |_, window, cx| { - if let Some(handle) = Entity::::upgrade_from(&entity) { + if let Some(entity) = handle.upgrade() { let event = event.downcast_ref().expect("invalid event type"); - on_event(handle, event, window, cx); + on_event(entity, event, window, cx); true } else { false