diff --git a/crates/gpui2/src/app/entity_map.rs b/crates/gpui2/src/app/entity_map.rs index 840b0831cd..bbeabd3e4f 100644 --- a/crates/gpui2/src/app/entity_map.rs +++ b/crates/gpui2/src/app/entity_map.rs @@ -172,14 +172,14 @@ impl AnyModel { } } - pub fn downcast(self) -> Option> { + pub fn downcast(self) -> Result, AnyModel> { if TypeId::of::() == self.entity_type { - Some(Model { - any_model: self.clone(), + Ok(Model { + any_model: self, entity_type: PhantomData, }) } else { - None + Err(self) } } } @@ -243,6 +243,14 @@ impl PartialEq for AnyModel { impl Eq for AnyModel {} +impl std::fmt::Debug for AnyModel { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("AnyModel") + .field("entity_id", &self.entity_id.as_u64()) + .finish() + } +} + #[derive(Deref, DerefMut)] pub struct Model { #[deref] diff --git a/crates/gpui2/src/view.rs b/crates/gpui2/src/view.rs index 08e64261e1..3c3ad034b4 100644 --- a/crates/gpui2/src/view.rs +++ b/crates/gpui2/src/view.rs @@ -211,6 +211,7 @@ trait ViewObject: Send + Sync { fn initialize(&self, cx: &mut WindowContext) -> AnyBox; fn layout(&self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId; fn paint(&self, bounds: Bounds, element: &mut AnyBox, cx: &mut WindowContext); + fn debug(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result; } impl ViewObject for View @@ -256,14 +257,24 @@ where }); }); } + + fn debug(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct(&format!("AnyView<{}>", std::any::type_name::())) + .field("entity_id", &ViewObject::entity_id(self).as_u64()) + .finish() + } } #[derive(Clone)] pub struct AnyView(Arc); impl AnyView { - pub fn downcast(self) -> Option> { - self.0.model().downcast().map(|model| View { model }) + pub fn downcast(self) -> Result, AnyView> { + self.0 + .model() + .downcast() + .map(|model| View { model }) + .map_err(|_| self) } pub(crate) fn entity_type(&self) -> TypeId { @@ -326,6 +337,12 @@ impl Element<()> for AnyView { } } +impl std::fmt::Debug for AnyView { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.0.debug(f) + } +} + struct EraseAnyViewState { view: AnyView, parent_view_state_type: PhantomData,