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
use crate::rt::CopyValue;
use crate::signal::{ReadOnlySignal, Signal, Write};

use std::cell::{Ref, RefMut};

use std::{
    fmt::{Debug, Display},
    ops::{Add, Div, Mul, Sub},
};

macro_rules! read_impls {
    ($ty:ident) => {
        impl<T: Default + 'static> Default for $ty<T> {
            fn default() -> Self {
                Self::new(Default::default())
            }
        }

        impl<T> std::clone::Clone for $ty<T> {
            fn clone(&self) -> Self {
                *self
            }
        }

        impl<T> Copy for $ty<T> {}

        impl<T: Display + 'static> Display for $ty<T> {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                self.with(|v| Display::fmt(v, f))
            }
        }

        impl<T: Debug + 'static> Debug for $ty<T> {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                self.with(|v| Debug::fmt(v, f))
            }
        }

        impl<T: 'static> $ty<Vec<T>> {
            /// Read a value from the inner vector.
            pub fn get(&self, index: usize) -> Option<Ref<'_, T>> {
                Ref::filter_map(self.read(), |v| v.get(index)).ok()
            }
        }

        impl<T: 'static> $ty<Option<T>> {
            /// Unwraps the inner value and clones it.
            pub fn unwrap(&self) -> T
            where
                T: Clone,
            {
                self.with(|v| v.clone()).unwrap()
            }

            /// Attemps to read the inner value of the Option.
            pub fn as_ref(&self) -> Option<Ref<'_, T>> {
                Ref::filter_map(self.read(), |v| v.as_ref()).ok()
            }
        }
    };
}

macro_rules! write_impls {
    ($ty:ident) => {
        impl<T: Add<Output = T> + Copy + 'static> std::ops::Add<T> for $ty<T> {
            type Output = T;

            fn add(self, rhs: T) -> Self::Output {
                self.with(|v| *v + rhs)
            }
        }

        impl<T: Add<Output = T> + Copy + 'static> std::ops::AddAssign<T> for $ty<T> {
            fn add_assign(&mut self, rhs: T) {
                self.with_mut(|v| *v = *v + rhs)
            }
        }

        impl<T: Sub<Output = T> + Copy + 'static> std::ops::SubAssign<T> for $ty<T> {
            fn sub_assign(&mut self, rhs: T) {
                self.with_mut(|v| *v = *v - rhs)
            }
        }

        impl<T: Sub<Output = T> + Copy + 'static> std::ops::Sub<T> for $ty<T> {
            type Output = T;

            fn sub(self, rhs: T) -> Self::Output {
                self.with(|v| *v - rhs)
            }
        }

        impl<T: Mul<Output = T> + Copy + 'static> std::ops::MulAssign<T> for $ty<T> {
            fn mul_assign(&mut self, rhs: T) {
                self.with_mut(|v| *v = *v * rhs)
            }
        }

        impl<T: Mul<Output = T> + Copy + 'static> std::ops::Mul<T> for $ty<T> {
            type Output = T;

            fn mul(self, rhs: T) -> Self::Output {
                self.with(|v| *v * rhs)
            }
        }

        impl<T: Div<Output = T> + Copy + 'static> std::ops::DivAssign<T> for $ty<T> {
            fn div_assign(&mut self, rhs: T) {
                self.with_mut(|v| *v = *v / rhs)
            }
        }

        impl<T: Div<Output = T> + Copy + 'static> std::ops::Div<T> for $ty<T> {
            type Output = T;

            fn div(self, rhs: T) -> Self::Output {
                self.with(|v| *v / rhs)
            }
        }

        impl<T: 'static> $ty<Vec<T>> {
            /// Pushes a new value to the end of the vector.
            pub fn push(&self, value: T) {
                self.with_mut(|v| v.push(value))
            }

            /// Pops the last value from the vector.
            pub fn pop(&self) -> Option<T> {
                self.with_mut(|v| v.pop())
            }

            /// Inserts a new value at the given index.
            pub fn insert(&self, index: usize, value: T) {
                self.with_mut(|v| v.insert(index, value))
            }

            /// Removes the value at the given index.
            pub fn remove(&self, index: usize) -> T {
                self.with_mut(|v| v.remove(index))
            }

            /// Clears the vector, removing all values.
            pub fn clear(&self) {
                self.with_mut(|v| v.clear())
            }

            /// Extends the vector with the given iterator.
            pub fn extend(&self, iter: impl IntoIterator<Item = T>) {
                self.with_mut(|v| v.extend(iter))
            }

            /// Truncates the vector to the given length.
            pub fn truncate(&self, len: usize) {
                self.with_mut(|v| v.truncate(len))
            }

            /// Swaps two values in the vector.
            pub fn swap_remove(&self, index: usize) -> T {
                self.with_mut(|v| v.swap_remove(index))
            }

            /// Retains only the values that match the given predicate.
            pub fn retain(&self, f: impl FnMut(&T) -> bool) {
                self.with_mut(|v| v.retain(f))
            }

            /// Splits the vector into two at the given index.
            pub fn split_off(&self, at: usize) -> Vec<T> {
                self.with_mut(|v| v.split_off(at))
            }
        }

        impl<T: 'static> $ty<Option<T>> {
            /// Takes the value out of the Option.
            pub fn take(&self) -> Option<T> {
                self.with_mut(|v| v.take())
            }

            /// Replace the value in the Option.
            pub fn replace(&self, value: T) -> Option<T> {
                self.with_mut(|v| v.replace(value))
            }

            /// Gets the value out of the Option, or inserts the given value if the Option is empty.
            pub fn get_or_insert(&self, default: T) -> Ref<'_, T> {
                self.get_or_insert_with(|| default)
            }

            /// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty.
            pub fn get_or_insert_with(&self, default: impl FnOnce() -> T) -> Ref<'_, T> {
                let borrow = self.read();
                if borrow.is_none() {
                    drop(borrow);
                    self.with_mut(|v| *v = Some(default()));
                    Ref::map(self.read(), |v| v.as_ref().unwrap())
                } else {
                    Ref::map(borrow, |v| v.as_ref().unwrap())
                }
            }
        }
    };
}

