diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 84127dac4b7..e017195ef58 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -4,7 +4,7 @@ use std::{ cell::{Cell, Ref, RefCell, RefMut}, ffi::OsString, marker::PhantomData, - mem, + mem::{self, DropGuard}, ops::{Deref, DerefMut}, path::{Path, PathBuf}, rc::{Rc, Weak}, @@ -3791,8 +3791,7 @@ impl HttpClient for NullHttpClient { /// A mutable reference to an entity owned by GPUI pub struct GpuiBorrow<'a, T> { - inner: Option>, - app: &'a mut App, + guard: DropGuard<(Lease, &'a mut App), fn((Lease, &'a mut App))>, } impl<'a, T: 'static> GpuiBorrow<'a, T> { @@ -3800,21 +3799,24 @@ impl<'a, T: 'static> GpuiBorrow<'a, T> { app.start_update(); let lease = app.entities.lease(&inner); Self { - inner: Some(lease), - app, + guard: DropGuard::new((lease, app), |(lease, app)| { + app.notify(lease.id); + app.entities.end_lease(lease); + app.finish_update(); + }), } } } impl<'a, T: 'static> std::borrow::Borrow for GpuiBorrow<'a, T> { fn borrow(&self) -> &T { - self.inner.as_ref().unwrap().borrow() + &self.guard.0 } } impl<'a, T: 'static> std::borrow::BorrowMut for GpuiBorrow<'a, T> { fn borrow_mut(&mut self) -> &mut T { - self.inner.as_mut().unwrap().borrow_mut() + &mut self.guard.0 } } @@ -3822,22 +3824,13 @@ impl<'a, T: 'static> std::ops::Deref for GpuiBorrow<'a, T> { type Target = T; fn deref(&self) -> &Self::Target { - self.inner.as_ref().unwrap() + &self.guard.0 } } impl<'a, T: 'static> std::ops::DerefMut for GpuiBorrow<'a, T> { fn deref_mut(&mut self) -> &mut T { - self.inner.as_mut().unwrap() - } -} - -impl<'a, T> Drop for GpuiBorrow<'a, T> { - fn drop(&mut self) { - let lease = self.inner.take().unwrap(); - self.app.notify(lease.id); - self.app.entities.end_lease(lease); - self.app.finish_update(); + &mut self.guard.0 } }