sycamore-reactive 0.9.3

Reactive primitives for Sycamore
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
//! Reactive signals.

use std::cell::{Ref, RefMut};
use std::fmt;
use std::fmt::Formatter;
use std::hash::Hash;
use std::marker::PhantomData;
use std::ops::{AddAssign, Deref, DivAssign, MulAssign, RemAssign, SubAssign};

use slotmap::Key;
use smallvec::SmallVec;

use crate::*;

/// A read-only reactive value.
///
/// Unlike the difference between Rust's shared and mutable-references (`&T` and `&mut`), the
/// underlying data is not immutable. The data can be updated with the corresponding [`Signal`]
/// (which has mutable access) and will show up in the `ReadSignal` as well.
///
/// A `ReadSignal` can be simply obtained by dereferencing a [`Signal`]. In fact, every [`Signal`]
/// is a `ReadSignal` with additional write abilities!
///
/// # Example
/// ```
/// # use sycamore_reactive::*;
/// # create_root(|| {
/// let signal: Signal<i32> = create_signal(123);
/// let read_signal: ReadSignal<i32> = *signal;
/// assert_eq!(read_signal.get(), 123);
/// signal.set(456);
/// assert_eq!(read_signal.get(), 456);
/// // read_signal.set(789); // <-- This is not allowed!
/// # });
/// ```
///
/// See [`create_signal`] for more information.
pub struct ReadSignal<T: 'static> {
    pub(crate) id: NodeId,
    root: &'static Root,
    /// Keep track of where the signal was created for diagnostics.
    /// This is also stored in the Node but we want to have access to this when accessing a
    /// disposed node so we store it here as well.
    #[cfg(debug_assertions)]
    created_at: &'static std::panic::Location<'static>,
    _phantom: PhantomData<T>,
}

/// A reactive value that can be read and written to.
///
/// This is the writable version of [`ReadSignal`].
///
/// See [`create_signal`] for more information.
pub struct Signal<T: 'static>(pub(crate) ReadSignal<T>);

/// Create a new [`Signal`].
///
/// Signals are reactive atoms, pieces of state that can be read and written to and which will
/// automatically update anything which depend on them.
///
/// # Usage
/// The simplest way to use a signal is by using [`.get()`](ReadSignal::get) and
/// [`.set(...)`](Signal::set). However, this only works if the value implements [`Copy`]. If
/// we wanted to store something that doesn't implement [`Copy`] but implements [`Clone`] instead,
/// say a [`String`], we can use [`.get_clone()`](ReadSignal::get_clone) which will automatically
/// clone the value for us.
///
/// ```rust
/// # use sycamore_reactive::*;
/// # create_root(|| {
/// let signal = create_signal(1);
/// signal.get(); // Should return 1.
/// signal.set(2);
/// signal.get(); // Should return 2.
/// # });
/// ```
///
/// There are many other ways of getting and setting signals, such as
/// [`.with(...)`](ReadSignal::with) and [`.update(...)`](Signal::update) which can access the
/// signal even if it does not implement [`Clone`] or if you simply don't want to pay the
/// performance overhead of cloning your value every time you read it.
///
/// # Reactivity
/// What makes signals so powerful, as opposed to some other wrapper type like
/// [`RefCell`](std::cell::RefCell) is the automatic dependency tracking. This means that accessing
/// a signal will automatically add it as a dependency in certain contexts (such as inside a
/// [`create_memo`](crate::create_memo)) which allows us to update related state whenever the signal
/// is changed.
///
/// ```rust
/// # use sycamore_reactive::*;
/// # create_root(|| {
/// let signal = create_signal(1);
/// // Note that we are accessing signal inside a closure in the line below. This will cause it to
/// // be automatically tracked and update our double value whenever signal is changed.
/// let double = create_memo(move || signal.get() * 2);
/// double.get(); // Should return 2.
/// signal.set(2);
/// double.get(); // Should return 4. Notice how this value was updated automatically when we
///               // modified signal. This way, we can rest assured that all our state will be
///               // consistent at all times!
/// # });
/// ```
///
/// # Ownership
/// Signals are always associated with a reactive node. This is what performs the memory management
/// for the actual value of the signal. What is returned from this function is just a
/// handle/reference to the signal allocated in the reactive node. This allows us to freely copy
/// this handle around and use it in closures and event handlers without worrying about ownership of
/// the signal.
///
/// This is why in the above example, we could access `signal` even after it was moved in to the
/// closure of the `create_memo`.
#[cfg_attr(debug_assertions, track_caller)]
pub fn create_signal<T>(value: T) -> Signal<T> {
    let signal = create_empty_signal();
    signal.get_mut().value = Some(Box::new(value));
    signal
}

/// Creates a new [`Signal`] with the `value` field set to `None`.
#[cfg_attr(debug_assertions, track_caller)]
pub(crate) fn create_empty_signal<T>() -> Signal<T> {
    let root = Root::global();
    let id = root.nodes.borrow_mut().insert(ReactiveNode {
        value: None,
        callback: None,
        children: Vec::new(),
        parent: root.current_node.get(),
        dependents: Vec::new(),
        dependencies: SmallVec::new(),
        cleanups: Vec::new(),
        context: Vec::new(),
        state: NodeState::Clean,
        mark: Mark::None,
        #[cfg(debug_assertions)]
        created_at: std::panic::Location::caller(),
    });
    // Add the signal to the parent's `children` list.
    let current_node = root.current_node.get();
    if !current_node.is_null() {
        root.nodes.borrow_mut()[current_node].children.push(id);
    }

    Signal(ReadSignal {
        id,
        root,
        #[cfg(debug_assertions)]
        created_at: std::panic::Location::caller(),
        _phantom: PhantomData,
    })
}

impl<T> ReadSignal<T> {
    /// Get a immutable reference to the underlying node.
    #[cfg_attr(debug_assertions, track_caller)]
    pub(crate) fn get_ref(self) -> Ref<'static, ReactiveNode> {
        Ref::map(
            self.root
                .nodes
                .try_borrow()
                .expect("cannot read signal while updating"),
            |nodes| match nodes.get(self.id) {
                Some(node) => node,
                None => panic!("{}", self.get_disposed_panic_message()),
            },
        )
    }

    /// Get a mutable reference to the underlying node.
    #[cfg_attr(debug_assertions, track_caller)]
    pub(crate) fn get_mut(self) -> RefMut<'static, ReactiveNode> {
        RefMut::map(
            self.root
                .nodes
                .try_borrow_mut()
                .expect("cannot update signal while reading"),
            |nodes| match nodes.get_mut(self.id) {
                Some(node) => node,
                None => panic!("{}", self.get_disposed_panic_message()),
            },
        )
    }

    /// Returns `true` if the signal is still alive, i.e. has not yet been disposed.
    pub fn is_alive(self) -> bool {
        self.root.nodes.borrow().get(self.id).is_some()
    }

    /// Disposes the signal, i.e. frees up the memory held on by this signal. Accessing a signal
    /// after it has been disposed immediately causes a panic.
    pub fn dispose(self) {
        NodeHandle(self.id, self.root).dispose();
    }

    fn get_disposed_panic_message(self) -> String {
        #[cfg(not(debug_assertions))]
        return "signal was disposed".to_string();

        #[cfg(debug_assertions)]
        return format!("signal was disposed. Created at {}", self.created_at);
    }

    /// Get the value of the signal without tracking it. The type must implement [`Copy`]. If this
    /// is not the case, use [`ReadSignal::get_clone_untracked`] or [`ReadSignal::with_untracked`]
    /// instead.
    ///
    /// # Example
    /// ```
    /// # use sycamore_reactive::*;
    /// # create_root(|| {
    /// let state = create_signal(0);
    /// // Note that we have used `get_untracked` here so the signal is not actually being tracked
    /// // by the memo.
    /// let doubled = create_memo(move || state.get_untracked() * 2);
    /// state.set(1);
    /// assert_eq!(doubled.get(), 0);
    /// # });
    /// ```
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn get_untracked(self) -> T
    where
        T: Copy,
    {
        self.with_untracked(|value| *value)
    }

    /// Get the value of the signal without tracking it. The type is [`Clone`]-ed automatically.
    ///
    /// This is the cloned equivalent of [`ReadSignal::get_untracked`].
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn get_clone_untracked(self) -> T
    where
        T: Clone,
    {
        self.with_untracked(Clone::clone)
    }

    /// Get the value of the signal. The type must implement [`Copy`]. If this is not the case, use
    /// [`ReadSignal::get_clone_untracked`] or [`ReadSignal::with_untracked`] instead.
    ///
    /// When called inside a reactive scope, the signal will be automatically tracked.
    ///
    /// # Example
    /// ```
    /// # use sycamore_reactive::*;
    /// # create_root(|| {
    /// let state = create_signal(0);
    /// assert_eq!(state.get(), 0);
    ///
    /// state.set(1);
    /// assert_eq!(state.get(), 1);
    ///
    /// // The signal is automatically tracked in the line below.
    /// let doubled = create_memo(move || state.get());
    /// # });
    /// ```
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn get(self) -> T
    where
        T: Copy,
    {
        self.track();
        self.get_untracked()
    }

    /// Get the value of the signal. The type is [`Clone`]-ed automatically.
    ///
    /// When called inside a reactive scope, the signal will be automatically tracked.
    ///
    /// If the value implements [`Copy`], you should use [`ReadSignal::get`] instead.
    ///
    /// # Example
    /// ```
    /// # use sycamore_reactive::*;
    /// # create_root(|| {
    /// let greeting = create_signal("Hello".to_string());
    /// assert_eq!(greeting.get_clone(), "Hello".to_string());
    ///
    /// // The signal is automatically tracked in the line below.
    /// let hello_world = create_memo(move || format!("{} World!", greeting.get_clone()));
    /// assert_eq!(hello_world.get_clone(), "Hello World!");
    ///
    /// greeting.set("Goodbye".to_string());
    /// assert_eq!(greeting.get_clone(), "Goodbye".to_string());
    /// assert_eq!(hello_world.get_clone(), "Goodbye World!");
    /// # });
    /// ```
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn get_clone(self) -> T
    where
        T: Clone,
    {
        self.track();
        self.get_clone_untracked()
    }

    /// Get a value from the signal without tracking it.
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn with_untracked<U>(self, f: impl FnOnce(&T) -> U) -> U {
        self.root.ensure_node_is_clean(self.id);

        let node = self.get_ref();
        debug_assert!(
            node.state == NodeState::Clean,
            "state should always be latest value"
        );
        let value = node
            .value
            .as_ref()
            .expect("cannot read signal while updating");

        f(value.downcast_ref().expect("wrong signal type"))
    }

    /// Get a value from the signal.
    ///
    /// When called inside a reactive scope, the signal will be automatically tracked.
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn with<U>(self, f: impl FnOnce(&T) -> U) -> U {
        self.track();
        self.with_untracked(f)
    }

    /// Creates a new [memo](create_memo) from this signal and a function. The resulting memo will
    /// be created in the current reactive scope.
    ///
    /// # Example
    /// ```
    /// # use sycamore_reactive::*;
    /// # create_root(|| {
    /// let state = create_signal(0);
    /// let doubled = state.map(|val| *val * 2);
    /// assert_eq!(doubled.get(), 0);
    /// state.set(1);
    /// assert_eq!(doubled.get(), 2);
    /// # });
    /// ```
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn map<U>(self, mut f: impl FnMut(&T) -> U + 'static) -> ReadSignal<U> {
        create_memo(move || self.with(&mut f))
    }

    /// Track the signal in the current reactive scope. This is done automatically when calling
    /// [`ReadSignal::get`] and other similar methods.
    ///
    /// # Example
    /// ```
    /// # use sycamore_reactive::*;
    /// # create_root(|| {
    /// let state = create_signal(0);
    /// create_effect(move || {
    ///     state.track(); // Track the signal without getting its value.
    ///     println!("Yipee!");
    /// });
    /// state.set(1); // Prints "Yipee!"
    /// # });
    /// ```
    pub fn track(self) {
        if let Some(tracker) = &mut *self.root.tracker.borrow_mut() {
            tracker.dependencies.push(self.id);
        }
    }
}

impl<T> Signal<T> {
    /// Silently set a new value for the signal. This will not trigger any updates in dependent
    /// signals. As such, this is generally not recommended as it can easily lead to state
    /// inconsistencies.
    ///
    /// # Example
    /// ```
    /// # use sycamore_reactive::*;
    /// # create_root(|| {
    /// let state = create_signal(0);
    /// let doubled = create_memo(move || state.get() * 2);
    /// assert_eq!(doubled.get(), 0);
    /// state.set_silent(1);
    /// assert_eq!(doubled.get(), 0); // We now have inconsistent state!
    /// # });
    /// ```
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn set_silent(self, new: T) {
        self.replace_silent(new);
    }

    /// Set a new value for the signal and automatically update any dependents.
    ///
    /// # Example
    /// ```
    /// # use sycamore_reactive::*;
    /// # create_root(|| {
    /// let state = create_signal(0);
    /// let doubled = create_memo(move || state.get() * 2);
    /// assert_eq!(doubled.get(), 0);
    /// state.set(1);
    /// assert_eq!(doubled.get(), 2);
    /// # });
    /// ```
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn set(self, new: T) {
        self.replace(new);
    }

    /// Silently set a new value for the signal and return the previous value.
    ///
    /// This is the silent version of [`Signal::replace`].
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn replace_silent(self, new: T) -> T {
        self.update_silent(|val| std::mem::replace(val, new))
    }

    /// Set a new value for the signal and return the previous value.
    ///
    /// # Example
    /// ```
    /// # use sycamore_reactive::*;
    /// # create_root(|| {
    /// let state = create_signal(123);
    /// let prev = state.replace(456);
    /// assert_eq!(state.get(), 456);
    /// assert_eq!(prev, 123);
    /// # });
    /// ```
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn replace(self, new: T) -> T {
        self.update(|val| std::mem::replace(val, new))
    }

    /// Silently gets the value of the signal and sets the new value to the default value.
    ///
    /// This is the silent version of [`Signal::take`].
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn take_silent(self) -> T
    where
        T: Default,
    {
        self.replace_silent(T::default())
    }

    /// Gets the value of the signal and sets the new value to the default value.
    ///
    /// # Example
    /// ```
    /// # use sycamore_reactive::*;
    /// # create_root(|| {
    /// let state = create_signal(Some(123));
    /// let prev = state.take();
    /// assert_eq!(state.get(), None);
    /// assert_eq!(prev, Some(123));
    /// # });
    /// ```
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn take(self) -> T
    where
        T: Default,
    {
        self.replace(T::default())
    }

    /// Update the value of the signal silently. This will not trigger any updates in dependent
    /// signals. As such, this is generally not recommended as it can easily lead to state
    /// inconsistencies.
    ///
    /// This is the silent version of [`Signal::update`].
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn update_silent<U>(self, f: impl FnOnce(&mut T) -> U) -> U {
        let mut value = self
            .get_mut()
            .value
            .take()
            .expect("cannot update signal while reading");
        let ret = f(value.downcast_mut().expect("wrong signal type"));
        self.get_mut().value = Some(value);
        ret
    }

