1use std::any::Any;
16
17use crate::memo::Memo;
18use crate::signal::ReadSignal;
19
20pub trait DepItem {
25 type Out: PartialEq + 'static;
27 fn current(&self) -> Self::Out;
29}
30
31impl<T: Clone + PartialEq + 'static> DepItem for T {
32 type Out = T;
33 fn current(&self) -> T {
34 self.clone()
35 }
36}
37
38impl<T: Clone + PartialEq + 'static> DepItem for ReadSignal<T> {
39 type Out = T;
40 fn current(&self) -> T {
41 self.get()
42 }
43}
44
45impl<T: Clone + PartialEq + 'static> DepItem for Memo<T> {
46 type Out = T;
47 fn current(&self) -> T {
48 self.get()
49 }
50}
51
52pub trait Deps {
54 type Out: PartialEq + 'static;
56 fn eval(&self) -> Self::Out;
60 fn is_auto_track() -> bool
62 where
63 Self: Sized,
64 {
65 false
66 }
67}
68
69impl Deps for () {
70 type Out = ();
71 fn eval(&self) {}
72
73 fn is_auto_track() -> bool {
74 true
75 }
76}
77
78macro_rules! impl_deps {
79 ($(($ty:ident, $idx:tt)),+ $(,)?) => {
80 impl<$($ty: DepItem),+> Deps for ($($ty,)+) {
81 type Out = ($(<$ty as DepItem>::Out,)+);
82 fn eval(&self) -> Self::Out {
83 ($(<$ty as DepItem>::current(&self.$idx),)+)
84 }
85 }
86 };
87}
88
89impl_deps!((A, 0));
90impl_deps!((A, 0), (B, 1));
91impl_deps!((A, 0), (B, 1), (C, 2));
92impl_deps!((A, 0), (B, 1), (C, 2), (D, 3));
93impl_deps!((A, 0), (B, 1), (C, 2), (D, 3), (E, 4));
94impl_deps!((A, 0), (B, 1), (C, 2), (D, 3), (E, 4), (F, 5));
95impl_deps!((A, 0), (B, 1), (C, 2), (D, 3), (E, 4), (F, 5), (G, 6));
96impl_deps!(
97 (A, 0),
98 (B, 1),
99 (C, 2),
100 (D, 3),
101 (E, 4),
102 (F, 5),
103 (G, 6),
104 (H, 7)
105);
106
107pub(crate) trait DepsAny {
109 fn eval(&self) -> Box<dyn Any>;
110 fn should_run(&self, prev: Option<&dyn Any>, next: &dyn Any) -> bool;
112}
113
114pub(crate) struct DepsBox<D>(pub D);
115
116impl<D: Deps> DepsAny for DepsBox<D> {
117 fn eval(&self) -> Box<dyn Any> {
118 Box::new(self.0.eval())
119 }
120
121 fn should_run(&self, prev: Option<&dyn Any>, next: &dyn Any) -> bool {
122 if D::is_auto_track() {
123 return true;
124 }
125 let Some(prev) = prev else { return true };
126 match (
127 prev.downcast_ref::<<D as Deps>::Out>(),
128 next.downcast_ref::<<D as Deps>::Out>(),
129 ) {
130 (Some(p), Some(n)) => p != n,
131 _ => true,
132 }
133 }
134}