euv-core 0.48.1

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use super::*;

/// Manual `Clone` implementation so `Signal<T>` is always `Copy` regardless of `T`.
impl<T> Clone for Signal<T>
where
    T: 'static,
{
    /// Copies the lightweight 8-byte signal handle.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to copy.
    ///
    /// # Returns
    ///
    /// - `Self` - Identical signal handle.
    fn clone(&self) -> Self {
        *self
    }
}

/// Marker `Copy` implementation for `Signal<T>`.
impl<T> Copy for Signal<T> where T: 'static {}

/// Zero-allocation dangling `Default` handle for `Signal<T>`, used when initializing component prop descriptors.
impl<T> Default for Signal<T>
where
    T: 'static,
{
    /// Constructs an unallocated placeholder `Signal<T>` handle with `SignalId::default()`.
    ///
    /// # Returns
    ///
    /// - `Self` - Zero-cost placeholder signal handle.
    fn default() -> Self {
        Self {
            id: SignalId::default(),
            marker: PhantomData,
        }
    }
}

/// Equality comparison by underlying `SignalId`.
impl<T> PartialEq for Signal<T>
where
    T: 'static,
{
    /// Compares two signal handles by arena identifier.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Left-hand signal handle.
    /// - `&Self` - Right-hand signal handle.
    ///
    /// # Returns
    ///
    /// - `bool` - True if both handles reference the same arena slot and generation.
    fn eq(&self, other: &Self) -> bool {
        self.id() == other.id()
    }
}

/// Reflexive equality for `Signal<T>`.
impl<T> Eq for Signal<T> where T: 'static {}

/// Hash implementation based on underlying `SignalId`.
impl<T> Hash for Signal<T>
where
    T: 'static,
{
    /// Hashes the underlying arena signal identifier.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to hash.
    /// - `&mut H` - Hasher state.
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher,
    {
        self.id().hash(state);
    }
}

