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 19, 2024
1 parent 40df50c commit 86db48b
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 @@ -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<AppContext>,
app: AppContext,
}

impl AppCell {
Expand All @@ -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)]
Expand All @@ -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) {
Expand All @@ -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() {
Expand Down Expand Up @@ -254,7 +284,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 @@ -288,7 +318,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 86db48b

Please sign in to comment.