ori-core 0.1.0-alpha.1

Core library for Ori, a declarative UI framework for Rust.
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
use std::{
    cmp::Ordering,
    fmt::{Debug, Formatter},
    hash::{Hash, Hasher},
    ops::{Deref, DerefMut},
    panic::Location,
};

use crate::{CallbackEmitter, Lock, Lockable, Shared, Weak};

/// A read-only [`Signal`].
pub struct ReadSignal<T: ?Sized> {
    value: Lock<Shared<T>>,
    emitter: CallbackEmitter,
}

impl<T> ReadSignal<T> {
    /// Creates a new [`ReadSignal`] from a value.
    pub fn new(value: T) -> Self {
        Self::new_arc(Shared::new(value))
    }
}

impl<T: ?Sized> ReadSignal<T> {
    /// Creates a new [`ReadSignal`] from an [`Shared`].
    pub fn new_arc(value: Shared<T>) -> Self {
        Self {
            value: Lock::new(value),
            emitter: CallbackEmitter::new(),
        }
    }

    /// Gets the [`CallbackEmitter`] for this [`ReadSignal`].
    pub fn emitter(&self) -> &CallbackEmitter {
        &self.emitter
    }

    /// Tracks `self` in the currently running `effect`.
    pub fn track(&self) {
        self.emitter.track();
    }

    /// Gets the current value of `self`.
    ///
    /// This will track `self` in the currently running `effect`.
    pub fn get(&self) -> Shared<T> {
        self.emitter.track();
        self.get_untracked()
    }

    /// Gets the current value of `self` without tracking it.
    pub fn get_untracked(&self) -> Shared<T> {
        self.value.lock_mut().clone()
    }
}

impl<T: Clone> ReadSignal<T> {
    /// Returns a clone of the current value of `self`.
    ///
    /// This will track `self` in the currently running `effect`.
    pub fn cloned(&self) -> T {
        self.get().as_ref().clone()
    }

    /// Returns a clone of the current value of `self` without tracking it.
    pub fn cloned_untracked(&self) -> T {
        self.get_untracked().as_ref().clone()
    }
}

impl<T: Copy> ReadSignal<T> {
    /// Returns a copy to the current value of `self`.
    pub fn copied(&self) -> T {
        *self.get().as_ref()
    }

    /// Returns a copy to the current value of `self`.
    pub fn copied_untracked(&self) -> T {
        *self.get_untracked().as_ref()
    }
}

/// A [`Signal`] that can be written to.
///
/// This is a wrapper around [`ReadSignal`].
///
/// Signals are used to store state that can be read from and written to.
/// Using [`ReadSignal::get()`] and [`Signal::set()`]. Getting the value of a signal
/// will track the signal in the currently running `effect`, and setting the
/// value of a signal will trigger all the callbacks and effects, that are subscribed to
/// the signal.
///
/// # Example
/// ```
/// # use ori_core::*;
/// # Scope::immediate(|cx| {
/// // create a new signal
/// let signal = cx.signal(0);
///
/// // create a new effect
/// cx.effect(|| {
///     // this will be called when it's created
///     // and every time the signal is set
///     println!("signal value: {}", signal.get());
/// });
///
/// // set the signal to 1
/// // this will trigger the effect
/// signal.set(1);
/// # });
/// ```
pub struct Signal<T: ?Sized>(ReadSignal<T>);

impl<T> Signal<T> {
    /// Creates a new [`Signal`] from a value.
    pub fn new(value: T) -> Self {
        Self(ReadSignal::new(value))
    }

    /// Sets the value of `self`.
    #[track_caller]
    pub fn set(&self, value: T) {
        self.set_arc(Shared::new(value));
    }

    /// Sets the value of `self` without triggering the callbacks.
    pub fn set_silent(&self, value: T) {
        self.set_arc_silent(Shared::new(value));
    }
}

impl<T: ?Sized> Signal<T> {
    /// Creates a new [`Signal`] from an [`Shared`].
    pub fn new_arc(value: Shared<T>) -> Self {
        Self(ReadSignal::new_arc(value))
    }

