mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-27 11:02:33 +01:00
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
This commit is contained in:
+11
-18
@@ -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<Lease<T>>,
|
||||
app: &'a mut App,
|
||||
guard: DropGuard<(Lease<T>, &'a mut App), fn((Lease<T>, &'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<T> for GpuiBorrow<'a, T> {
|
||||
fn borrow(&self) -> &T {
|
||||
self.inner.as_ref().unwrap().borrow()
|
||||
&self.guard.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: 'static> std::borrow::BorrowMut<T> 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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user