@@ -126,14 +126,6 @@ pub trait BorrowAppContext {
fn update<T, F: FnOnce(&mut AppContext) -> T>(&mut self, f: F) -> T;
}
-pub trait ReadModelWith {
- fn read_model_with<E: Entity, T>(
- &self,
- handle: &ModelHandle<E>,
- read: &mut dyn FnMut(&E, &AppContext) -> T,
- ) -> T;
-}
-
pub trait UpdateModel {
fn update_model<T: Entity, O>(
&mut self,
@@ -414,18 +406,6 @@ impl UpdateModel for AsyncAppContext {
}
}
-impl ReadModelWith for AsyncAppContext {
- fn read_model_with<E: Entity, T>(
- &self,
- handle: &ModelHandle<E>,
- read: &mut dyn FnMut(&E, &AppContext) -> T,
- ) -> T {
- let cx = self.0.borrow();
- let cx = &*cx;
- read(handle.read(cx), cx)
- }
-}
-
impl UpdateView for AsyncAppContext {
type Output<S> = Result<S>;
@@ -3609,14 +3589,10 @@ impl<T: Entity> ModelHandle<T> {
pub fn read_with<C, F, S>(&self, cx: &C, read: F) -> S
where
- C: ReadModelWith,
+ C: BorrowAppContext,
F: FnOnce(&T, &AppContext) -> S,
{
- let mut read = Some(read);
- cx.read_model_with(self, &mut |model, cx| {
- let read = read.take().unwrap();
- read(model, cx)
- })
+ cx.read_with(|cx| read(self.read(cx), cx))
}
pub fn update<C, F, S>(&self, cx: &mut C, update: F) -> S
@@ -22,8 +22,8 @@ use crate::{
keymap_matcher::Keystroke,
platform,
platform::{Event, InputHandler, KeyDownEvent, Platform},
- Action, AnyViewHandle, AppContext, Entity, FontCache, Handle, ModelContext, ModelHandle,
- ReadModelWith, ReadViewWith, Subscription, Task, UpdateModel, UpdateView, View, ViewContext,
+ Action, AnyViewHandle, AppContext, BorrowAppContext, Entity, FontCache, Handle, ModelContext,
+ ModelHandle, ReadViewWith, Subscription, Task, UpdateModel, UpdateView, View, ViewContext,
ViewHandle, WeakHandle, WindowContext,
};
use collections::BTreeMap;
@@ -381,6 +381,16 @@ impl TestAppContext {
}
}
+impl BorrowAppContext for TestAppContext {
+ fn read_with<T, F: FnOnce(&AppContext) -> T>(&self, f: F) -> T {
+ self.cx.borrow().read_with(f)
+ }
+
+ fn update<T, F: FnOnce(&mut AppContext) -> T>(&mut self, f: F) -> T {
+ self.cx.borrow_mut().update(f)
+ }
+}
+
impl UpdateModel for TestAppContext {
fn update_model<T: Entity, O>(
&mut self,
@@ -391,18 +401,6 @@ impl UpdateModel for TestAppContext {
}
}
-impl ReadModelWith for TestAppContext {
- fn read_model_with<E: Entity, T>(
- &self,
- handle: &ModelHandle<E>,
- read: &mut dyn FnMut(&E, &AppContext) -> T,
- ) -> T {
- let cx = self.cx.borrow();
- let cx = &*cx;
- read(handle.read(cx), cx)
- }
-}
-
impl UpdateView for TestAppContext {
type Output<S> = S;