/// Core reactive operations for `Signal<T>`.
impl<T> Signal<T>
where
    T: 'static,
{
    /// Allocates a new reactive signal initialized with `initial_value`.
    ///
    /// # Arguments
    ///
    /// - `T` - Initial signal value.
    ///
    /// # Returns
    ///
    /// - `Self` - Copyable handle to the created signal.
    #[must_use]
    pub fn new(initial_value: T) -> Self {
        let id: SignalId = with_runtime(
            |rt: &mut ReactiveRuntime| rt.create_signal(initial_value),
            SignalId::default(),
        );
        Self {
            id,
            marker: PhantomData,
        }
    }

    /// Allocates a new reactive signal detached from any enclosing transient `Scope` so hub/store state survives UI rebuilds.
    ///
    /// # Arguments
    ///
    /// - `T` - Initial signal value.
    ///
    /// # Returns
    ///
    /// - `Self` - Copyable handle to the created detached signal.
    #[must_use]
    pub fn new_detached(initial_value: T) -> Self {
        let id: SignalId = with_runtime(
            |rt: &mut ReactiveRuntime| rt.create_detached_signal(initial_value),
            SignalId::default(),
        );
        Self {
            id,
            marker: PhantomData,
        }
    }

    /// Reconstructs a typed `Signal<T>` handle from an arena `SignalId`.
    ///
    /// # Arguments
    ///
    /// - `SignalId` - Arena signal identifier.
    ///
    /// # Returns
    ///
    /// - `Self` - Lightweight copyable signal handle.
    #[must_use]
    pub(crate) fn from_raw_id(id: SignalId) -> Self {
        Self {
            id,
            marker: PhantomData,
        }
    }

    /// Returns the underlying `SignalId`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle.
    ///
    /// # Returns
    ///
    /// - `SignalId` - Underlying arena identifier.
    #[must_use]
    #[inline(always)]
    pub fn id(&self) -> SignalId {
        let Self { id, .. } = *self;
        id
    }

    /// Returns whether this signal currently has at least one active downstream subscriber.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to inspect.
    ///
    /// # Returns
    ///
    /// - `bool` - True when the signal is alive and observed by at least one memo or effect.
    #[must_use]
    #[inline(always)]
    pub fn has_subscribers(&self) -> bool {
        let id: SignalId = self.id();
        with_runtime_ref(|rt: &ReactiveRuntime| rt.signal_has_subscribers(id), false)
    }

    /// Returns the number of active downstream subscribers observing this signal in O(1).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to inspect.
    ///
    /// # Returns
    ///
    /// - `usize` - Number of live memo or effect subscribers, or `0` if disposed.
    #[must_use]
    #[inline(always)]
    pub fn subscriber_count(&self) -> usize {
        let id: SignalId = self.id();
        with_runtime_ref(|rt: &ReactiveRuntime| rt.signal_subscriber_count(id), 0)
    }

    /// Returns whether this live signal stores its value inline inside the 24-byte SBO buffer without heap allocation.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to inspect.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when alive and stored inline in SBO storage.
    #[must_use]
    #[inline(always)]
    pub fn is_inline(&self) -> bool {
        let id: SignalId = self.id();
        with_runtime_ref(|rt: &ReactiveRuntime| rt.signal_is_inline(id), false)
    }

    /// Records a dependency on this signal in the active observer without cloning its value.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to track.
    #[inline(always)]
    pub fn track(&self) {
        track_or_defer_dependency(DependencyId::Signal(self.id()));
    }

    /// Reads the current value by cloning and tracks the active observer dependency.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to read.
    ///
    /// # Returns
    ///
    /// - `Option<T>` - Current value if the signal is still alive.
    #[must_use]
    #[inline(always)]
    pub fn try_get(&self) -> Option<T>
    where
        T: Clone,
    {
        let id: SignalId = self.id();
        if ref_borrow_depth() > 0 || !is_tracking_active() {
            return read_signal_with_ref::<T, _, T>(id, true, |val: &T| val.clone());
        }
        with_runtime(
            |rt: &mut ReactiveRuntime| rt.read_signal_cloned::<T>(id),
            None,
        )
    }

    /// Reads the current value, returning `T::default()` if the signal was disposed.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to read.
    ///
    /// # Returns
    ///
    /// - `T` - Current value or default fallback.
    #[must_use]
    #[inline(always)]
    pub fn get(&self) -> T
    where
        T: Clone + Default,
    {
        self.try_get().unwrap_or_default()
    }

    /// Reads the current value without tracking any reactive dependency.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to inspect.
    ///
    /// # Returns
    ///
    /// - `Option<T>` - Current value if alive.
    #[must_use]
    #[inline(always)]
    pub fn try_get_untracked(&self) -> Option<T>
    where
        T: Clone,
    {
        let id: SignalId = self.id();
        with_runtime_ref(
            |rt: &ReactiveRuntime| rt.read_signal_untracked::<T>(id),
            None,
        )
    }

    /// Reads the current value untracked, returning `T::default()` if disposed.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to inspect.
    ///
    /// # Returns
    ///
    /// - `T` - Current value or default fallback.
    #[must_use]
    #[inline(always)]
    pub fn get_untracked(&self) -> T
    where
        T: Clone + Default,
    {
        self.try_get_untracked().unwrap_or_default()
    }

    /// Applies `f` directly to a shared borrowed `&T` inside the arena without cloning, returning `None` if disposed or mutably borrowed.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to borrow.
    /// - `impl FnOnce(&T) -> R` - Read-only closure receiving `&T` (nested `with_ref` calls are supported).
    ///
    /// # Returns
    ///
    /// - `Option<R>` - Closure return value when the signal is alive and readable.
    #[inline]
    pub fn try_with_ref<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R> {
        read_signal_with_ref::<T, _, R>(self.id(), true, f)
    }

    /// Applies `f` directly to a shared borrowed `&T` without tracking dependencies or cloning, returning `None` if disposed or mutably borrowed.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to borrow untracked.
    /// - `impl FnOnce(&T) -> R` - Read-only closure receiving `&T`.
    ///
    /// # Returns
    ///
    /// - `Option<R>` - Closure return value when the signal is alive and readable.
    #[inline]
    pub fn try_with_ref_untracked<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R> {
        read_signal_with_ref::<T, _, R>(self.id(), false, f)
    }

    /// Applies `f` directly to a shared borrowed `&T` inside the arena without cloning and tracks the active observer dependency.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to borrow.
    /// - `impl FnOnce(&T) -> R` - Read-only closure receiving `&T` (nested read-only `with_ref` calls are supported; do not mutate signals inside `f`).
    ///
    /// # Returns
    ///
    /// - `R` - Value returned by `f`.
    #[inline]
    pub fn with_ref<R>(&self, f: impl FnOnce(&T) -> R) -> R {
        self.try_with_ref(f).unwrap()
    }

    /// Applies `reader` to a cloned snapshot outside any `RefCell` borrow so re-entrant mutating callbacks remain safe.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to read.
    /// - `F` - Reader closure receiving `&T`.
    ///
    /// # Returns
    ///
    /// - `Option<R>` - Closure return value if the signal is alive.
    #[inline]
    pub fn with<F, R>(&self, reader: F) -> Option<R>
    where
        T: Clone,
        F: FnOnce(&T) -> R,
    {
        let snapshot: T = self.try_get()?;
        Some(reader(&snapshot))
    }

    /// Replaces the stored value and flushes downstream reactive effects.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to write.
    /// - `T` - Replacement value.
    ///
    /// # Returns
    ///
    /// - `bool` - True when the signal was alive and updated.
    #[inline(always)]
    pub fn set(&self, value: T) -> bool {
        let id: SignalId = self.id();
        let (updated, should_flush): (bool, bool) = with_runtime(
            |rt: &mut ReactiveRuntime| rt.write_signal(id, value),
            (false, false),
        );
        if should_flush {
            flush_effects();
        }
        updated
    }

    /// Replaces the stored value only when `value` differs from the current stored value.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to write.
    /// - `T` - Candidate value.
    ///
    /// # Returns
    ///
    /// - `bool` - True when the value changed and downstream effects were scheduled.
    #[inline(always)]
    pub fn set_if_changed(&self, value: T) -> bool
    where
        T: PartialEq,
    {
        let id: SignalId = self.id();
        let (updated, should_flush): (bool, bool) = with_runtime(
            |rt: &mut ReactiveRuntime| rt.write_signal_if_changed(id, value),
            (false, false),
        );
        if should_flush {
            flush_effects();
        }
        updated
    }

    /// Mutates the signal value in place outside the runtime borrow without cloning and flushes effects.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to update.
    /// - `F` - Mutation closure receiving `&mut T`.
    ///
    /// # Returns
    ///
    /// - `bool` - True when the signal was alive and updated.
    #[inline]
    pub fn update<F>(&self, updater: F) -> bool
    where
        T: Clone,
        F: FnOnce(&mut T),
    {
        let id: SignalId = self.id();
        let Some(mut slot) = with_runtime(
            |rt: &mut ReactiveRuntime| rt.take_signal_slot::<T>(id),
            None,
        ) else {
            return false;
        };
        if let Some(current) = slot.downcast_mut::<T>() {
            updater(current);
        }
        let (updated, should_flush): (bool, bool) = with_runtime(
            |rt: &mut ReactiveRuntime| rt.commit_signal_slot(id, slot),
            (false, false),
        );
        if should_flush {
            flush_effects();
        }
        updated
    }

    /// Disposes this signal, unlinks its edges in O(deg), and drops its payload outside the runtime borrow.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Signal handle to dispose.
    pub fn dispose(&self) {
        let id: SignalId = self.id();
        let dropped: SignalSlot = with_runtime(
            |rt: &mut ReactiveRuntime| rt.dispose_signal(id),
            SignalSlot::empty(),
        );
        drop(dropped);
    }
}