Moved world effect function type from effect stack to the world trait.

This commit is contained in:
Filipe Rodrigues 2025-05-23 02:50:00 +01:00
parent 3627c5c5b3
commit daffffb620
Signed by: zenithsiz
SSH Key Fingerprint: SHA256:Mb5ppb3Sh7IarBO/sBTXLHbYEOz37hJAlslLQPPAPaU
6 changed files with 31 additions and 42 deletions

View File

@ -33,7 +33,7 @@
// Imports
use {
crate::{world::UnsizeF, Effect, EffectRun, ReactiveWorld, SignalBorrow, SignalWithDefaultImpl, Trigger},
crate::{Effect, EffectRun, ReactiveWorld, SignalBorrow, SignalWithDefaultImpl, Trigger},
core::{
fmt,
marker::{PhantomData, Unsize},
@ -73,7 +73,7 @@ impl<T, F, W: DerivedWorld<T, F>> Derived<T, F, W> {
where
T: 'static,
F: Fn() -> T + 'static,
EffectFn<T, F, W>: UnsizeF<W>,
EffectFn<T, F, W>: Unsize<W::F>,
{
let value = IMut::<_, W>::new(None);
let effect = Effect::new_in(

View File

@ -10,11 +10,7 @@
#[cfg(debug_assertions)]
use core::panic::Location;
use {
crate::{
world::{self, EffectStack, UnsizeF},
ReactiveWorld,
WeakTrigger,
},
crate::{world::EffectStack, ReactiveWorld, WeakTrigger},
core::{
fmt,
hash::Hash,
@ -91,7 +87,7 @@ impl<F, W: ReactiveWorld> Effect<F, W> {
#[track_caller]
pub fn new_in(run: F, world: W) -> Self
where
F: EffectRun + UnsizeF<W> + 'static,
F: EffectRun + Unsize<W::F> + 'static,
{
// Create the effect
let effect = Self::new_raw_in(run, world);
@ -128,7 +124,7 @@ impl<F, W: ReactiveWorld> Effect<F, W> {
#[track_caller]
pub fn try_new_in(run: F, world: W) -> Option<Self>
where
F: EffectRun + UnsizeF<W> + 'static,
F: EffectRun + Unsize<W::F> + 'static,
{
let effect = Self::new_in(run, world);
match effect.is_inert() {
@ -186,7 +182,7 @@ impl<F: ?Sized, W: ReactiveWorld> Effect<F, W> {
#[must_use]
pub fn deps_gatherer(&self) -> EffectDepsGatherer<W>
where
F: UnsizeF<W> + 'static,
F: Unsize<W::F> + 'static,
{
// Push the effect
W::EffectStack::push(self.downgrade());
@ -214,7 +210,7 @@ impl<F: ?Sized, W: ReactiveWorld> Effect<F, W> {
/// Removes any existing dependencies before running.
pub fn gather_dependencies<G, O>(&self, gather: G) -> O
where
F: UnsizeF<W> + 'static,
F: Unsize<W::F> + 'static,
G: FnOnce() -> O,
{
let _gatherer = self.deps_gatherer();
@ -224,7 +220,7 @@ impl<F: ?Sized, W: ReactiveWorld> Effect<F, W> {
/// Runs the effect
pub fn run(&self)
where
F: EffectRun + UnsizeF<W> + 'static,
F: EffectRun + Unsize<W::F> + 'static,
{
// If we're suppressed, don't do anything
// TODO: Should we clear our dependencies in this case?
@ -347,7 +343,7 @@ impl<F: ?Sized, W: ReactiveWorld> WeakEffect<F, W> {
/// Returns if the effect still existed
pub fn try_run(&self) -> bool
where
F: EffectRun + UnsizeF<W> + 'static,
F: EffectRun + Unsize<W::F> + 'static,
{
// Try to upgrade, else return that it was missing
let Some(effect) = self.upgrade() else {
@ -424,7 +420,7 @@ impl<W: ReactiveWorld> Drop for EffectDepsGatherer<'_, W> {
/// Returns the current running effect
// TODO: Move this elsewhere
#[must_use]
pub fn running<W: ReactiveWorld>() -> Option<WeakEffect<world::F<W>, W>> {
pub fn running<W: ReactiveWorld>() -> Option<WeakEffect<W::F, W>> {
<W>::EffectStack::top()
}

View File

@ -2,7 +2,7 @@
// Imports
use {
crate::{world::UnsizeF, Effect, EffectRun, ReactiveWorld, SignalBorrow, SignalWithDefaultImpl, Trigger},
crate::{Effect, EffectRun, ReactiveWorld, SignalBorrow, SignalWithDefaultImpl, Trigger},
core::{
fmt,
marker::{PhantomData, Unsize},
@ -42,7 +42,7 @@ impl<T, F, W: MemoWorld<T, F>> Memo<T, F, W> {
where
T: PartialEq + 'static,
F: Fn() -> T + 'static,
EffectFn<T, F, W>: UnsizeF<W>,
EffectFn<T, F, W>: Unsize<W::F>,
{
let value = IMut::<_, W>::new(None);
let effect = Effect::new_in(

View File

@ -5,7 +5,7 @@
// Imports
use {
crate::{effect, world, Effect, ReactiveWorld, WeakEffect},
crate::{effect, Effect, ReactiveWorld, WeakEffect},
core::{
fmt,
hash::{Hash, Hasher},
@ -24,7 +24,7 @@ use {
#[derive(Debug)]
pub struct Subscriber<W: ReactiveWorld> {
/// Effect
effect: WeakEffect<world::F<W>, W>,
effect: WeakEffect<W::F, W>,
}
impl<W: ReactiveWorld> Clone for Subscriber<W> {
@ -366,9 +366,9 @@ impl<W: ReactiveWorld> IntoSubscriber<W> for Subscriber<W> {
)]
impl<F, W> IntoSubscriber<W> for T<F, W>
where
F: ?Sized + core::marker::Unsize<world::F<W>>,
F: ?Sized + core::marker::Unsize<W::F>,
W: ReactiveWorld,
WeakEffect<F, W>: CoerceUnsized<WeakEffect<world::F<W>, W>>,
WeakEffect<F, W>: CoerceUnsized<WeakEffect<W::F, W>>,
{
#[track_caller]
fn into_subscriber(self) -> Subscriber<W> {

View File

@ -17,7 +17,7 @@ pub use self::effect_stack::{EffectStack, EffectStackGlobal, EffectStackThreadLo
// Imports
use {
crate::{effect, trigger, WeakTrigger},
crate::{effect, trigger, EffectRun, WeakTrigger},
core::{marker::Unsize, ops::CoerceUnsized},
dynatos_world::{IMut, Weak, World, WorldGlobal, WorldThreadLocal},
std::collections::{HashMap, HashSet},
@ -25,6 +25,9 @@ use {
/// Reactive world
pub trait ReactiveWorldInner: World {
/// Effect function
type F: ?Sized + EffectRun + Unsize<Self::F> + 'static;
/// Effect stack
type EffectStack: EffectStack<Self>;
}
@ -40,20 +43,17 @@ pub trait ReactiveWorldInner: World {
)]
pub trait ReactiveWorld = ReactiveWorldInner
where
Weak<effect::Inner<F<Self>, Self>, Self>: CoerceUnsized<Weak<effect::Inner<F<Self>, Self>, Self>>,
Weak<effect::Inner<<Self as ReactiveWorldInner>::F, Self>, Self>:
CoerceUnsized<Weak<effect::Inner<<Self as ReactiveWorldInner>::F, Self>, Self>>,
IMut<HashMap<crate::Subscriber<Self>, trigger::SubscriberInfo>, Self>: Sized,
IMut<HashSet<WeakTrigger<Self>>, Self>: Sized,
IMut<(), Self>: Sized;
impl ReactiveWorldInner for WorldThreadLocal {
type EffectStack = EffectStackThreadLocal;
type F = dyn EffectRun + 'static;
}
impl ReactiveWorldInner for WorldGlobal {
type EffectStack = EffectStackGlobal;
type F = dyn EffectRun + Send + Sync + 'static;
}
/// The effect stack function type of the world `W`
pub type F<W: ReactiveWorld> = <W::EffectStack as EffectStack<W>>::F;
/// `Unsize` into the effect stack function of the world `W`
pub trait UnsizeF<W: ReactiveWorld> = Unsize<F<W>>;

View File

@ -2,7 +2,7 @@
// Imports
use {
super::ReactiveWorld,
super::{ReactiveWorld, ReactiveWorldInner},
crate::{EffectRun, WeakEffect},
core::marker::Unsize,
dynatos_world::{IMut, IMutLike, WorldGlobal, WorldThreadLocal},
@ -11,20 +11,17 @@ use {
/// Effect stack
// TODO: Require `W: ReactiveWorld` once that doesn't result in a cycle overflow.
pub trait EffectStack<W>: Sized {
/// Effect function
type F: ?Sized + EffectRun + Unsize<Self::F> + 'static;
/// Pushes an effect to the stack.
fn push<F>(f: WeakEffect<F, W>)
where
F: ?Sized + Unsize<Self::F>,
F: ?Sized + Unsize<W::F>,
W: ReactiveWorld;
/// Pops an effect from the stack
fn pop();
/// Returns the top effect of the stack
fn top() -> Option<WeakEffect<Self::F, W>>
fn top() -> Option<WeakEffect<W::F, W>>
where
W: ReactiveWorld;
}
@ -41,11 +38,9 @@ static EFFECT_STACK_STD_RC: EffectStackImpl<dyn EffectRun, WorldThreadLocal> =
EffectStackImpl::<_, WorldThreadLocal>::new(vec![]);
impl EffectStack<WorldThreadLocal> for EffectStackThreadLocal {
type F = dyn EffectRun + 'static;
fn push<F>(f: WeakEffect<F, WorldThreadLocal>)
where
F: ?Sized + Unsize<Self::F>,
F: ?Sized + Unsize<<WorldThreadLocal as ReactiveWorldInner>::F>,
{
EFFECT_STACK_STD_RC.write().push(f);
}
@ -54,7 +49,7 @@ impl EffectStack<WorldThreadLocal> for EffectStackThreadLocal {
EFFECT_STACK_STD_RC.write().pop().expect("Missing added effect");
}
fn top() -> Option<WeakEffect<Self::F, WorldThreadLocal>> {
fn top() -> Option<WeakEffect<<WorldThreadLocal as ReactiveWorldInner>::F, WorldThreadLocal>> {
EFFECT_STACK_STD_RC.read().last().cloned()
}
}
@ -68,11 +63,9 @@ static EFFECT_STACK_STD_ARC: EffectStackImpl<dyn EffectRun + Send + Sync, WorldG
impl EffectStack<WorldGlobal> for EffectStackGlobal {
type F = dyn EffectRun + Send + Sync + 'static;
fn push<F>(f: WeakEffect<F, WorldGlobal>)
where
F: ?Sized + Unsize<Self::F>,
F: ?Sized + Unsize<<WorldGlobal as ReactiveWorldInner>::F>,
{
EFFECT_STACK_STD_ARC.write().push(f);
}
@ -81,7 +74,7 @@ impl EffectStack<WorldGlobal> for EffectStackGlobal {
EFFECT_STACK_STD_ARC.write().pop().expect("Missing added effect");
}
fn top() -> Option<WeakEffect<Self::F, WorldGlobal>> {
fn top() -> Option<WeakEffect<<WorldGlobal as ReactiveWorldInner>::F, WorldGlobal>> {
EFFECT_STACK_STD_ARC.read().last().cloned()
}
}