experiment with a way to recover the any entities when downcasting fails
This commit is contained in:
parent
6f1197e00c
commit
f5b13071f1
2 changed files with 31 additions and 6 deletions
|
@ -172,14 +172,14 @@ impl AnyModel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn downcast<T: 'static>(self) -> Option<Model<T>> {
|
pub fn downcast<T: 'static>(self) -> Result<Model<T>, AnyModel> {
|
||||||
if TypeId::of::<T>() == self.entity_type {
|
if TypeId::of::<T>() == self.entity_type {
|
||||||
Some(Model {
|
Ok(Model {
|
||||||
any_model: self.clone(),
|
any_model: self,
|
||||||
entity_type: PhantomData,
|
entity_type: PhantomData,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
None
|
Err(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -243,6 +243,14 @@ impl PartialEq for AnyModel {
|
||||||
|
|
||||||
impl Eq 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)]
|
#[derive(Deref, DerefMut)]
|
||||||
pub struct Model<T> {
|
pub struct Model<T> {
|
||||||
#[deref]
|
#[deref]
|
||||||
|
|
|
@ -211,6 +211,7 @@ trait ViewObject: Send + Sync {
|
||||||
fn initialize(&self, cx: &mut WindowContext) -> AnyBox;
|
fn initialize(&self, cx: &mut WindowContext) -> AnyBox;
|
||||||
fn layout(&self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId;
|
fn layout(&self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId;
|
||||||
fn paint(&self, bounds: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext);
|
fn paint(&self, bounds: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext);
|
||||||
|
fn debug(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V> ViewObject for View<V>
|
impl<V> ViewObject for View<V>
|
||||||
|
@ -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::<V>()))
|
||||||
|
.field("entity_id", &ViewObject::entity_id(self).as_u64())
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AnyView(Arc<dyn ViewObject>);
|
pub struct AnyView(Arc<dyn ViewObject>);
|
||||||
|
|
||||||
impl AnyView {
|
impl AnyView {
|
||||||
pub fn downcast<V: 'static + Send>(self) -> Option<View<V>> {
|
pub fn downcast<V: 'static + Send>(self) -> Result<View<V>, AnyView> {
|
||||||
self.0.model().downcast().map(|model| View { model })
|
self.0
|
||||||
|
.model()
|
||||||
|
.downcast()
|
||||||
|
.map(|model| View { model })
|
||||||
|
.map_err(|_| self)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn entity_type(&self) -> TypeId {
|
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<ParentViewState> {
|
struct EraseAnyViewState<ParentViewState> {
|
||||||
view: AnyView,
|
view: AnyView,
|
||||||
parent_view_state_type: PhantomData<ParentViewState>,
|
parent_view_state_type: PhantomData<ParentViewState>,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue