Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Add downcast_ref to AnyViewHandle

Nathan Sobo138aa0492a12
diff --git a/crates/file_finder/src/file_finder.rs b/crates/file_finder/src/file_finder.rs
index bc7e23d1ae7..1160cb61744 100644
--- a/crates/file_finder/src/file_finder.rs
+++ b/crates/file_finder/src/file_finder.rs
@@ -353,8 +353,7 @@ mod tests {
assert_eq!(
active_item
.as_any()
- .clone()
- .downcast::<Editor>()
+ .downcast_ref::<Editor>()
.unwrap()
.read(cx)
.title(cx),
diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs
index 137a9d06536..4f14d4e9d62 100644
--- a/crates/gpui/src/app.rs
+++ b/crates/gpui/src/app.rs
@@ -4661,6 +4661,7 @@ impl<T> Clone for WeakModelHandle<T> {
impl<T> Copy for WeakModelHandle<T> {}
+#[repr(transparent)]
pub struct ViewHandle<T> {
any_handle: AnyViewHandle,
view_type: PhantomData<T>,
@@ -4877,6 +4878,14 @@ impl AnyViewHandle {
}
}
+ pub fn downcast_ref<T: View>(&self) -> Option<&ViewHandle<T>> {
+ if self.is::<T>() {
+ Some(unsafe { mem::transmute(self) })
+ } else {
+ None
+ }
+ }
+
pub fn downgrade(&self) -> AnyWeakViewHandle {
AnyWeakViewHandle {
window_id: self.window_id,
diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs
index c1b1131aac2..572c87fcd50 100644
--- a/crates/workspace/src/pane.rs
+++ b/crates/workspace/src/pane.rs
@@ -2279,8 +2279,7 @@ mod tests {
.map(|(ix, item)| {
let mut state = item
.as_any()
- .clone()
- .downcast::<TestItem>()
+ .downcast_ref::<TestItem>()
.unwrap()
.read(cx)
.label
diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs
index 9258a79d92a..11546565a4e 100644
--- a/crates/zed/src/zed.rs
+++ b/crates/zed/src/zed.rs
@@ -1021,8 +1021,7 @@ mod tests {
.active_item()
.unwrap()
.as_any()
- .clone()
- .downcast::<Editor>()
+ .downcast_ref::<Editor>()
.unwrap()
.read(cx)
.title(cx),
@@ -1058,8 +1057,7 @@ mod tests {
.active_item()
.unwrap()
.as_any()
- .clone()
- .downcast::<Editor>()
+ .downcast_ref::<Editor>()
.unwrap()
.read(cx)
.title(cx),
@@ -1095,8 +1093,7 @@ mod tests {
.active_item()
.unwrap()
.as_any()
- .clone()
- .downcast::<Editor>()
+ .downcast_ref::<Editor>()
.unwrap()
.read(cx)
.title(cx),
@@ -1146,8 +1143,7 @@ mod tests {
.active_item()
.unwrap()
.as_any()
- .clone()
- .downcast::<Editor>()
+ .downcast_ref::<Editor>()
.unwrap()
.read(cx)
.title(cx),