From fa6e2e98c703d6fa65aef299d4f07ddc28b8a466 Mon Sep 17 00:00:00 2001 From: tidely <43219534+tidely@users.noreply.github.com> Date: Wed, 25 Nov 2026 06:06:40 +0000 Subject: [PATCH] gpui: Remove `.unwrap()` on every GPUI entity borrow (#67361) ## Summary Removes an `unwrap` call during entity borrows. Realistically this only happens once in each function where the compiler can optimize the later calls away, but every new function usually still has one of these unwraps. I didn't take the time to benchmark though, since the effect will likely be insignificant. `DropGuard`, Rust 1.100 new feature, allows writing Drop implementations inline. The differentiating feature for me, is that the function takes a owned `self`, instead of `&mut self` as in `impl Drop`, this allows us to get rid of `Option` in a lot of drop implementations, where the type needs to be moved out during `Drop`. ## Testing Trivial change once familiar with `DropGuard`. ## Self-review - [x] I've reviewed my diff for quality, security, reliability, and performance. - [x] UI changes follow the [checklist](https://zed.dev/docs/development/ui-checklist). - [x] Tests cover the new or changed behavior. Release Notes: - N/A - [GPUI] Removed an unwrap during Entity borrow --- crates/gpui/src/app.rs | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) 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 } }