WIP: Parameterize over thread
This commit is contained in:
parent
c1a35a29a8
commit
103183f494
7 changed files with 107 additions and 76 deletions
|
@ -63,12 +63,12 @@ impl Window {
|
|||
}
|
||||
|
||||
#[derive(Deref, DerefMut)]
|
||||
pub struct WindowContext<'a, 'b, Thread = ()> {
|
||||
pub struct WindowContext<'a, 'w, Thread = ()> {
|
||||
thread: PhantomData<Thread>,
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
app: Reference<'a, AppContext<Thread>>,
|
||||
window: Reference<'b, Window>,
|
||||
window: Reference<'w, Window>,
|
||||
}
|
||||
|
||||
impl<'a, 'w, Thread> WindowContext<'a, 'w, Thread> {
|
||||
|
@ -158,8 +158,8 @@ impl WindowContext<'_, '_, MainThread> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<Thread> Context for WindowContext<'_, '_, Thread> {
|
||||
type EntityContext<'a, 'w, T: Send + Sync + 'static> = ViewContext<'a, 'w, T>;
|
||||
impl<Thread: 'static> Context for WindowContext<'_, '_, Thread> {
|
||||
type EntityContext<'a, 'w, T: Send + Sync + 'static> = ViewContext<'a, 'w, T, Thread>;
|
||||
type Result<T> = T;
|
||||
|
||||
fn entity<T: Send + Sync + 'static>(
|
||||
|
@ -167,7 +167,7 @@ impl<Thread> Context for WindowContext<'_, '_, Thread> {
|
|||
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
|
||||
) -> Handle<T> {
|
||||
let slot = self.entities.reserve();
|
||||
let entity = build_entity(&mut ViewContext::mutable(
|
||||
let entity = build_entity(&mut ViewContext::<'_, '_, T, Thread>::mutable(
|
||||
&mut *self.app,
|
||||
&mut self.window,
|
||||
slot.id,
|
||||
|
@ -191,7 +191,7 @@ impl<Thread> Context for WindowContext<'_, '_, Thread> {
|
|||
}
|
||||
|
||||
impl<S, Thread> StackContext for ViewContext<'_, '_, S, Thread> {
|
||||
fn app(&mut self) -> &mut AppContext {
|
||||
fn app(&mut self) -> &mut AppContext<Thread> {
|
||||
&mut *self.app
|
||||
}
|
||||
|
||||
|
@ -220,14 +220,18 @@ impl<S, Thread> StackContext for ViewContext<'_, '_, S, Thread> {
|
|||
pub struct ViewContext<'a, 'w, S, Thread = ()> {
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
window_cx: WindowContext<'a, 'w>,
|
||||
window_cx: WindowContext<'a, 'w, Thread>,
|
||||
entity_type: PhantomData<S>,
|
||||
entity_id: EntityId,
|
||||
thread: PhantomData<Thread>,
|
||||
}
|
||||
|
||||
impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> {
|
||||
fn mutable(app: &'a mut AppContext, window: &'w mut Window, entity_id: EntityId) -> Self {
|
||||
fn mutable(
|
||||
app: &'a mut AppContext<Thread>,
|
||||
window: &'w mut Window,
|
||||
entity_id: EntityId,
|
||||
) -> Self {
|
||||
Self {
|
||||
window_cx: WindowContext::mutable(app, window),
|
||||
entity_id,
|
||||
|
@ -243,7 +247,10 @@ impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> {
|
|||
pub fn observe<E: Send + Sync + 'static>(
|
||||
&mut self,
|
||||
handle: &Handle<E>,
|
||||
on_notify: impl Fn(&mut S, Handle<E>, &mut ViewContext<'_, '_, S>) + Send + Sync + 'static,
|
||||
on_notify: impl Fn(&mut S, Handle<E>, &mut ViewContext<'_, '_, S, Thread>)
|
||||
+ Send
|
||||
+ Sync
|
||||
+ 'static,
|
||||
) {
|
||||
let this = self.handle();
|
||||
let handle = handle.downgrade();
|
||||
|
@ -273,7 +280,10 @@ impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> {
|
|||
self.window.dirty = true;
|
||||
}
|
||||
|
||||
pub(crate) fn erase_state<R>(&mut self, f: impl FnOnce(&mut ViewContext<()>) -> R) -> R {
|
||||
pub(crate) fn erase_state<R>(
|
||||
&mut self,
|
||||
f: impl FnOnce(&mut ViewContext<(), Thread>) -> R,
|
||||
) -> R {
|
||||
let entity_id = self.unit_entity.id;
|
||||
let mut cx = ViewContext::mutable(
|
||||
&mut *self.window_cx.app,
|
||||
|
@ -284,8 +294,8 @@ impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, 'w, S: 'static, Thread> Context for ViewContext<'a, 'w, S, Thread> {
|
||||
type EntityContext<'b, 'c, U: Send + Sync + 'static> = ViewContext<'b, 'c, U>;
|
||||
impl<'a, 'w, S: 'static, Thread: 'static> Context for ViewContext<'a, 'w, S, Thread> {
|
||||
type EntityContext<'b, 'c, U: Send + Sync + 'static> = ViewContext<'b, 'c, U, Thread>;
|
||||
type Result<U> = U;
|
||||
|
||||
fn entity<T2: Send + Sync + 'static>(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue