diff --git a/composable-architecture/src/dependencies/values.rs b/composable-architecture/src/dependencies/values.rs index 106701c0..fc97ef88 100644 --- a/composable-architecture/src/dependencies/values.rs +++ b/composable-architecture/src/dependencies/values.rs @@ -34,9 +34,9 @@ impl Default for Dependency { /// [`deref`]: Dependency::deref /// [`borrow`]: Dependency::borrow impl Dependency { - /// Creates a optional reference to the dependency of type `T`. + /// Retrieves the (optional) reference to the dependency of type `T`. #[inline] - pub fn new() -> Self { + pub fn get() -> Self { Self::default() } @@ -256,6 +256,15 @@ impl Dependency { { self.as_deref().cloned() } + + /// Returns a copy of the dependency [`Some`] value or a default. + #[inline(always)] + pub fn get_or_default() -> T + where + T: Copy + Default, + { + Self::get().copied().unwrap_or_default() + } } /// The default value for a dependency. @@ -303,18 +312,6 @@ Either register the dependency on the TestStore or use with_dependency(…) with self.as_deref().unwrap() }) } - - /// ## SAFETY - /// A `DependencyDefault`, once fetched, will last for the life of the process. - /// - /// Holding this reference is not advised as it will not reflect further overrides of this dependency. - #[inline(always)] - pub fn as_ref() -> &'static T { - #[allow(unsafe_code)] - unsafe { - std::mem::transmute(Self::default().get_or_insert_default()) - } - } } impl Deref for Dependency { diff --git a/composable-architecture/src/effects/delay.rs b/composable-architecture/src/effects/delay.rs index 63929229..c282f26a 100644 --- a/composable-architecture/src/effects/delay.rs +++ b/composable-architecture/src/effects/delay.rs @@ -43,7 +43,7 @@ impl Stream for Delay { drop(state); // Now that it has a Waker… - let scheduler = Dependency::::new(); + let scheduler = Dependency::::get(); scheduler.add(instant, self.0.clone()); Poll::Pending diff --git a/composable-architecture/src/effects/task.rs b/composable-architecture/src/effects/task.rs index 2c5bf4f6..65e3f38e 100644 --- a/composable-architecture/src/effects/task.rs +++ b/composable-architecture/src/effects/task.rs @@ -37,7 +37,7 @@ impl Task { pub(crate) fn new + 'static>(stream: S) -> Self { // Only called by “root” `Effects`, so it will be the same `Action` as used by the `Store` let handle = - Dependency::>>::new().and_then( + Dependency::>>::get().and_then( |executor| match executor.actions.upgrade() { None => None, Some(sender) => executor diff --git a/composable-architecture/src/store/testing/mod.rs b/composable-architecture/src/store/testing/mod.rs index 9f23b1a9..0ca58e42 100644 --- a/composable-architecture/src/store/testing/mod.rs +++ b/composable-architecture/src/store/testing/mod.rs @@ -69,7 +69,7 @@ where inner.now = now; drop(inner); - let timer = Dependency::::new(); + let timer = Dependency::::get(); loop { self.pool.run_until_stalled(); diff --git a/composable-views/src/gesture/recognizer.rs b/composable-views/src/gesture/recognizer.rs index fd7414cd..94931f2d 100644 --- a/composable-views/src/gesture/recognizer.rs +++ b/composable-views/src/gesture/recognizer.rs @@ -6,7 +6,7 @@ use super::{Id, State}; /// #[inline(never)] pub fn recognizer(id: Id, gesture: Gesture, location: Point, bounds: Bounds) -> Option { - let current = Dependency::::new(); + let current = Dependency::::get(); let mut state = current.get(); let id = Some(id); diff --git a/composable-views/src/shapes/mod.rs b/composable-views/src/shapes/mod.rs index 247f660b..b271c5f2 100644 --- a/composable-views/src/shapes/mod.rs +++ b/composable-views/src/shapes/mod.rs @@ -150,7 +150,7 @@ impl View for Shape { bounds.min.y, size.width, size.height, - &Dependency::::new().unwrap_or_default(), + &Dependency::::get_or_default(), onto, ); } diff --git a/composable-views/src/text/mod.rs b/composable-views/src/text/mod.rs index 8b928b03..79bb6273 100644 --- a/composable-views/src/text/mod.rs +++ b/composable-views/src/text/mod.rs @@ -118,12 +118,12 @@ impl View for Text<'_> { } } - let transform = Dependency::::new(); + let transform = Dependency::::get_or_default(); let mut builder = Builder { transform: Transform::scale(self.scale, -self.scale) // negate y-axis .then_translate((0.0, self.ascender()).into()) // font baseline .then_translate(bounds.min.to_vector()) // start position, - .then(&transform.unwrap_or_default()), + .then(&transform), rgba: self.rgba, output, };