    /// Update the value of the signal and automatically update any dependents.
    ///
    /// Using this has the advantage of not needing to clone the value when updating it, especially
    /// with types that do not implement `Copy` where cloning can be expensive, or for types that
    /// do not implement `Clone` at all.
    ///
    /// # Example
    /// ```
    /// # use sycamore_reactive::*;
    /// # create_root(|| {
    /// let state = create_signal("Hello".to_string());
    /// state.update(|val| val.push_str(" Sycamore!"));
    /// assert_eq!(state.get_clone(), "Hello Sycamore!");
    /// # });
    /// ```
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn update<U>(self, f: impl FnOnce(&mut T) -> U) -> U {
        let ret = self.update_silent(f);
        self.0.root.propagate_updates(self.0.id);
        ret
    }

    /// Use a function to produce a new value and sets the value silently.
    ///
    /// This is the silent version of [`Signal::set_fn`].
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn set_fn_silent(self, f: impl FnOnce(&T) -> T) {
        self.update_silent(move |val| *val = f(val));
    }

    /// Use a function to produce a new value and sets the value.
    ///
    /// # Example
    /// ```
    /// # use sycamore_reactive::*;
    /// # create_root(|| {
    /// let state = create_signal(123);
    /// state.set_fn(|val| *val + 1);
    /// assert_eq!(state.get(), 124);
    /// # });
    /// ```
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn set_fn(self, f: impl FnOnce(&T) -> T) {
        self.update(move |val| *val = f(val));
    }

    /// Split the signal into a reader/writer pair.
    ///
    /// # Example
    /// ```
    /// # use sycamore_reactive::*;
    /// # create_root(|| {
    /// let (read_signal, mut write_signal) = create_signal(0).split();
    /// assert_eq!(read_signal.get(), 0);
    /// write_signal(1);
    /// assert_eq!(read_signal.get(), 1);
    /// # });
    /// ```
    pub fn split(self) -> (ReadSignal<T>, impl Fn(T) -> T) {
        (*self, move |value| self.replace(value))
    }
}

/// We manually implement `Clone` + `Copy` for `Signal` so that we don't get extra bounds on `T`.
impl<T> Clone for ReadSignal<T> {
    fn clone(&self) -> Self {
        *self
    }
}
impl<T> Copy for ReadSignal<T> {}

impl<T> Clone for Signal<T> {
    fn clone(&self) -> Self {
        *self
    }
}
impl<T> Copy for Signal<T> {}

// Implement `Default` for `ReadSignal` and `Signal`.
impl<T: Default> Default for ReadSignal<T> {
    fn default() -> Self {
        *create_signal(Default::default())
    }
}
impl<T: Default> Default for Signal<T> {
    fn default() -> Self {
        create_signal(Default::default())
    }
}

// Forward `PartialEq`, `Eq`, `PartialOrd`, `Ord`, `Hash` from inner type.
impl<T: PartialEq> PartialEq for ReadSignal<T> {
    fn eq(&self, other: &Self) -> bool {
        self.with(|value| other.with(|other| value == other))
    }
}
impl<T: Eq> Eq for ReadSignal<T> {}
impl<T: PartialOrd> PartialOrd for ReadSignal<T> {
    #[cfg_attr(debug_assertions, track_caller)]
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.with(|value| other.with(|other| value.partial_cmp(other)))
    }
}
impl<T: Ord> Ord for ReadSignal<T> {
    #[cfg_attr(debug_assertions, track_caller)]
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.with(|value| other.with(|other| value.cmp(other)))
    }
}
impl<T: Hash> Hash for ReadSignal<T> {
    #[cfg_attr(debug_assertions, track_caller)]
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.with(|value| value.hash(state))
    }
}

