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
use crate::ShareValue;

use super::SharedState;

#[derive(Debug)]
pub struct SharedStateEq<T: PartialEq>(SharedState<T>);

impl<T: PartialEq> Unpin for SharedStateEq<T> {}

impl<T: PartialEq> Clone for SharedStateEq<T> {
    #[inline]
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<T: PartialEq> SharedStateEq<T> {
    #[inline]
    pub fn new(initial_value: T) -> Self {
        Self(SharedState::new(initial_value))
    }

    #[inline]
    pub fn inner(&self) -> &SharedState<T> {
        &self.0
    }
}

impl<T: PartialEq> ShareValue for SharedStateEq<T> {
    type Value = T;

    #[inline]
    fn is_shared(&self) -> bool {
        self.0.is_shared()
    }

    #[inline]
    fn get(&self) -> T
    where
        T: Copy,
    {
        self.0.get()
    }

    #[inline]
    fn get_cloned(&self) -> T
    where
        T: Clone,
    {
        self.0.get_cloned()
    }

    #[inline]
    fn set(&self, new_value: T) {
        self.0.map_mut_and_notify_if(move |old| {
            let changed = *old != new_value;
            *old = new_value;
            ((), changed)
        })
    }

    #[inline]
    fn replace(&self, new_value: T) -> T {
        self.0.map_mut_and_notify_if(move |old| {
            let changed = *old != new_value;
            let old = std::mem::replace(old, new_value);
            (old, changed)
        })
    }

    /// Always notify the value is changed
    /// because the caller can mutate the value inside `f`.
    ///
    /// To notify changes only when `new_value != old_value`,
    /// use [`replace_from_ref`](ShareValue::replace_with) or
    /// [`self.inner().map_mut_and_notify_if()`](SharedState::map_mut_and_notify_if)
    /// instead.
    #[inline]
    fn replace_mut<F: FnOnce(&mut T) -> T>(&self, f: F) -> T {
        self.0.replace_mut(f)
    }

    #[inline]
    fn replace_with<F: FnOnce(&T) -> T>(&self, f: F) -> T {
        self.0.map_mut_and_notify_if(move |v| {
            let new_value = f(v);
            let changed = new_value != *v;
            let old = std::mem::replace(v, new_value);
            (old, changed)
        })
    }

    #[inline]
    fn map<R>(&self, f: impl FnOnce(&T) -> R) -> R {
        self.0.map(f)
    }

    #[inline]
    fn map_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
        self.0.map_mut(f)
    }

    fn equivalent_to(&self, other: &Self) -> bool {
        self.0.equivalent_to(&other.0)
    }
}

hooks_core::impl_hook![
    type For<T: PartialEq> = SharedStateEq<T>;
    fn unmount() {}
    #[inline]
    fn poll_next_update(self, cx: _) {
        self.get_mut().0.impl_poll_next_update(cx)
    }
    #[inline]
    fn use_hook(self) -> &'hook Self {
        let this = self.get_mut();
        this.0.mark_as_unregistered();
        this
    }
];