Remove Reference

This commit is contained in:
Nathan Sobo 2023-10-31 11:29:13 -06:00
parent 8f1000ea10
commit fd15551d97
4 changed files with 19 additions and 55 deletions

View file

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

View file

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

View file

@ -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> { pub(crate) struct MainThreadOnly<T: ?Sized> {
executor: Executor, executor: Executor,
value: Arc<T>, value: Arc<T>,

View file

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