impl<T: PartialEq> PartialEq for Signal<T> {
    #[cfg_attr(debug_assertions, track_caller)]
    fn eq(&self, other: &Self) -> bool {
        self.with(|value| other.with(|other| value == other))
    }
}
impl<T: Eq> Eq for Signal<T> {}
impl<T: PartialOrd> PartialOrd for Signal<T> {
    #[cfg_attr(debug_assertions, track_caller)]
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.with(|value| other.with(|other| value.partial_cmp(other)))
    }
}
impl<T: Ord> Ord for Signal<T> {
    #[cfg_attr(debug_assertions, track_caller)]
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.with(|value| other.with(|other| value.cmp(other)))
    }
}
impl<T: Hash> Hash for Signal<T> {
    #[cfg_attr(debug_assertions, track_caller)]
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.with(|value| value.hash(state))
    }
}

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

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

// Formatting implementations for `ReadSignal` and `Signal`.
impl<T: fmt::Debug> fmt::Debug for ReadSignal<T> {
    #[cfg_attr(debug_assertions, track_caller)]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        self.with(|value| value.fmt(f))
    }
}
impl<T: fmt::Debug> fmt::Debug for Signal<T> {
    #[cfg_attr(debug_assertions, track_caller)]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        self.with(|value| value.fmt(f))
    }
}

impl<T: fmt::Display> fmt::Display for ReadSignal<T> {
    #[cfg_attr(debug_assertions, track_caller)]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        self.with(|value| value.fmt(f))
    }
}
impl<T: fmt::Display> fmt::Display for Signal<T> {
    #[cfg_attr(debug_assertions, track_caller)]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        self.with(|value| value.fmt(f))
    }
}

// Serde implementations for `ReadSignal` and `Signal`.
#[cfg(feature = "serde")]
impl<T: serde::Serialize> serde::Serialize for ReadSignal<T> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.with(|value| value.serialize(serializer))
    }
}
#[cfg(feature = "serde")]
impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for ReadSignal<T> {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        Ok(*create_signal(T::deserialize(deserializer)?))
    }
}
#[cfg(feature = "serde")]
impl<T: serde::Serialize> serde::Serialize for Signal<T> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.with(|value| value.serialize(serializer))
    }
}
#[cfg(feature = "serde")]
impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for Signal<T> {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        Ok(create_signal(T::deserialize(deserializer)?))
    }
}

#[cfg(feature = "nightly")]
impl<T: Copy> FnOnce<()> for ReadSignal<T> {
    type Output = T;

    extern "rust-call" fn call_once(self, _args: ()) -> Self::Output {
        self.get()
    }
}

impl<T: AddAssign<Rhs>, Rhs> AddAssign<Rhs> for Signal<T> {
    fn add_assign(&mut self, rhs: Rhs) {
        self.update(|this| *this += rhs);
    }
}
impl<T: SubAssign<Rhs>, Rhs> SubAssign<Rhs> for Signal<T> {
    fn sub_assign(&mut self, rhs: Rhs) {
        self.update(|this| *this -= rhs);
    }
}
impl<T: MulAssign<Rhs>, Rhs> MulAssign<Rhs> for Signal<T> {
    fn mul_assign(&mut self, rhs: Rhs) {
        self.update(|this| *this *= rhs);
    }
}
impl<T: DivAssign<Rhs>, Rhs> DivAssign<Rhs> for Signal<T> {
    fn div_assign(&mut self, rhs: Rhs) {
        self.update(|this| *this /= rhs);
    }
}
impl<T: RemAssign<Rhs>, Rhs> RemAssign<Rhs> for Signal<T> {
    fn rem_assign(&mut self, rhs: Rhs) {
        self.update(|this| *this %= rhs);
    }
}

// We need to implement this again for `Signal` despite `Signal` deref-ing to `ReadSignal` since
// we also have another implementation of `FnOnce` for `Signal`.
#[cfg(feature = "nightly")]
impl<T: Copy> FnOnce<()> for Signal<T> {
    type Output = T;

    extern "rust-call" fn call_once(self, _args: ()) -> Self::Output {
        self.get()
    }
}

#[cfg(feature = "nightly")]
impl<T: Copy> FnOnce<(T,)> for Signal<T> {
    type Output = T;

