Skip to content

Commit

Permalink
Fix borrow_mut error on Windows.
Browse files Browse the repository at this point in the history
Co-authored-by: Sunli <[email protected]>
  • Loading branch information
huacnlee and sunli829 committed Jul 18, 2024
1 parent a91b039 commit 4df6491
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions crates/gpui/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
any::{type_name, TypeId},
cell::{Ref, RefCell, RefMut},
cell::RefCell,
marker::PhantomData,
ops::{Deref, DerefMut},
path::{Path, PathBuf},
Expand All @@ -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;
Expand Down Expand Up @@ -48,7 +47,7 @@ pub const SHUTDOWN_TIMEOUT: Duration = Duration::from_millis(100);
/// Strongly consider removing after stabilization.
#[doc(hidden)]
pub struct AppCell {
app: RefCell<AppContext>,
app: AppContext,
}

impl AppCell {
Expand All @@ -59,7 +58,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)]
Expand All @@ -69,13 +71,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) {
Expand All @@ -87,9 +102,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() {
Expand Down Expand Up @@ -255,7 +285,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,
Expand Down Expand Up @@ -289,7 +319,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());
Expand Down

0 comments on commit 4df6491

Please sign in to comment.