From faabed1df03c210340bd4e912d55e41168e805b3 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 12 Sep 2023 21:34:03 -0600 Subject: [PATCH] Checkpoint --- crates/storybook/src/sketch.rs | 83 ++++++++++++++++------------------ 1 file changed, 39 insertions(+), 44 deletions(-) diff --git a/crates/storybook/src/sketch.rs b/crates/storybook/src/sketch.rs index 28bdde5cd2..f56b7a1189 100644 --- a/crates/storybook/src/sketch.rs +++ b/crates/storybook/src/sketch.rs @@ -35,22 +35,6 @@ impl AppContext { } } - fn update_entity( - &mut self, - handle: &Handle, - update: impl FnOnce(&mut T, &mut ModelContext) -> R, - ) -> R { - let mut entity = self - .entities - .remove(&handle.id) - .unwrap() - .downcast::() - .unwrap(); - let result = update(&mut *entity, &mut ModelContext::mutable(self, handle.id)); - self.entities.insert(handle.id, Box::new(entity)); - result - } - fn update_window( &mut self, window_id: WindowId, @@ -213,50 +197,61 @@ pub struct Handle { entity_type: PhantomData, } -trait Context<'a, 'parent, 'app, 'win> { - type EntityContext; +trait Context { + type EntityContext<'cx, 'app, 'win, T: 'static>; - fn update_entity( - &mut self, + fn update_entity<'a, 'app, 'win, T: 'static, R>( + &'a mut self, handle: &Handle, - update: impl FnOnce(&mut T, Self::EntityContext) -> R, + update: impl for<'cx> FnOnce(&mut T, &mut Self::EntityContext<'cx, 'app, 'win, T>) -> R, ) -> R; } -impl<'a, 'parent: 'a, 'app, 'win> Context<'a, 'parent, 'app, 'win> for AppContext { - type EntityContext = &'a mut ModelContext<'parent, T>; +impl Context for AppContext { + type EntityContext<'cx, 'app, 'win, T: 'static> = ModelContext<'cx, T>; - fn update_entity( - &mut self, + fn update_entity<'a, 'app, 'win, T: 'static, R>( + &'a mut self, handle: &Handle, - update: impl FnOnce(&mut T, Self::EntityContext) -> R, + update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, 'app, 'win, T>) -> R, ) -> R { - todo!() + let mut entity = self + .entities + .remove(&handle.id) + .unwrap() + .downcast::() + .unwrap(); + let result = update(&mut *entity, &mut ModelContext::mutable(self, handle.id)); + self.entities.insert(handle.id, Box::new(entity)); + result } } -impl<'a, 'parent, 'app, 'win> Context<'a, 'parent, 'app, 'win> for WindowContext<'app, 'win> -where - 'parent: 'a, - 'app: 'parent, - 'win: 'parent, -{ - type EntityContext = &'a mut ViewContext<'parent, 'app, 'win, T>; +// impl<'app, 'win> Context for WindowContext<'app, 'win> { +// type EntityContext<'cx, 'app, 'win, T: 'static> = ModelContext<'cx, T>; - fn update_entity( - &mut self, - handle: &Handle, - update: impl FnOnce(&mut T, Self::EntityContext) -> R, - ) -> R { - todo!() - } -} +// fn update_entity<'a, 'app, 'win, T: 'static, R>( +// &'a mut self, +// handle: &Handle, +// update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, 'app, 'win, T>) -> R, +// ) -> R { +// let mut entity = self +// .entities +// .remove(&handle.id) +// .unwrap() +// .downcast::() +// .unwrap(); +// let result = update(&mut *entity, &mut ModelContext::mutable(self, handle.id)); +// self.entities.insert(handle.id, Box::new(entity)); +// result +// } +// } impl Handle { - fn update<'a, 'parent, 'app, 'win, C: Context<'a, 'parent, 'app, 'win>, R>( + fn update<'app, 'win, C: Context, R>( &self, cx: &mut C, - update: impl FnOnce(&mut T, C::EntityContext) -> R, + update: impl FnOnce(&mut T, &mut C::EntityContext<'_, 'app, 'win, T>) -> R, ) -> R { cx.update_entity(self, update) }