Eliminate ReadView trait
This commit is contained in:
parent
bce51c521a
commit
5dac95c47c
2 changed files with 12 additions and 36 deletions
|
@ -158,10 +158,6 @@ pub trait UpgradeViewHandle {
|
||||||
fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option<AnyViewHandle>;
|
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 {
|
pub trait ReadViewWith {
|
||||||
fn read_view_with<V, T>(
|
fn read_view_with<V, T>(
|
||||||
&self,
|
&self,
|
||||||
|
@ -1444,6 +1440,14 @@ impl AppContext {
|
||||||
.unwrap()
|
.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) {
|
fn remove_dropped_entities(&mut self) {
|
||||||
loop {
|
loop {
|
||||||
let (dropped_models, dropped_views, dropped_element_states) =
|
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)]
|
#[derive(Debug)]
|
||||||
pub enum ParentId {
|
pub enum ParentId {
|
||||||
View(usize),
|
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> {
|
impl<V: View> UpdateView for ViewContext<'_, '_, V> {
|
||||||
type Output<S> = S;
|
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> {
|
impl<V: View> UpdateView for EventContext<'_, '_, '_, V> {
|
||||||
type Output<S> = S;
|
type Output<S> = S;
|
||||||
|
|
||||||
|
@ -3924,7 +3906,7 @@ impl<T: View> ViewHandle<T> {
|
||||||
self.view_id
|
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)
|
cx.read_view(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,9 +15,9 @@ use crate::{
|
||||||
util::post_inc,
|
util::post_inc,
|
||||||
Action, AnyModelHandle, AnyView, AnyViewHandle, AnyWeakModelHandle, AnyWeakViewHandle,
|
Action, AnyModelHandle, AnyView, AnyViewHandle, AnyWeakModelHandle, AnyWeakViewHandle,
|
||||||
AppContext, Effect, Element, Entity, Handle, ModelContext, ModelHandle, MouseRegion,
|
AppContext, Effect, Element, Entity, Handle, ModelContext, ModelHandle, MouseRegion,
|
||||||
MouseRegionId, ParentId, ReadModel, ReadView, SceneBuilder, Subscription, UpdateModel,
|
MouseRegionId, ParentId, ReadModel, SceneBuilder, Subscription, UpdateModel, UpdateView,
|
||||||
UpdateView, UpgradeModelHandle, UpgradeViewHandle, View, ViewContext, ViewHandle,
|
UpgradeModelHandle, UpgradeViewHandle, View, ViewContext, ViewHandle, WeakModelHandle,
|
||||||
WeakModelHandle, WeakViewHandle, WindowInvalidation,
|
WeakViewHandle, WindowInvalidation,
|
||||||
};
|
};
|
||||||
use anyhow::{anyhow, bail, Result};
|
use anyhow::{anyhow, bail, Result};
|
||||||
use collections::{HashMap, HashSet};
|
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<'_> {
|
impl UpdateView for WindowContext<'_> {
|
||||||
type Output<S> = S;
|
type Output<S> = S;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue