Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
WIP: Parameterize over thread
Marshall Bowers103183f494c9
diff --git a/crates/gpui3/src/app.rs b/crates/gpui3/src/app.rsindex 39603f96f0c..dbc68f77604 100644--- a/crates/gpui3/src/app.rs+++ b/crates/gpui3/src/app.rs@@ -120,40 +120,40 @@ impl<Thread> AppContext<Thread> {AsyncContext(self.this.clone())}- pub fn run_on_main<R>(- &self,- f: impl FnOnce(&mut AppContext<MainThread>) -> R + Send + 'static,- ) -> impl Future<Output = R>- where- R: Send + 'static,- {- let this = self.this.upgrade().unwrap();- run_on_main(self.dispatcher.clone(), move || {- let cx = &mut *this.lock();- let main_thread_cx: &mut AppContext<MainThread> = unsafe { std::mem::transmute(cx) };- main_thread_cx.update(|cx| f(cx))- })- }-- pub fn spawn_on_main<F, R>(- &self,- f: impl FnOnce(&mut AppContext<MainThread>) -> F + Send + 'static,- ) -> impl Future<Output = R>- where- F: Future<Output = R> + 'static,- R: Send + 'static,- {- let this = self.this.upgrade().unwrap();- spawn_on_main(self.dispatcher.clone(), move || {- let cx = &mut *this.lock();- let platform = cx.platform.borrow_on_main_thread().clone();- // todo!()- // cx.update(|cx| f(&mut MainThreadContext::mutable(cx, platform.as_ref())))-- future::ready(())- })- // self.platform.read(move |platform| {- }+ // pub fn run_on_main<R>(+ // &self,+ // f: impl FnOnce(&mut AppContext<MainThread>) -> R + Send + 'static,+ // ) -> impl Future<Output = R>+ // where+ // R: Send + 'static,+ // {+ // let this = self.this.upgrade().unwrap();+ // run_on_main(self.dispatcher.clone(), move || {+ // let cx = &mut *this.lock();+ // let main_thread_cx: &mut AppContext<MainThread> = unsafe { std::mem::transmute(cx) };+ // main_thread_cx.update(|cx| f(cx))+ // })+ // }++ // pub fn spawn_on_main<F, R>(+ // &self,+ // f: impl FnOnce(&mut AppContext<MainThread>) -> F + Send + 'static,+ // ) -> impl Future<Output = R>+ // where+ // F: Future<Output = R> + 'static,+ // R: Send + 'static,+ // {+ // let this = self.this.upgrade().unwrap();+ // spawn_on_main(self.dispatcher.clone(), move || {+ // let cx = &mut *this.lock();+ // let platform = cx.platform.borrow_on_main_thread().clone();+ // // todo!()+ // // cx.update(|cx| f(&mut MainThreadContext::mutable(cx, platform.as_ref())))++ // future::ready(())+ // })+ // // self.platform.read(move |platform| {+ // }pub fn text_style(&self) -> TextStyle {let mut style = TextStyle::default();@@ -276,8 +276,8 @@ impl<Thread> AppContext<Thread> {}}-impl Context for AppContext {- type EntityContext<'a, 'w, T: Send + Sync + 'static> = ModelContext<'a, T>;+impl<Thread: 'static> Context for AppContext<Thread> {+ type EntityContext<'a, 'w, T: Send + Sync + 'static> = ModelContext<'a, T, Thread>;type Result<T> = T;fn entity<T: Send + Sync + 'static>(diff --git a/crates/gpui3/src/app/model_context.rs b/crates/gpui3/src/app/model_context.rsindex b16d29057a6..95e7866493a 100644--- a/crates/gpui3/src/app/model_context.rs+++ b/crates/gpui3/src/app/model_context.rs@@ -1,14 +1,14 @@use crate::{AppContext, Context, Effect, EntityId, Handle, Reference, WeakHandle};use std::{marker::PhantomData, sync::Arc};-pub struct ModelContext<'a, T> {- app: Reference<'a, AppContext>,+pub struct ModelContext<'a, T, Thread = ()> {+ app: Reference<'a, AppContext<Thread>>,entity_type: PhantomData<T>,entity_id: EntityId,}-impl<'a, T: Send + Sync + 'static> ModelContext<'a, T> {- pub(crate) fn mutable(app: &'a mut AppContext, entity_id: EntityId) -> Self {+impl<'a, T: Send + Sync + 'static, Thread> ModelContext<'a, T, Thread> {+ pub(crate) fn mutable(app: &'a mut AppContext<Thread>, entity_id: EntityId) -> Self {Self {app: Reference::Mutable(app),entity_type: PhantomData,@@ -41,7 +41,7 @@ impl<'a, T: Send + Sync + 'static> ModelContext<'a, T> {pub fn observe<E: Send + Sync + 'static>(&mut self,handle: &Handle<E>,- on_notify: impl Fn(&mut T, Handle<E>, &mut ModelContext<'_, T>) + Send + Sync + 'static,+ on_notify: impl Fn(&mut T, Handle<E>, &mut ModelContext<'_, T, Thread>) + Send + Sync + 'static,) {let this = self.handle();let handle = handle.downgrade();diff --git a/crates/gpui3/src/element.rs b/crates/gpui3/src/element.rsindex c145a9725b1..0acfa07ce84 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<Thread>(&mut self,state: &mut Self::State,- cx: &mut ViewContext<Self::State>,+ cx: &mut ViewContext<Self::State, Thread>,) -> Result<(LayoutId, Self::FrameState)>;- fn paint(+ fn paint<Thread>(&mut self,layout: Layout,state: &mut Self::State,frame_state: &mut Self::FrameState,- cx: &mut ViewContext<Self::State>,+ cx: &mut ViewContext<Self::State, Thread>,) -> Result<()>;}@@ -42,12 +42,16 @@ pub trait ParentElement<S> {}trait ElementObject<S> {- fn layout(&mut self, state: &mut S, cx: &mut ViewContext<S>) -> Result<LayoutId>;- fn paint(+ fn layout<Thread>(+ &mut self,+ state: &mut S,+ cx: &mut ViewContext<S, Thread>,+ ) -> Result<LayoutId>;+ fn paint<Thread>(&mut self,state: &mut S,offset: Option<Point<Pixels>>,- cx: &mut ViewContext<S>,+ cx: &mut ViewContext<S, Thread>,) -> Result<()>;}@@ -135,15 +139,19 @@ impl<E: Element> ElementObject<E::State> for RenderedElement<E> {pub struct AnyElement<S>(Box<dyn ElementObject<S>>);impl<S> AnyElement<S> {- pub fn layout(&mut self, state: &mut S, cx: &mut ViewContext<S>) -> Result<LayoutId> {+ pub fn layout<Thread>(+ &mut self,+ state: &mut S,+ cx: &mut ViewContext<S, Thread>,+ ) -> Result<LayoutId> {self.0.layout(state, cx)}- pub fn paint(+ pub fn paint<Thread>(&mut self,state: &mut S,offset: Option<Point<Pixels>>,- cx: &mut ViewContext<S>,+ cx: &mut ViewContext<S, Thread>,) -> Result<()> {self.0.paint(state, offset, cx)}diff --git a/crates/gpui3/src/elements/div.rs b/crates/gpui3/src/elements/div.rsindex c763619de19..aef32be22c6 100644--- a/crates/gpui3/src/elements/div.rs+++ b/crates/gpui3/src/elements/div.rs@@ -27,10 +27,10 @@ impl<S: 'static + Send + Sync> Element for Div<S> {type State = S;type FrameState = Vec<LayoutId>;- fn layout(+ fn layout<Thread>(&mut self,view: &mut S,- cx: &mut ViewContext<S>,+ cx: &mut ViewContext<S, Thread>,) -> 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<S: 'static + Send + Sync> Element for Div<S> {))}- fn paint(+ fn paint<Thread>(&mut self,layout: Layout,state: &mut S,child_layouts: &mut Self::FrameState,- cx: &mut ViewContext<S>,+ cx: &mut ViewContext<S, Thread>,) -> Result<()> {let Layout { order, bounds } = layout;@@ -130,18 +130,22 @@ impl<S: 'static> Div<S> {offset}- fn layout_children(&mut self, view: &mut S, cx: &mut ViewContext<S>) -> Result<Vec<LayoutId>> {+ fn layout_children<Thread>(+ &mut self,+ view: &mut S,+ cx: &mut ViewContext<S, Thread>,+ ) -> Result<Vec<LayoutId>> {self.children.iter_mut().map(|child| child.layout(view, cx)).collect::<Result<Vec<LayoutId>>>()}- fn paint_children(+ fn paint_children<Thread>(&mut self,overflow: &Point<Overflow>,state: &mut S,- cx: &mut ViewContext<S>,+ cx: &mut ViewContext<S, Thread>,) -> Result<()> {let scroll_offset = self.scroll_offset(overflow);for child in &mut self.children {@@ -150,13 +154,13 @@ impl<S: 'static> Div<S> {Ok(())}- fn handle_scroll(+ fn handle_scroll<Thread>(&mut self,_order: u32,bounds: Bounds<Pixels>,overflow: Point<Overflow>,child_layout_ids: &[LayoutId],- cx: &mut ViewContext<S>,+ cx: &mut ViewContext<S, Thread>,) {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.rsindex a8bac7ed782..b319b460758 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<Thread>(&self, _cx: &WindowContext<Thread>) -> Option<&TextStyleRefinement> {if self.text.is_some() {Some(&self.text)} else {@@ -180,7 +180,11 @@ impl Style {}/// Paints the background of an element styled with this style.- pub fn paint_background<V: 'static>(&self, _bounds: Bounds<Pixels>, cx: &mut ViewContext<V>) {+ pub fn paint_background<V: 'static, Thread>(+ &self,+ _bounds: Bounds<Pixels>,+ cx: &mut ViewContext<V, Thread>,+ ) {let _rem_size = cx.rem_size();if let Some(_color) = self.fill.as_ref().and_then(Fill::color) {todo!();@@ -188,7 +192,11 @@ impl Style {}/// Paints the foreground of an element styled with this style.- pub fn paint_foreground<V: 'static>(&self, _bounds: Bounds<Pixels>, cx: &mut ViewContext<V>) {+ pub fn paint_foreground<V: 'static, Thread>(+ &self,+ _bounds: Bounds<Pixels>,+ cx: &mut ViewContext<V, Thread>,+ ) {let rem_size = cx.rem_size();if let Some(_color) = self.border_color {diff --git a/crates/gpui3/src/view.rs b/crates/gpui3/src/view.rsindex 86826606bcb..261505ce512 100644--- a/crates/gpui3/src/view.rs+++ b/crates/gpui3/src/view.rs@@ -1,14 +1,15 @@use parking_lot::Mutex;use crate::{- AnyElement, Element, Handle, IntoAnyElement, Layout, LayoutId, Result, ViewContext,+ AnyElement, Element, Handle, IntoAnyElement, Layout, LayoutId, MainThread, Result, ViewContext,WindowContext,};use std::{any::Any, marker::PhantomData, sync::Arc};-pub struct View<S: Send + Sync, P> {+pub struct View<S: Send + Sync, P, Thread = ()> {state: Handle<S>,- render: Arc<dyn Fn(&mut S, &mut ViewContext<S>) -> AnyElement<S> + Send + Sync + 'static>,+ render:+ Arc<dyn Fn(&mut S, &mut ViewContext<S, Thread>) -> AnyElement<S> + Send + Sync + 'static>,parent_state_type: PhantomData<P>,}diff --git a/crates/gpui3/src/window.rs b/crates/gpui3/src/window.rsindex 6309cbc54e4..ea81d1b8156 100644--- a/crates/gpui3/src/window.rs+++ b/crates/gpui3/src/window.rs@@ -63,12 +63,12 @@ impl Window {}#[derive(Deref, DerefMut)]-pub struct WindowContext<'a, 'b, Thread = ()> {+pub struct WindowContext<'a, 'w, Thread = ()> {thread: PhantomData<Thread>,#[deref]#[deref_mut]app: Reference<'a, AppContext<Thread>>,- window: Reference<'b, Window>,+ window: Reference<'w, Window>,}impl<'a, 'w, Thread> WindowContext<'a, 'w, Thread> {@@ -158,8 +158,8 @@ impl WindowContext<'_, '_, MainThread> {}}-impl<Thread> Context for WindowContext<'_, '_, Thread> {- type EntityContext<'a, 'w, T: Send + Sync + 'static> = ViewContext<'a, 'w, T>;+impl<Thread: 'static> Context for WindowContext<'_, '_, Thread> {+ type EntityContext<'a, 'w, T: Send + Sync + 'static> = ViewContext<'a, 'w, T, Thread>;type Result<T> = T;fn entity<T: Send + Sync + 'static>(@@ -167,7 +167,7 @@ impl<Thread> Context for WindowContext<'_, '_, Thread> {build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,) -> Handle<T> {let slot = self.entities.reserve();- let entity = build_entity(&mut ViewContext::mutable(+ let entity = build_entity(&mut ViewContext::<'_, '_, T, Thread>::mutable(&mut *self.app,&mut self.window,slot.id,@@ -191,7 +191,7 @@ impl<Thread> Context for WindowContext<'_, '_, Thread> {}impl<S, Thread> StackContext for ViewContext<'_, '_, S, Thread> {- fn app(&mut self) -> &mut AppContext {+ fn app(&mut self) -> &mut AppContext<Thread> {&mut *self.app}@@ -220,14 +220,18 @@ impl<S, Thread> StackContext for ViewContext<'_, '_, S, Thread> {pub struct ViewContext<'a, 'w, S, Thread = ()> {#[deref]#[deref_mut]- window_cx: WindowContext<'a, 'w>,+ window_cx: WindowContext<'a, 'w, Thread>,entity_type: PhantomData<S>,entity_id: EntityId,thread: PhantomData<Thread>,}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 {+ fn mutable(+ app: &'a mut AppContext<Thread>,+ window: &'w mut Window,+ entity_id: EntityId,+ ) -> Self {Self {window_cx: WindowContext::mutable(app, window),entity_id,@@ -243,7 +247,10 @@ impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> {pub fn observe<E: Send + Sync + 'static>(&mut self,handle: &Handle<E>,- on_notify: impl Fn(&mut S, Handle<E>, &mut ViewContext<'_, '_, S>) + Send + Sync + 'static,+ on_notify: impl Fn(&mut S, Handle<E>, &mut ViewContext<'_, '_, S, Thread>)+ + Send+ + Sync+ + 'static,) {let this = self.handle();let handle = handle.downgrade();@@ -273,7 +280,10 @@ impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> {self.window.dirty = true;}- pub(crate) fn erase_state<R>(&mut self, f: impl FnOnce(&mut ViewContext<()>) -> R) -> R {+ pub(crate) fn erase_state<R>(+ &mut self,+ f: impl FnOnce(&mut ViewContext<(), Thread>) -> R,+ ) -> R {let entity_id = self.unit_entity.id;let mut cx = ViewContext::mutable(&mut *self.window_cx.app,@@ -284,8 +294,8 @@ impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> {}}-impl<'a, 'w, S: 'static, Thread> Context for ViewContext<'a, 'w, S, Thread> {- type EntityContext<'b, 'c, U: Send + Sync + 'static> = ViewContext<'b, 'c, U>;+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>;type Result<U> = U;fn entity<T2: Send + Sync + 'static>(