1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use TokenStream;
use ;
use ;
/// Wraps a `static mut` variable as a reactive global signal.
///
/// The `signal!` macro transforms a `static mut` variable into a `reactive_cache::Signal`,
/// and generates a **function with the same name as the variable** that returns a
/// `&'static Rc<Signal<T>>`. You can then call `.get()` to read the value or `.set(value)` to update it.
///
/// # Requirements
///
/// - Supports only `static mut` variables.
/// - Type `T` must implement `Eq`.
///
/// # Examples
///
/// ```rust
/// use reactive_cache::prelude::*;
/// use reactive_macros::signal;
///
/// signal!(static mut A: i32 = 10;);
///
/// assert_eq!(*A().get(), 10);
/// assert!(A().set(20));
/// assert_eq!(*A().get(), 20);
/// assert!(!A().set(20)); // No change
///
/// signal!(static mut B: String = "hello".to_string(););
///
/// assert_eq!(*B().get(), "hello");
/// assert!(B().set("world".to_string()));
/// assert_eq!(*B().get(), "world");
/// ```
///
/// # SAFETY
///
/// This macro wraps `static mut` variables internally, so it **is not thread-safe**.
/// It should be used only in single-threaded contexts.
///
/// # Warning
///
/// **Do not set any signal that is part of the same effect chain.**
///
/// Effects automatically run whenever one of their dependent signals changes.
/// If an effect modifies a signal that it (directly or indirectly) observes,
/// it creates a circular dependency. This can lead to:
/// - an infinite loop of updates, or
/// - conflicting updates that the system cannot resolve.
///
/// In the general case, it is impossible to automatically determine whether
/// such an effect will ever terminate—this is essentially a version of the
/// halting problem. Therefore, you must ensure manually that effects do not
/// update signals within their own dependency chain.
/// Turns a zero-argument function into a memoized, reactive computation.
///
/// The `#[memo]` attribute macro transforms a function into a static
/// `reactive_cache::Memo`, which:
/// 1. Computes the value the first time the function is called.
/// 2. Caches the result for future calls.
/// 3. Automatically tracks reactive dependencies if used inside `Signal` or other reactive contexts.
///
/// # Requirements
///
/// - The function must have **no parameters**.
/// - The function must return a value (`-> T`), which must implement `Clone`.
///
/// # Examples
///
/// ```rust
/// use reactive_cache::prelude::*;
/// use reactive_macros::memo;
///
/// #[memo]
/// pub fn get_number() -> i32 {
/// // The first call sets INVOKED to true
/// static mut INVOKED: bool = false;
/// assert!(!unsafe { INVOKED });
/// unsafe { INVOKED = true };
///
/// 42
/// }
///
/// #[memo]
/// pub fn get_string() -> String {
/// "Hello, World!".to_string()
/// }
///
/// fn main() {
/// // First call computes and caches the value
/// assert_eq!(get_number(), 42);
/// // Subsequent calls return the cached value without re-running the block
/// assert_eq!(get_number(), 42);
///
/// assert_eq!(get_string(), "Hello, World!");
/// }
/// ```
///
/// # SAFETY
///
/// This macro uses a `static mut` internally, so it **is not thread-safe**.
/// It is intended for single-threaded usage only. Accessing the memo from
/// multiple threads concurrently can cause undefined behavior.
/// Evaluates a zero-argument function and optionally reports when the value changes.
///
/// The `#[evaluate(print_fn)]` attribute macro transforms a function into a reactive
/// evaluator that:
/// 1. Computes the function result on each call.
/// 2. Compares it with the previously computed value.
/// 3. If the value is unchanged, calls the specified print function with a message.
///
/// # Requirements
///
/// - The function must have **no parameters**.
/// - The function must return a value (`-> T`), which must implement `Eq + Clone`.
/// - The print function (e.g., `print`) must be a callable accepting a `String`.
///
/// # Examples
///
/// ```rust
/// use reactive_cache::prelude::*;
/// use reactive_macros::evaluate;
///
/// fn print(msg: String) {
/// println!("{}", msg);
/// }
///
/// #[evaluate(print)]
/// pub fn get_number() -> i32 {
/// 42
/// }
///
/// fn main() {
/// // First call computes the value
/// assert_eq!(get_number(), 42);
/// // Second call compares with previous; prints message since value didn't change
/// assert_eq!(get_number(), 42);
/// }
/// ```
///
/// # SAFETY
///
/// This macro uses a `static mut` internally to store the previous value,
/// so it **is not thread-safe**. It should only be used in single-threaded contexts.