Remove ReadModelWith trait

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2023-04-24 16:44:54 +02:00
parent 5f500d34b2
commit a9417f3d2e
2 changed files with 14 additions and 40 deletions

View file

@ -126,14 +126,6 @@ pub trait BorrowAppContext {
fn update<T, F: FnOnce(&mut AppContext) -> T>(&mut self, f: F) -> T; 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 { pub trait UpdateModel {
fn update_model<T: Entity, O>( fn update_model<T: Entity, O>(
&mut self, &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 { impl UpdateView for AsyncAppContext {
type Output<S> = Result<S>; 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 pub fn read_with<C, F, S>(&self, cx: &C, read: F) -> S
where where
C: ReadModelWith, C: BorrowAppContext,
F: FnOnce(&T, &AppContext) -> S, F: FnOnce(&T, &AppContext) -> S,
{ {
let mut read = Some(read); cx.read_with(|cx| read(self.read(cx), cx))
cx.read_model_with(self, &mut |model, cx| {
let read = read.take().unwrap();
read(model, cx)
})
} }
pub fn update<C, F, S>(&self, cx: &mut C, update: F) -> S pub fn update<C, F, S>(&self, cx: &mut C, update: F) -> S

View file

@ -22,8 +22,8 @@ use crate::{
keymap_matcher::Keystroke, keymap_matcher::Keystroke,
platform, platform,
platform::{Event, InputHandler, KeyDownEvent, Platform}, platform::{Event, InputHandler, KeyDownEvent, Platform},
Action, AnyViewHandle, AppContext, Entity, FontCache, Handle, ModelContext, ModelHandle, Action, AnyViewHandle, AppContext, BorrowAppContext, Entity, FontCache, Handle, ModelContext,
ReadModelWith, ReadViewWith, Subscription, Task, UpdateModel, UpdateView, View, ViewContext, ModelHandle, ReadViewWith, Subscription, Task, UpdateModel, UpdateView, View, ViewContext,
ViewHandle, WeakHandle, WindowContext, ViewHandle, WeakHandle, WindowContext,
}; };
use collections::BTreeMap; 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 { impl UpdateModel for TestAppContext {
fn update_model<T: Entity, O>( fn update_model<T: Entity, O>(
&mut self, &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 { impl UpdateView for TestAppContext {
type Output<S> = S; type Output<S> = S;