From 3ee10bfc04aa4ab6252726f9e3ecdb66f0aca11c Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 18 Jul 2024 12:04:37 +0800 Subject: [PATCH] Fix borrow_mut error on Windows. Co-authored-by: Sunli --- crates/gpui/src/app.rs | 52 +++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 4dde1f8f10dc26..4285965f2adddf 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -1,6 +1,6 @@ use std::{ any::{type_name, TypeId}, - cell::{Ref, RefCell, RefMut}, + cell::RefCell, marker::PhantomData, ops::{Deref, DerefMut}, path::{Path, PathBuf}, @@ -10,7 +10,6 @@ use std::{ }; use anyhow::{anyhow, Result}; -use derive_more::{Deref, DerefMut}; use futures::{channel::oneshot, future::LocalBoxFuture, Future}; use slotmap::SlotMap; use smol::future::FutureExt; @@ -47,7 +46,7 @@ pub const SHUTDOWN_TIMEOUT: Duration = Duration::from_millis(100); /// Strongly consider removing after stabilization. #[doc(hidden)] pub struct AppCell { - app: RefCell, + app: AppContext, } impl AppCell { @@ -58,7 +57,10 @@ impl AppCell { let thread_id = std::thread::current().id(); eprintln!("borrowed {thread_id:?}"); } - AppRef(self.app.borrow()) + AppRef { + _mark: PhantomData, + app: &self.app as *const _, + } } #[doc(hidden)] @@ -68,13 +70,26 @@ impl AppCell { let thread_id = std::thread::current().id(); eprintln!("borrowed {thread_id:?}"); } - AppRefMut(self.app.borrow_mut()) + AppRefMut { + _mark: PhantomData, + app: &self.app as *const _ as *mut _, + } } } #[doc(hidden)] -#[derive(Deref, DerefMut)] -pub struct AppRef<'a>(Ref<'a, AppContext>); +pub struct AppRef<'a> { + _mark: PhantomData<&'a ()>, + app: *const AppContext, +} + +impl<'a> Deref for AppRef<'a> { + type Target = AppContext; + + fn deref(&self) -> &Self::Target { + unsafe { &*self.app } + } +} impl<'a> Drop for AppRef<'a> { fn drop(&mut self) { @@ -86,9 +101,24 @@ impl<'a> Drop for AppRef<'a> { } #[doc(hidden)] -#[derive(Deref, DerefMut)] -pub struct AppRefMut<'a>(RefMut<'a, AppContext>); +pub struct AppRefMut<'a> { + _mark: PhantomData<&'a ()>, + app: *mut AppContext, +} +impl<'a> Deref for AppRefMut<'a> { + type Target = AppContext; + + fn deref(&self) -> &Self::Target { + unsafe { &*self.app } + } +} + +impl<'a> DerefMut for AppRefMut<'a> { + fn deref_mut(&mut self) -> &mut Self::Target { + unsafe { &mut *self.app } + } +} impl<'a> Drop for AppRefMut<'a> { fn drop(&mut self) { if option_env!("TRACK_THREAD_BORROWS").is_some() { @@ -265,7 +295,7 @@ impl AppContext { let entities = EntityMap::new(); let app = Rc::new_cyclic(|this| AppCell { - app: RefCell::new(AppContext { + app: AppContext { this: this.clone(), platform: platform.clone(), text_system, @@ -299,7 +329,7 @@ impl AppContext { layout_id_buffer: Default::default(), propagate_event: true, prompt_builder: Some(PromptBuilder::Default), - }), + }, }); init_app_menus(platform.as_ref(), &mut app.borrow_mut());