Eliminate ReadView trait

This commit is contained in:
Nathan Sobo 2023-04-21 15:03:57 -06:00
parent bce51c521a
commit 5dac95c47c
2 changed files with 12 additions and 36 deletions

View file

@ -158,10 +158,6 @@ pub trait UpgradeViewHandle {
fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option<AnyViewHandle>;
}
pub trait ReadView {
fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T;
}
pub trait ReadViewWith {
fn read_view_with<V, T>(
&self,
@ -1444,6 +1440,14 @@ impl AppContext {
.unwrap()
}
pub fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
if let Some(view) = self.views.get(&(handle.window_id, handle.view_id)) {
view.as_any().downcast_ref().expect("downcast is type safe")
} else {
panic!("circular view reference for type {}", type_name::<T>());
}
}
fn remove_dropped_entities(&mut self) {
loop {
let (dropped_models, dropped_views, dropped_element_states) =
@ -2172,16 +2176,6 @@ impl UpgradeViewHandle for AppContext {
}
}
impl ReadView for AppContext {
fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
if let Some(view) = self.views.get(&(handle.window_id, handle.view_id)) {
view.as_any().downcast_ref().expect("downcast is type safe")
} else {
panic!("circular view reference for type {}", type_name::<T>());
}
}
}
#[derive(Debug)]
pub enum ParentId {
View(usize),
@ -3488,12 +3482,6 @@ impl<V: View> UpdateModel for ViewContext<'_, '_, V> {
}
}
impl<V: View> ReadView for ViewContext<'_, '_, V> {
fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
self.window_context.read_view(handle)
}
}
impl<V: View> UpdateView for ViewContext<'_, '_, V> {
type Output<S> = S;
@ -3551,12 +3539,6 @@ impl<V: View> UpdateModel for EventContext<'_, '_, '_, V> {
}
}
impl<V: View> ReadView for EventContext<'_, '_, '_, V> {
fn read_view<W: View>(&self, handle: &crate::ViewHandle<W>) -> &W {
self.view_context.read_view(handle)
}
}
impl<V: View> UpdateView for EventContext<'_, '_, '_, V> {
type Output<S> = S;
@ -3924,7 +3906,7 @@ impl<T: View> ViewHandle<T> {
self.view_id
}
pub fn read<'a, C: ReadView>(&self, cx: &'a C) -> &'a T {
pub fn read<'a>(&self, cx: &'a AppContext) -> &'a T {
cx.read_view(self)
}

View file

@ -15,9 +15,9 @@ use crate::{
util::post_inc,
Action, AnyModelHandle, AnyView, AnyViewHandle, AnyWeakModelHandle, AnyWeakViewHandle,
AppContext, Effect, Element, Entity, Handle, ModelContext, ModelHandle, MouseRegion,
MouseRegionId, ParentId, ReadModel, ReadView, SceneBuilder, Subscription, UpdateModel,
UpdateView, UpgradeModelHandle, UpgradeViewHandle, View, ViewContext, ViewHandle,
WeakModelHandle, WeakViewHandle, WindowInvalidation,
MouseRegionId, ParentId, ReadModel, SceneBuilder, Subscription, UpdateModel, UpdateView,
UpgradeModelHandle, UpgradeViewHandle, View, ViewContext, ViewHandle, WeakModelHandle,
WeakViewHandle, WindowInvalidation,
};
use anyhow::{anyhow, bail, Result};
use collections::{HashMap, HashSet};
@ -149,12 +149,6 @@ impl UpdateModel for WindowContext<'_> {
}
}
impl ReadView for WindowContext<'_> {
fn read_view<W: View>(&self, handle: &crate::ViewHandle<W>) -> &W {
self.app_context.read_view(handle)
}
}
impl UpdateView for WindowContext<'_> {
type Output<S> = S;