    /// Sets the value of `self` to an [`Shared`].
    #[track_caller]
    pub fn set_arc(&self, value: Shared<T>) {
        self.set_arc_silent(value.clone());
        self.emit();
    }

    /// Sets the value of `self` to an [`Shared`] without triggering the callbacks.
    pub fn set_arc_silent(&self, value: Shared<T>) {
        *self.value.lock_mut() = value;
    }

    /// Emits the [`CallbackEmitter`] for this [`Signal`].
    #[track_caller]
    pub fn emit(&self) {
        let location = Location::caller();
        tracing::trace!("emitting signal at {}", location);

        self.emitter.emit(&());
    }
}

impl<T: ?Sized> Deref for Signal<T> {
    type Target = ReadSignal<T>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[derive(Debug)]
pub struct Modify<'a, T> {
    value: Option<T>,
    signal: &'a Signal<T>,
}

impl<'a, T> Deref for Modify<'a, T> {
    type Target = T;

    #[track_caller]
    fn deref(&self) -> &Self::Target {
        self.value.as_ref().unwrap()
    }
}

impl<'a, T> DerefMut for Modify<'a, T> {
    #[track_caller]
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.value.as_mut().unwrap()
    }
}

/// When the [`Modify`] is dropped, update the [`Signal`].
impl<'a, T> Drop for Modify<'a, T> {
    fn drop(&mut self) {
        self.signal.set(self.value.take().unwrap());
    }
}

impl<T: Clone> Signal<T> {
    /// Returns a [`Modify`] that can be used to modify the value of the [`Signal`].
    /// When the [`Modify`] is dropped, the [`Signal`] will be updated.
    pub fn modify(&self) -> Modify<'_, T> {
        Modify {
            value: Some(self.get().as_ref().clone()),
            signal: self,
        }
    }
}

/// A [`Signal`] that can be cloned.
pub struct SharedSignal<T: ?Sized>(Shared<Signal<T>>);

impl<T> SharedSignal<T> {
    pub fn new(value: T) -> Self {
        Self(Shared::new(Signal::new(value)))
    }
}

impl<T: ?Sized> SharedSignal<T> {
    pub fn new_arc(value: Shared<T>) -> Self {
        Self(Shared::new(Signal::new_arc(value)))
    }

    pub fn downgrade(&self) -> WeakSignal<T> {
        WeakSignal(Shared::downgrade(&self.0))
    }
}

impl<T: ?Sized> Clone for SharedSignal<T> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<T: ?Sized> Deref for SharedSignal<T> {
    type Target = Signal<T>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// A weak reference to a [`Signal`].
pub struct WeakSignal<T: ?Sized>(Weak<Signal<T>>);

impl<T> WeakSignal<T> {
    #[track_caller]
    pub fn set(&self, value: T) {
        if let Some(signal) = self.upgrade() {
            signal.set(value);
        }
    }

    pub fn set_silent(&self, value: T) {
        if let Some(signal) = self.upgrade() {
            signal.set_silent(value);
        }
    }
}

impl<T: ?Sized> WeakSignal<T> {
    pub fn upgrade(&self) -> Option<SharedSignal<T>> {
        self.0.upgrade().map(SharedSignal)
    }

    #[track_caller]
    pub fn set_arc(&self, value: Shared<T>) {
        if let Some(signal) = self.upgrade() {
            signal.set_arc(value);
        }
    }

    pub fn set_arc_silent(&self, value: Shared<T>) {
        if let Some(signal) = self.upgrade() {
            signal.set_arc_silent(value);
        }
    }

    pub fn get(&self) -> Option<Shared<T>> {
        Some(self.upgrade()?.get())
    }

    pub fn get_untracked(&self) -> Option<Shared<T>> {
        Some(self.upgrade()?.get_untracked())
    }
}

impl<T: ?Sized> Clone for WeakSignal<T> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<T: Debug + ?Sized> Debug for ReadSignal<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("ReadSignal").field(&self.get()).finish()
    }
}

