Remove Reference

Nathan Sobo created

Change summary

crates/gpui2/src/app.rs               | 11 ++++-------
crates/gpui2/src/app/model_context.rs | 11 ++++-------
crates/gpui2/src/gpui2.rs             | 27 ---------------------------
crates/gpui2/src/window.rs            | 25 +++++++++++--------------
4 files changed, 19 insertions(+), 55 deletions(-)

Detailed changes

crates/gpui2/src/app.rs 🔗

@@ -280,7 +280,7 @@ impl AppContext {
                 .take()
                 .unwrap();
 
-            let result = update(&mut WindowContext::mutable(cx, &mut window));
+            let result = update(&mut WindowContext::new(cx, &mut window));
 
             cx.windows
                 .get_mut(handle.id)
@@ -765,7 +765,7 @@ impl Context for AppContext {
     ) -> Model<T> {
         self.update(|cx| {
             let slot = cx.entities.reserve();
-            let entity = build_model(&mut ModelContext::mutable(cx, slot.downgrade()));
+            let entity = build_model(&mut ModelContext::new(cx, slot.downgrade()));
             cx.entities.insert(slot, entity)
         })
     }
@@ -779,10 +779,7 @@ impl Context for AppContext {
     ) -> R {
         self.update(|cx| {
             let mut entity = cx.entities.lease(model);
-            let result = update(
-                &mut entity,
-                &mut ModelContext::mutable(cx, model.downgrade()),
-            );
+            let result = update(&mut entity, &mut ModelContext::new(cx, model.downgrade()));
             cx.entities.end_lease(entity);
             result
         })
@@ -898,7 +895,7 @@ impl MainThread<AppContext> {
             let id = cx.windows.insert(None);
             let handle = WindowHandle::new(id);
             let mut window = Window::new(handle.into(), options, cx);
-            let root_view = build_root_view(&mut WindowContext::mutable(cx, &mut window));
+            let root_view = build_root_view(&mut WindowContext::new(cx, &mut window));
             window.root_view.replace(root_view.into());
             cx.windows.get_mut(id).unwrap().replace(window);
             handle

crates/gpui2/src/app/model_context.rs 🔗

@@ -1,6 +1,6 @@
 use crate::{
     AppContext, AsyncAppContext, Context, Effect, Entity, EntityId, EventEmitter, MainThread,
-    Model, Reference, Subscription, Task, WeakModel,
+    Model, Subscription, Task, WeakModel,
 };
 use derive_more::{Deref, DerefMut};
 use futures::FutureExt;
@@ -14,16 +14,13 @@ use std::{
 pub struct ModelContext<'a, T> {
     #[deref]
     #[deref_mut]
-    app: Reference<'a, AppContext>,
+    app: &'a mut AppContext,
     model_state: WeakModel<T>,
 }
 
 impl<'a, T: 'static> ModelContext<'a, T> {
-    pub(crate) fn mutable(app: &'a mut AppContext, model_state: WeakModel<T>) -> Self {
-        Self {
-            app: Reference::Mutable(app),
-            model_state,
-        }
+    pub(crate) fn new(app: &'a mut AppContext, model_state: WeakModel<T>) -> Self {
+        Self { app, model_state }
     }
 
     pub fn entity_id(&self) -> EntityId {

crates/gpui2/src/gpui2.rs 🔗

@@ -307,33 +307,6 @@ impl<T: Into<ArcCow<'static, str>>> From<T> for SharedString {
     }
 }
 
-pub enum Reference<'a, T> {
-    Immutable(&'a T),
-    Mutable(&'a mut T),
-}
-
-impl<'a, T> Deref for Reference<'a, T> {
-    type Target = T;
-
-    fn deref(&self) -> &Self::Target {
-        match self {
-            Reference::Immutable(target) => target,
-            Reference::Mutable(target) => target,
-        }
-    }
-}
-
-impl<'a, T> DerefMut for Reference<'a, T> {
-    fn deref_mut(&mut self) -> &mut Self::Target {
-        match self {
-            Reference::Immutable(_) => {
-                panic!("cannot mutably deref an immutable reference. this is a bug in GPUI.");
-            }
-            Reference::Mutable(target) => target,
-        }
-    }
-}
-
 pub(crate) struct MainThreadOnly<T: ?Sized> {
     executor: Executor,
     value: Arc<T>,

crates/gpui2/src/window.rs 🔗

@@ -5,10 +5,10 @@ use crate::{
     Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch, KeyMatcher, Keystroke, LayoutId,
     MainThread, MainThreadOnly, Model, ModelContext, Modifiers, MonochromeSprite, MouseButton,
     MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformWindow,
-    Point, PolychromeSprite, Quad, Reference, RenderGlyphParams, RenderImageParams,
-    RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size, Style, Subscription,
-    TaffyLayoutEngine, Task, Underline, UnderlineStyle, View, VisualContext, WeakView,
-    WindowOptions, SUBPIXEL_VARIANTS,
+    Point, PolychromeSprite, Quad, RenderGlyphParams, RenderImageParams, RenderSvgParams,
+    ScaledPixels, SceneBuilder, Shadow, SharedString, Size, Style, Subscription, TaffyLayoutEngine,
+    Task, Underline, UnderlineStyle, View, VisualContext, WeakView, WindowOptions,
+    SUBPIXEL_VARIANTS,
 };
 use anyhow::Result;
 use collections::HashMap;
@@ -306,16 +306,13 @@ impl ContentMask<Pixels> {
 /// to an `AppContext`, so you can also pass a `WindowContext` to any method that takes
 /// an `AppContext` and call any `AppContext` methods.
 pub struct WindowContext<'a> {
-    pub(crate) app: Reference<'a, AppContext>,
-    pub(crate) window: Reference<'a, Window>,
+    pub(crate) app: &'a mut AppContext,
+    pub(crate) window: &'a mut Window,
 }
 
 impl<'a> WindowContext<'a> {
-    pub(crate) fn mutable(app: &'a mut AppContext, window: &'a mut Window) -> Self {
-        Self {
-            app: Reference::Mutable(app),
-            window: Reference::Mutable(window),
-        }
+    pub(crate) fn new(app: &'a mut AppContext, window: &'a mut Window) -> Self {
+        Self { app, window }
     }
 
     /// Obtain a handle to the window that belongs to this context.
@@ -1278,7 +1275,7 @@ impl Context for WindowContext<'_> {
         T: 'static + Send,
     {
         let slot = self.app.entities.reserve();
-        let model = build_model(&mut ModelContext::mutable(&mut *self.app, slot.downgrade()));
+        let model = build_model(&mut ModelContext::new(&mut *self.app, slot.downgrade()));
         self.entities.insert(slot, model)
     }
 
@@ -1290,7 +1287,7 @@ impl Context for WindowContext<'_> {
         let mut entity = self.entities.lease(model);
         let result = update(
             &mut *entity,
-            &mut ModelContext::mutable(&mut *self.app, model.downgrade()),
+            &mut ModelContext::new(&mut *self.app, model.downgrade()),
         );
         self.entities.end_lease(entity);
         result
@@ -1565,7 +1562,7 @@ impl<V> BorrowMut<Window> for ViewContext<'_, V> {
 impl<'a, V: 'static> ViewContext<'a, V> {
     pub(crate) fn new(app: &'a mut AppContext, window: &'a mut Window, view: &'a View<V>) -> Self {
         Self {
-            window_cx: WindowContext::mutable(app, window),
+            window_cx: WindowContext::new(app, window),
             view,
         }
     }