read_impls!(CopyValue);
write_impls!(CopyValue);
read_impls!(Signal);
write_impls!(Signal);
read_impls!(ReadOnlySignal);

/// An iterator over the values of a `CopyValue<Vec<T>>`.
pub struct CopyValueIterator<T: 'static> {
    index: usize,
    value: CopyValue<Vec<T>>,
}

impl<T: Clone> Iterator for CopyValueIterator<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        let index = self.index;
        self.index += 1;
        self.value.get(index).map(|v| v.clone())
    }
}

impl<T: Clone + 'static> IntoIterator for CopyValue<Vec<T>> {
    type IntoIter = CopyValueIterator<T>;

    type Item = T;

    fn into_iter(self) -> Self::IntoIter {
        CopyValueIterator {
            index: 0,
            value: self,
        }
    }
}

impl<T: 'static> CopyValue<Vec<T>> {
    /// Write to an element in the inner vector.
    pub fn get_mut(&self, index: usize) -> Option<RefMut<'_, T>> {
        RefMut::filter_map(self.write(), |v| v.get_mut(index)).ok()
    }
}

impl<T: 'static> CopyValue<Option<T>> {
    /// Deref the inner value mutably.
    pub fn as_mut(&self) -> Option<RefMut<'_, T>> {
        RefMut::filter_map(self.write(), |v| v.as_mut()).ok()
    }
}

/// An iterator over items in a `Signal<Vec<T>>`.
pub struct SignalIterator<T: 'static> {
    index: usize,
    value: Signal<Vec<T>>,
}

impl<T: Clone> Iterator for SignalIterator<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        let index = self.index;
        self.index += 1;
        self.value.get(index).map(|v| v.clone())
    }
}

impl<T: Clone + 'static> IntoIterator for Signal<Vec<T>> {
    type IntoIter = SignalIterator<T>;

    type Item = T;

    fn into_iter(self) -> Self::IntoIter {
        SignalIterator {
            index: 0,
            value: self,
        }
    }
}

impl<T: 'static> Signal<Vec<T>> {
    /// Returns a reference to an element or `None` if out of bounds.
    pub fn get_mut(&self, index: usize) -> Option<Write<'_, T, Vec<T>>> {
        Write::filter_map(self.write(), |v| v.get_mut(index))
    }
}

impl<T: 'static> Signal<Option<T>> {
    /// Returns a reference to an element or `None` if out of bounds.
    pub fn as_mut(&self) -> Option<Write<'_, T, Option<T>>> {
        Write::filter_map(self.write(), |v| v.as_mut())
    }
}