impl<T: Debug + ?Sized> Debug for Signal<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("Signal").field(&self.get()).finish()
    }
}

impl<T: Debug + ?Sized> Debug for SharedSignal<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("SharedSignal").field(&self.get()).finish()
    }
}

impl<T: Debug + ?Sized> Debug for WeakSignal<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("WeakSignal").field(&self.get()).finish()
    }
}

impl<T: Default> Default for ReadSignal<T> {
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl<T: Default> Default for Signal<T> {
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl<T: Default> Default for SharedSignal<T> {
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl<T: PartialEq + ?Sized> PartialEq for ReadSignal<T> {
    fn eq(&self, other: &Self) -> bool {
        self.get() == other.get()
    }
}

impl<T: PartialEq + ?Sized> PartialEq for Signal<T> {
    fn eq(&self, other: &Self) -> bool {
        self.get() == other.get()
    }
}

impl<T: PartialEq + ?Sized> PartialEq for SharedSignal<T> {
    fn eq(&self, other: &Self) -> bool {
        self.get() == other.get()
    }
}

impl<T: PartialEq + ?Sized> PartialEq for WeakSignal<T> {
    fn eq(&self, other: &Self) -> bool {
        self.get() == other.get()
    }
}

impl<T: PartialEq + ?Sized> PartialEq<T> for ReadSignal<T> {
    fn eq(&self, other: &T) -> bool {
        self.get().as_ref() == other
    }
}

impl<T: PartialEq + ?Sized> PartialEq<T> for Signal<T> {
    fn eq(&self, other: &T) -> bool {
        self.get().as_ref() == other
    }
}

impl<T: PartialEq + ?Sized> PartialEq<T> for SharedSignal<T> {
    fn eq(&self, other: &T) -> bool {
        self.get().as_ref() == other
    }
}

impl<T: Eq + ?Sized> Eq for ReadSignal<T> {}
impl<T: Eq + ?Sized> Eq for Signal<T> {}
impl<T: Eq + ?Sized> Eq for SharedSignal<T> {}

impl<T: PartialOrd + ?Sized> PartialOrd for ReadSignal<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.get().partial_cmp(&other.get())
    }
}

impl<T: PartialOrd + ?Sized> PartialOrd for Signal<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.get().partial_cmp(&other.get())
    }
}

impl<T: PartialOrd + ?Sized> PartialOrd for SharedSignal<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.get().partial_cmp(&other.get())
    }
}

impl<T: PartialOrd + ?Sized> PartialOrd<T> for ReadSignal<T> {
    fn partial_cmp(&self, other: &T) -> Option<Ordering> {
        self.get().as_ref().partial_cmp(other)
    }
}

impl<T: PartialOrd + ?Sized> PartialOrd<T> for Signal<T> {
    fn partial_cmp(&self, other: &T) -> Option<Ordering> {
        self.get().as_ref().partial_cmp(other)
    }
}

impl<T: PartialOrd + ?Sized> PartialOrd<T> for SharedSignal<T> {
    fn partial_cmp(&self, other: &T) -> Option<Ordering> {
        self.get().as_ref().partial_cmp(other)
    }
}

impl<T: Ord + ?Sized> Ord for ReadSignal<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.get().cmp(&other.get())
    }
}

impl<T: Ord + ?Sized> Ord for Signal<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.get().cmp(&other.get())
    }
}

impl<T: Ord + ?Sized> Ord for SharedSignal<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.get().cmp(&other.get())
    }
}

impl<T: Hash + ?Sized> Hash for ReadSignal<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.get().hash(state);
    }
}

impl<T: Hash + ?Sized> Hash for Signal<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.get().hash(state);
    }
}

impl<T: Hash + ?Sized> Hash for SharedSignal<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.get().hash(state);
    }
}

impl<T: Hash + ?Sized> Hash for WeakSignal<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.get().hash(state);
    }
}