From 963f179d7f0fa9ec94b44f5ba17bab9a4c10912b Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Fri, 29 Sep 2023 19:41:31 -0400 Subject: [PATCH] Checkpoint: Cast through `std::mem::transmute` --- crates/gpui3/src/app.rs | 25 ++++++++++++++++--- crates/gpui3/src/app/model_context.rs | 6 ++--- crates/gpui3/src/element.rs | 28 ++++++++------------- crates/gpui3/src/elements/div.rs | 22 +++++++--------- crates/gpui3/src/style.rs | 14 +++-------- crates/gpui3/src/window.rs | 36 ++++++++++----------------- 6 files changed, 59 insertions(+), 72 deletions(-) diff --git a/crates/gpui3/src/app.rs b/crates/gpui3/src/app.rs index dbc68f7760..5d33f57247 100644 --- a/crates/gpui3/src/app.rs +++ b/crates/gpui3/src/app.rs @@ -112,6 +112,23 @@ impl DerefMut for AppContext { } impl AppContext { + // TODO: Better names for these? + #[inline] + pub fn downcast(&self) -> &AppContext<()> { + // Any `Thread` can become `()`. + // + // Can't do this in a blanket `Deref` impl, as it infinitely recurses. + unsafe { std::mem::transmute::<&AppContext, &AppContext<()>>(self) } + } + + #[inline] + pub fn downcast_mut(&mut self) -> &mut AppContext<()> { + // Any `Thread` can become `()`. + // + // Can't do this in a blanket `DerefMut` impl, as it infinitely recurses. + unsafe { std::mem::transmute::<&mut AppContext, &mut AppContext<()>>(self) } + } + pub fn text_system(&self) -> &Arc { &self.text_system } @@ -206,7 +223,7 @@ impl AppContext { pub(crate) fn update_window( &mut self, id: WindowId, - update: impl FnOnce(&mut WindowContext) -> R, + update: impl FnOnce(&mut WindowContext) -> R, ) -> Result { self.update(|cx| { let mut window = cx @@ -216,7 +233,7 @@ impl AppContext { .take() .unwrap(); - let result = update(&mut WindowContext::mutable(cx, &mut window)); + let result = update(&mut WindowContext::mutable(cx.downcast_mut(), &mut window)); window.dirty = true; cx.windows @@ -276,8 +293,8 @@ impl AppContext { } } -impl Context for AppContext { - type EntityContext<'a, 'w, T: Send + Sync + 'static> = ModelContext<'a, T, Thread>; +impl Context for AppContext { + type EntityContext<'a, 'w, T: Send + Sync + 'static> = ModelContext<'a, T>; type Result = T; fn entity( diff --git a/crates/gpui3/src/app/model_context.rs b/crates/gpui3/src/app/model_context.rs index 95e7866493..67b90db971 100644 --- a/crates/gpui3/src/app/model_context.rs +++ b/crates/gpui3/src/app/model_context.rs @@ -7,8 +7,8 @@ pub struct ModelContext<'a, T, Thread = ()> { entity_id: EntityId, } -impl<'a, T: Send + Sync + 'static, Thread> ModelContext<'a, T, Thread> { - pub(crate) fn mutable(app: &'a mut AppContext, entity_id: EntityId) -> Self { +impl<'a, T: Send + Sync + 'static> ModelContext<'a, T> { + pub(crate) fn mutable(app: &'a mut AppContext, entity_id: EntityId) -> Self { Self { app: Reference::Mutable(app), entity_type: PhantomData, @@ -41,7 +41,7 @@ impl<'a, T: Send + Sync + 'static, Thread> ModelContext<'a, T, Thread> { pub fn observe( &mut self, handle: &Handle, - on_notify: impl Fn(&mut T, Handle, &mut ModelContext<'_, T, Thread>) + Send + Sync + 'static, + on_notify: impl Fn(&mut T, Handle, &mut ModelContext<'_, T>) + Send + Sync + 'static, ) { let this = self.handle(); let handle = handle.downgrade(); diff --git a/crates/gpui3/src/element.rs b/crates/gpui3/src/element.rs index 0acfa07ce8..c145a9725b 100644 --- a/crates/gpui3/src/element.rs +++ b/crates/gpui3/src/element.rs @@ -5,18 +5,18 @@ pub trait Element: 'static { type State; type FrameState; - fn layout( + fn layout( &mut self, state: &mut Self::State, - cx: &mut ViewContext, + cx: &mut ViewContext, ) -> Result<(LayoutId, Self::FrameState)>; - fn paint( + fn paint( &mut self, layout: Layout, state: &mut Self::State, frame_state: &mut Self::FrameState, - cx: &mut ViewContext, + cx: &mut ViewContext, ) -> Result<()>; } @@ -42,16 +42,12 @@ pub trait ParentElement { } trait ElementObject { - fn layout( - &mut self, - state: &mut S, - cx: &mut ViewContext, - ) -> Result; - fn paint( + fn layout(&mut self, state: &mut S, cx: &mut ViewContext) -> Result; + fn paint( &mut self, state: &mut S, offset: Option>, - cx: &mut ViewContext, + cx: &mut ViewContext, ) -> Result<()>; } @@ -139,19 +135,15 @@ impl ElementObject for RenderedElement { pub struct AnyElement(Box>); impl AnyElement { - pub fn layout( - &mut self, - state: &mut S, - cx: &mut ViewContext, - ) -> Result { + pub fn layout(&mut self, state: &mut S, cx: &mut ViewContext) -> Result { self.0.layout(state, cx) } - pub fn paint( + pub fn paint( &mut self, state: &mut S, offset: Option>, - cx: &mut ViewContext, + cx: &mut ViewContext, ) -> Result<()> { self.0.paint(state, offset, cx) } diff --git a/crates/gpui3/src/elements/div.rs b/crates/gpui3/src/elements/div.rs index aef32be22c..c763619de1 100644 --- a/crates/gpui3/src/elements/div.rs +++ b/crates/gpui3/src/elements/div.rs @@ -27,10 +27,10 @@ impl Element for Div { type State = S; type FrameState = Vec; - fn layout( + fn layout( &mut self, view: &mut S, - cx: &mut ViewContext, + cx: &mut ViewContext, ) -> Result<(LayoutId, Self::FrameState)> { let style = self.computed_style(); let child_layout_ids = if let Some(text_style) = style.text_style(cx) { @@ -45,12 +45,12 @@ impl Element for Div { )) } - fn paint( + fn paint( &mut self, layout: Layout, state: &mut S, child_layouts: &mut Self::FrameState, - cx: &mut ViewContext, + cx: &mut ViewContext, ) -> Result<()> { let Layout { order, bounds } = layout; @@ -130,22 +130,18 @@ impl Div { offset } - fn layout_children( - &mut self, - view: &mut S, - cx: &mut ViewContext, - ) -> Result> { + fn layout_children(&mut self, view: &mut S, cx: &mut ViewContext) -> Result> { self.children .iter_mut() .map(|child| child.layout(view, cx)) .collect::>>() } - fn paint_children( + fn paint_children( &mut self, overflow: &Point, state: &mut S, - cx: &mut ViewContext, + cx: &mut ViewContext, ) -> Result<()> { let scroll_offset = self.scroll_offset(overflow); for child in &mut self.children { @@ -154,13 +150,13 @@ impl Div { Ok(()) } - fn handle_scroll( + fn handle_scroll( &mut self, _order: u32, bounds: Bounds, overflow: Point, child_layout_ids: &[LayoutId], - cx: &mut ViewContext, + cx: &mut ViewContext, ) { if overflow.y == Overflow::Scroll || overflow.x == Overflow::Scroll { let mut scroll_max = Point::default(); diff --git a/crates/gpui3/src/style.rs b/crates/gpui3/src/style.rs index b319b46075..a8bac7ed78 100644 --- a/crates/gpui3/src/style.rs +++ b/crates/gpui3/src/style.rs @@ -171,7 +171,7 @@ pub struct HighlightStyle { impl Eq for HighlightStyle {} impl Style { - pub fn text_style(&self, _cx: &WindowContext) -> Option<&TextStyleRefinement> { + pub fn text_style(&self, _cx: &WindowContext) -> Option<&TextStyleRefinement> { if self.text.is_some() { Some(&self.text) } else { @@ -180,11 +180,7 @@ impl Style { } /// Paints the background of an element styled with this style. - pub fn paint_background( - &self, - _bounds: Bounds, - cx: &mut ViewContext, - ) { + pub fn paint_background(&self, _bounds: Bounds, cx: &mut ViewContext) { let _rem_size = cx.rem_size(); if let Some(_color) = self.fill.as_ref().and_then(Fill::color) { todo!(); @@ -192,11 +188,7 @@ impl Style { } /// Paints the foreground of an element styled with this style. - pub fn paint_foreground( - &self, - _bounds: Bounds, - cx: &mut ViewContext, - ) { + pub fn paint_foreground(&self, _bounds: Bounds, cx: &mut ViewContext) { let rem_size = cx.rem_size(); if let Some(_color) = self.border_color { diff --git a/crates/gpui3/src/window.rs b/crates/gpui3/src/window.rs index ea81d1b815..346b5cb25b 100644 --- a/crates/gpui3/src/window.rs +++ b/crates/gpui3/src/window.rs @@ -71,8 +71,8 @@ pub struct WindowContext<'a, 'w, Thread = ()> { window: Reference<'w, Window>, } -impl<'a, 'w, Thread> WindowContext<'a, 'w, Thread> { - pub(crate) fn mutable(app: &'a mut AppContext, window: &'w mut Window) -> Self { +impl<'a, 'w> WindowContext<'a, 'w> { + pub(crate) fn mutable(app: &'a mut AppContext, window: &'w mut Window) -> Self { Self { thread: PhantomData, app: Reference::Mutable(app), @@ -158,8 +158,8 @@ impl WindowContext<'_, '_, MainThread> { } } -impl Context for WindowContext<'_, '_, Thread> { - type EntityContext<'a, 'w, T: Send + Sync + 'static> = ViewContext<'a, 'w, T, Thread>; +impl Context for WindowContext<'_, '_> { + type EntityContext<'a, 'w, T: Send + Sync + 'static> = ViewContext<'a, 'w, T>; type Result = T; fn entity( @@ -167,7 +167,7 @@ impl Context for WindowContext<'_, '_, Thread> { build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T, ) -> Handle { let slot = self.entities.reserve(); - let entity = build_entity(&mut ViewContext::<'_, '_, T, Thread>::mutable( + let entity = build_entity(&mut ViewContext::mutable( &mut *self.app, &mut self.window, slot.id, @@ -190,8 +190,8 @@ impl Context for WindowContext<'_, '_, Thread> { } } -impl StackContext for ViewContext<'_, '_, S, Thread> { - fn app(&mut self) -> &mut AppContext { +impl StackContext for ViewContext<'_, '_, S> { + fn app(&mut self) -> &mut AppContext { &mut *self.app } @@ -226,12 +226,8 @@ pub struct ViewContext<'a, 'w, S, Thread = ()> { thread: PhantomData, } -impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> { - fn mutable( - app: &'a mut AppContext, - window: &'w mut Window, - entity_id: EntityId, - ) -> Self { +impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> { + fn mutable(app: &'a mut AppContext, window: &'w mut Window, entity_id: EntityId) -> Self { Self { window_cx: WindowContext::mutable(app, window), entity_id, @@ -247,10 +243,7 @@ impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> { pub fn observe( &mut self, handle: &Handle, - on_notify: impl Fn(&mut S, Handle, &mut ViewContext<'_, '_, S, Thread>) - + Send - + Sync - + 'static, + on_notify: impl Fn(&mut S, Handle, &mut ViewContext<'_, '_, S>) + Send + Sync + 'static, ) { let this = self.handle(); let handle = handle.downgrade(); @@ -280,10 +273,7 @@ impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> { self.window.dirty = true; } - pub(crate) fn erase_state( - &mut self, - f: impl FnOnce(&mut ViewContext<(), Thread>) -> R, - ) -> R { + pub(crate) fn erase_state(&mut self, f: impl FnOnce(&mut ViewContext<()>) -> R) -> R { let entity_id = self.unit_entity.id; let mut cx = ViewContext::mutable( &mut *self.window_cx.app, @@ -294,8 +284,8 @@ impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> { } } -impl<'a, 'w, S: 'static, Thread: 'static> Context for ViewContext<'a, 'w, S, Thread> { - type EntityContext<'b, 'c, U: Send + Sync + 'static> = ViewContext<'b, 'c, U, Thread>; +impl<'a, 'w, S: 'static> Context for ViewContext<'a, 'w, S> { + type EntityContext<'b, 'c, U: Send + Sync + 'static> = ViewContext<'b, 'c, U>; type Result = U; fn entity(