    extern "rust-call" fn call_once(self, (val,): (T,)) -> Self::Output {
        self.replace(val)
    }
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn signal() {
        let _ = create_root(|| {
            let state = create_signal(0);
            assert_eq!(state.get(), 0);

            state.set(1);
            assert_eq!(state.get(), 1);

            state.set_fn(|n| *n + 1);
            assert_eq!(state.get(), 2);
        });
    }

    #[test]
    fn signal_composition() {
        let _ = create_root(|| {
            let state = create_signal(0);
            let double = || state.get() * 2;

            assert_eq!(double(), 0);
            state.set(1);
            assert_eq!(double(), 2);
        });
    }

    #[test]
    fn set_silent_signal() {
        let _ = create_root(|| {
            let state = create_signal(0);
            let double = state.map(|&x| x * 2);

            assert_eq!(double.get(), 0);
            state.set_silent(1);
            assert_eq!(double.get(), 0); // double value is unchanged.

            state.set_fn_silent(|n| n + 1);
            assert_eq!(double.get(), 0); // double value is unchanged.
        });
    }

    #[test]
    fn read_signal() {
        let _ = create_root(|| {
            let state = create_signal(0);
            let readonly: ReadSignal<i32> = *state;

            assert_eq!(readonly.get(), 0);
            state.set(1);
            assert_eq!(readonly.get(), 1);
        });
    }

    #[test]
    fn map_signal() {
        let _ = create_root(|| {
            let state = create_signal(0);
            let double = state.map(|&x| x * 2);

            assert_eq!(double.get(), 0);
            state.set(1);
            assert_eq!(double.get(), 2);
        });
    }

    #[test]
    fn take_signal() {
        let _ = create_root(|| {
            let state = create_signal(123);

            let x = state.take();
            assert_eq!(x, 123);
            assert_eq!(state.get(), 0);
        });
    }

    #[test]
    fn take_silent_signal() {
        let _ = create_root(|| {
            let state = create_signal(123);
            let double = state.map(|&x| x * 2);

            // Do not trigger subscribers.
            state.take_silent();
            assert_eq!(state.get(), 0);
            assert_eq!(double.get(), 246);
        });
    }

    #[test]
    fn signal_split() {
        let _ = create_root(|| {
            let (state, set_state) = create_signal(0).split();
            assert_eq!(state.get(), 0);

            set_state(1);
            assert_eq!(state.get(), 1);
        });
    }

    #[test]
    fn signal_display() {
        let _ = create_root(|| {
            let signal = create_signal(0);
            assert_eq!(format!("{signal}"), "0");
            let read_signal: ReadSignal<_> = *signal;
            assert_eq!(format!("{read_signal}"), "0");
            let memo = create_memo(|| 0);
            assert_eq!(format!("{memo}"), "0");
        });
    }

    #[test]
    fn signal_debug() {
        let _ = create_root(|| {
            let signal = create_signal(0);
            assert_eq!(format!("{signal:?}"), "0");
            let read_signal: ReadSignal<_> = *signal;
            assert_eq!(format!("{read_signal:?}"), "0");
            let memo = create_memo(|| 0);
            assert_eq!(format!("{memo:?}"), "0");
        });
    }

    #[test]
    fn signal_add_assign_update() {
        let _ = create_root(|| {
            let mut signal = create_signal(0);
            let counter = create_signal(0);
            create_effect(move || {
                signal.track();
                counter.set(counter.get_untracked() + 1);
            });
            signal += 1;
            signal -= 1;
            signal *= 1;
            signal /= 1;
            assert_eq!(counter.get(), 5);
        });
    }

    #[test]
    fn signal_update() {
        let _ = create_root(|| {
            let signal = create_signal("Hello ".to_string());
            let counter = create_signal(0);
            create_effect(move || {
                signal.track();
                counter.set(counter.get_untracked() + 1);
            });
            signal.update(|value| value.push_str("World!"));
            assert_eq!(signal.get_clone(), "Hello World!");
            assert_eq!(counter.get(), 2);
        });
    }
}