diff --git a/dynatos-reactive/src/effect.rs b/dynatos-reactive/src/effect.rs index e5e8920..3c7d7d8 100644 --- a/dynatos-reactive/src/effect.rs +++ b/dynatos-reactive/src/effect.rs @@ -251,24 +251,18 @@ mod test { std::cell::OnceCell, }; - /// Leaks a value and returns a `&'static T` - /// - /// This is useful because `&'static T` is `Copy`, - /// so we don't need to worry about cloning `Rc`s to - /// pass variables to effects. - fn leaked(value: T) -> &'static T { - Box::leak(Box::new(value)) - } - /// Ensures the function returned by `Effect::running` is the same as the future being run. #[test] fn running() { // Create an effect, and save the running effect within it to `running`. - let running = self::leaked(OnceCell::new()); - let effect = Effect::new(move || { - running - .set(effect::running().expect("Future wasn't running")) - .expect("Unable to set running effect"); + let running = Rc::new(OnceCell::new()); + let effect = Effect::new({ + let running = Rc::clone(&running); + move || { + running + .set(effect::running().expect("Future wasn't running")) + .expect("Unable to set running effect"); + } }); // Then ensure the running effect is the same as the one created. @@ -286,26 +280,33 @@ mod test { fn running_stacked() { // Create 2 stacked effects, saving the running within each to `running1` and `running2`. // `running1` contains the top-level effect, while `running2` contains the inner one. - let running_top = self::leaked(OnceCell::new()); - let running_bottom = self::leaked(OnceCell::new()); - let effect = Effect::new(move || { - running_top - .set(effect::running().expect("Future wasn't running")) - .expect("Unable to set running effect"); - - let effect = Effect::new(move || { - running_bottom + let running_top = Rc::new(OnceCell::new()); + let running_bottom = Rc::new(OnceCell::new()); + let effect = Effect::new({ + let running_top = Rc::clone(&running_top); + let running_bottom = Rc::clone(&running_bottom); + move || { + running_top .set(effect::running().expect("Future wasn't running")) .expect("Unable to set running effect"); - }); - // Then ensure the bottom-level running effect is the same as the one created. - let running_bottom = running_bottom - .get() - .expect("Running effect missing") - .upgrade() - .expect("Running effect was dropped"); - assert_eq!(effect, running_bottom); + let effect = Effect::new({ + let running_bottom = Rc::clone(&running_bottom); + move || { + running_bottom + .set(effect::running().expect("Future wasn't running")) + .expect("Unable to set running effect"); + } + }); + + // Then ensure the bottom-level running effect is the same as the one created. + let running_bottom = running_bottom + .get() + .expect("Running effect missing") + .upgrade() + .expect("Running effect was dropped"); + assert_eq!(effect, running_bottom); + } }); // Then ensure the top-level running effect is the same as the one created.