This commit is contained in:
Antonio Scandurra
2023-10-26 17:15:19 +02:00
parent 98d03f6e7a
commit 637cff3ebd
22 changed files with 206 additions and 267 deletions
+3 -3
View File
@@ -5,7 +5,7 @@ use crate::{
use anyhow::anyhow;
use derive_more::{Deref, DerefMut};
use parking_lot::Mutex;
use std::{any::Any, future::Future, sync::Weak};
use std::{future::Future, sync::Weak};
#[derive(Clone)]
pub struct AsyncAppContext {
@@ -22,7 +22,7 @@ impl Context for AsyncAppContext {
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
) -> Self::Result<Handle<T>>
where
T: Any + Send + Sync,
T: 'static + Send,
{
let app = self
.app
@@ -224,7 +224,7 @@ impl Context for AsyncWindowContext {
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
) -> Result<Handle<T>>
where
T: Any + Send + Sync,
T: 'static + Send,
{
self.app
.update_window(self.window, |cx| cx.entity(build_entity))
+2 -2
View File
@@ -4,7 +4,7 @@ use derive_more::{Deref, DerefMut};
use parking_lot::{RwLock, RwLockUpgradableReadGuard};
use slotmap::{SecondaryMap, SlotMap};
use std::{
any::{type_name, Any, TypeId},
any::{type_name, TypeId},
fmt::{self, Display},
hash::{Hash, Hasher},
marker::PhantomData,
@@ -59,7 +59,7 @@ impl EntityMap {
/// Insert an entity into a slot obtained by calling `reserve`.
pub fn insert<T>(&mut self, slot: Slot<T>, entity: T) -> Handle<T>
where
T: Any + Send + Sync,
T: 'static + Send,
{
let handle = slot.0;
self.entities.insert(handle.entity_id, Box::new(entity));
+13 -14
View File
@@ -43,10 +43,10 @@ impl<'a, T: 'static> ModelContext<'a, T> {
pub fn observe<T2: 'static>(
&mut self,
handle: &Handle<T2>,
mut on_notify: impl FnMut(&mut T, Handle<T2>, &mut ModelContext<'_, T>) + Send + Sync + 'static,
mut on_notify: impl FnMut(&mut T, Handle<T2>, &mut ModelContext<'_, T>) + Send + 'static,
) -> Subscription
where
T: Any + Send + Sync,
T: 'static + Send,
{
let this = self.weak_handle();
let handle = handle.downgrade();
@@ -68,11 +68,10 @@ impl<'a, T: 'static> ModelContext<'a, T> {
handle: &Handle<E>,
mut on_event: impl FnMut(&mut T, Handle<E>, &E::Event, &mut ModelContext<'_, T>)
+ Send
+ Sync
+ 'static,
) -> Subscription
where
T: Any + Send + Sync,
T: 'static + Send,
{
let this = self.weak_handle();
let handle = handle.downgrade();
@@ -92,7 +91,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
pub fn on_release(
&mut self,
mut on_release: impl FnMut(&mut T, &mut AppContext) + Send + Sync + 'static,
mut on_release: impl FnMut(&mut T, &mut AppContext) + Send + 'static,
) -> Subscription
where
T: 'static,
@@ -109,10 +108,10 @@ impl<'a, T: 'static> ModelContext<'a, T> {
pub fn observe_release<E: 'static>(
&mut self,
handle: &Handle<E>,
mut on_release: impl FnMut(&mut T, &mut E, &mut ModelContext<'_, T>) + Send + Sync + 'static,
mut on_release: impl FnMut(&mut T, &mut E, &mut ModelContext<'_, T>) + Send + 'static,
) -> Subscription
where
T: Any + Send + Sync,
T: Any + Send,
{
let this = self.weak_handle();
self.app.release_listeners.insert(
@@ -128,10 +127,10 @@ impl<'a, T: 'static> ModelContext<'a, T> {
pub fn observe_global<G: 'static>(
&mut self,
mut f: impl FnMut(&mut T, &mut ModelContext<'_, T>) + Send + Sync + 'static,
mut f: impl FnMut(&mut T, &mut ModelContext<'_, T>) + Send + 'static,
) -> Subscription
where
T: Any + Send + Sync,
T: 'static + Send,
{
let handle = self.weak_handle();
self.global_observers.insert(
@@ -142,11 +141,11 @@ impl<'a, T: 'static> ModelContext<'a, T> {
pub fn on_app_quit<Fut>(
&mut self,
mut on_quit: impl FnMut(&mut T, &mut ModelContext<T>) -> Fut + Send + Sync + 'static,
mut on_quit: impl FnMut(&mut T, &mut ModelContext<T>) -> Fut + Send + 'static,
) -> Subscription
where
Fut: 'static + Future<Output = ()> + Send,
T: Any + Send + Sync,
T: 'static + Send,
{
let handle = self.weak_handle();
self.app.quit_observers.insert(
@@ -177,7 +176,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
pub fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
where
G: 'static + Send + Sync,
G: 'static + Send,
{
let mut global = self.app.lease_global::<G>();
let result = f(&mut global, self);
@@ -214,7 +213,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
impl<'a, T> ModelContext<'a, T>
where
T: EventEmitter,
T::Event: Send + Sync,
T::Event: Send,
{
pub fn emit(&mut self, event: T::Event) {
self.app.pending_effects.push_back(Effect::Emit {
@@ -233,7 +232,7 @@ impl<'a, T> Context for ModelContext<'a, T> {
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, U>) -> U,
) -> Handle<U>
where
U: 'static + Send + Sync,
U: 'static + Send,
{
self.app.entity(build_entity)
}
+2 -2
View File
@@ -3,7 +3,7 @@ use crate::{
ModelContext, Result, Task, TestDispatcher, TestPlatform, WindowContext,
};
use parking_lot::Mutex;
use std::{any::Any, future::Future, sync::Arc};
use std::{future::Future, sync::Arc};
#[derive(Clone)]
pub struct TestAppContext {
@@ -20,7 +20,7 @@ impl Context for TestAppContext {
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
) -> Self::Result<Handle<T>>
where
T: Any + Send + Sync,
T: 'static + Send,
{
let mut lock = self.app.lock();
lock.entity(build_entity)