Skip to content

Commit

Permalink
Dependency simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
bwoods committed Dec 7, 2024
1 parent 3918995 commit 930dcce
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 21 deletions.
25 changes: 11 additions & 14 deletions composable-architecture/src/dependencies/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ impl<T> Default for Dependency<T> {
/// [`deref`]: Dependency::deref
/// [`borrow`]: Dependency::borrow
impl<T> Dependency<T> {
/// 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()
}

Expand Down Expand Up @@ -256,6 +256,15 @@ impl<T> Dependency<T> {
{
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.
Expand Down Expand Up @@ -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<T: DependencyDefault> Deref for Dependency<T> {
Expand Down
2 changes: 1 addition & 1 deletion composable-architecture/src/effects/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Stream for Delay {
drop(state);

// Now that it has a Waker…
let scheduler = Dependency::<Reactor>::new();
let scheduler = Dependency::<Reactor>::get();
scheduler.add(instant, self.0.clone());

Poll::Pending
Expand Down
2 changes: 1 addition & 1 deletion composable-architecture/src/effects/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Task {
pub(crate) fn new<Action: 'static, S: Stream<Item = Action> + 'static>(stream: S) -> Self {
// Only called by “root” `Effects`, so it will be the same `Action` as used by the `Store`
let handle =
Dependency::<Executor<Result<Action, Thread>>>::new().and_then(
Dependency::<Executor<Result<Action, Thread>>>::get().and_then(
|executor| match executor.actions.upgrade() {
None => None,
Some(sender) => executor
Expand Down
2 changes: 1 addition & 1 deletion composable-architecture/src/store/testing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ where
inner.now = now;
drop(inner);

let timer = Dependency::<Reactor>::new();
let timer = Dependency::<Reactor>::get();

loop {
self.pool.run_until_stalled();
Expand Down
2 changes: 1 addition & 1 deletion composable-views/src/gesture/recognizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{Id, State};
///
#[inline(never)]
pub fn recognizer(id: Id, gesture: Gesture, location: Point, bounds: Bounds) -> Option<Response> {
let current = Dependency::<State>::new();
let current = Dependency::<State>::get();
let mut state = current.get();

let id = Some(id);
Expand Down
2 changes: 1 addition & 1 deletion composable-views/src/shapes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl<T: Path> View for Shape<T> {
bounds.min.y,
size.width,
size.height,
&Dependency::<Transform>::new().unwrap_or_default(),
&Dependency::<Transform>::get_or_default(),
onto,
);
}
Expand Down
4 changes: 2 additions & 2 deletions composable-views/src/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ impl View for Text<'_> {
}
}

let transform = Dependency::<Transform>::new();
let transform = Dependency::<Transform>::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,
};
Expand Down

0 comments on commit 930dcce

Please sign in to comment.