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
#![doc = include_str!("../README.md")]
#![no_std]

use core::ops::{Deref, DerefMut};
use core::borrow::{Borrow, BorrowMut};
use core::fmt::{Debug, Formatter, Pointer};

enum State {
    First,
    Second,
}

/// Encapsulates a piece of state that can be modified and
/// we want all outside code to see the edit as a single
/// atomic change.
///
/// # Trait implementations
///
/// If trait use an immutable reference ([`AsRef<T>`], [`Deref`], [`Borrow<T>`]...) give access to the current value
/// and mutable references ([`AsMut<T>`], [`DerefMut`], [`BorrowMut<T>`]...) give access to the next value.
///
/// # Swapping
///
/// There are three ways to swap:
///
/// 1. [`DoubleBuffer::swap()`] - when swapping, the next value will have the previous current value.
/// 2. [`DoubleBuffer::swap_with_clone()`] - when swapping, the next value will keep same and will be cloned to the current value.
/// 3. [`DoubleBuffer::swap_with_default()`] - like [`DoubleBuffer::swap()`] but the next value will be set to the default value of the type.
///
/// Note that for the third way, the type must implement [`Default`].
///
/// You can read about the two ways [how the buffers are swapped](https://gameprogrammingpatterns.com/double-buffer.html#how-are-the-buffers-swapped)
/// in "Game Programming Patterns" by Robert Nystrom.
///
/// ## Swapping Benchmarks
///
/// The following are the results in a i7 10th gen with 32GB RAM for a `vec![u8; 8192]` buffer:
///
/// 1. [`DoubleBuffer::swap()`] - 1.6655 ns 1.6814 ns 1.6964 ns
/// 2. [`DoubleBuffer::swap_with_default()`] - 1.7783 ns 1.8009 ns 1.8262 ns
/// 3. [`DoubleBuffer::swap_with_clone()`] - 174.18 ns 177.86 ns 182.07 ns
///
/// If it's not important to keep the pointer address of the current value unchanged,
/// [`DoubleBuffer::swap()`] is the best option, or [`DoubleBuffer::swap_with_default()`]
/// if the type implements [`Default`] and starts with the default value is important.
///
/// Only use [`DoubleBuffer::swap_with_clone()`] if it's important to keep the pointer
/// address of the current value unchanged.
///
/// # Examples
///
/// The following example shows how the buffer is swapped with the three ways:
///
/// ```
/// # use double_buffer::DoubleBuffer;
/// let mut buffer: DoubleBuffer<[u8; 32]> = DoubleBuffer::default();
/// print!("{:?}", buffer); // DoubleBuffer { current: [0, ...], next: [0, ...] }
///
/// buffer[0] = 1;
/// print!("{:?}", buffer); // DoubleBuffer { current: [0, ...], next: [1, ...] }
///
/// buffer.swap();
/// print!("{:?}", buffer); // DoubleBuffer { current: [1, ...], next: [0, ...] }
///
/// buffer[0] = 2;
/// print!("{:?}", buffer); // DoubleBuffer { current: [1, ...], next: [2, ...] }
///
/// buffer.swap_with_clone();
/// print!("{:?}", buffer); // DoubleBuffer { current: [2, ...], next: [2, ...] }
///
/// buffer[0] = 3;
/// print!("{:?}", buffer); // DoubleBuffer { current: [2, ...], next: [3, ...] }
///
/// buffer.swap_with_default();
/// print!("{:?}", buffer); // DoubleBuffer { current: [3, ...], next: [0, ...] }
/// ```
pub struct DoubleBuffer<T> {
    state: State,
    buffers: [T; 2],
}

impl<T> DoubleBuffer<T> {
    #[inline]
    pub const fn new(current: T, next: T) -> Self {
        let state = State::First;
        let buffers = [current, next];
        Self { state, buffers }
    }

    /// Swaps the current and next values,
    /// then writes will be over the previous current value.
    ///
    /// This changes the pointer address of the current value.
    ///
    /// # Examples
    ///
    /// ```
    /// # use double_buffer::DoubleBuffer;
    /// let mut buffer: DoubleBuffer<[u8; 8192]> = DoubleBuffer::new([0; 8192], [0; 8192]);
    /// let first_address = format!("{:p}", buffer);
    /// buffer.swap();
    /// let second_address = format!("{:p}", buffer);
    /// // The addresses are different.
    /// assert_ne!(first_address, second_address);
    /// ```
    #[inline]
    pub fn swap(&mut self) {
        self.state = match &self.state {
            State::First => State::Second,
            State::Second => State::First,
        };
    }

    #[inline]
    fn current(&self) -> &T {
        match self.state {
            State::First => &self.buffers[0],
            State::Second => &self.buffers[1],
        }
    }

    #[inline]
    fn next(&self) -> &T {
        match self.state {
            State::First => &self.buffers[1],
            State::Second => &self.buffers[0],
        }
    }

    #[inline]
    fn current_mut(&mut self) -> &mut T {
        match self.state {
            State::First => &mut self.buffers[0],
            State::Second => &mut self.buffers[1],
        }
    }

    #[inline]
    fn next_mut(&mut self) -> &mut T {
        match self.state {
            State::First => &mut self.buffers[1],
            State::Second => &mut self.buffers[0],
        }
    }
}

impl<T: Clone> DoubleBuffer<T> {
    /// Clone the next value to the current value,
    /// then writes will continue over the same next value.
    ///
    /// This let the pointer address of the current value unchanged.
    ///
    /// # Examples
    ///
    /// ```
    /// # use double_buffer::DoubleBuffer;
    /// let mut buffer: DoubleBuffer<[u8; 8192]> = DoubleBuffer::new([0; 8192], [0; 8192]);
    /// let first_address = format!("{:p}", buffer);
    /// buffer.swap_with_clone();
    /// let second_address = format!("{:p}", buffer);
    /// // The addresses are different.
    /// assert_eq!(first_address, second_address);
    /// ```
    #[inline]
    pub fn swap_with_clone(&mut self) {
        let next = self.next().clone();
        let current = self.current_mut();
        *current = next;
    }
}

impl<T: Default> DoubleBuffer<T> {
    /// Swaps buffers like [`DoubleBuffer::swap()`] and sets the next
    /// value to the default value of the type, then writes will be
    /// over the default value.
    #[inline]
    pub fn swap_with_default(&mut self) {
        self.swap();
        let next = self.next_mut();
        *next = T::default();
    }
}

impl<T: Debug> Debug for DoubleBuffer<T> {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("DoubleBuffer")
            .field("current", self.current())
            .field("next", self.next())
            .finish()
    }
}

impl<T> Pointer for DoubleBuffer<T> {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write!(f, "{:p}", self.current())
    }
}

impl<T: Default> Default for DoubleBuffer<T> {
    #[inline]
    fn default() -> Self {
        Self::new(T::default(), T::default())
    }
}

impl<T> Deref for DoubleBuffer<T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.current()
    }
}

impl<T> DerefMut for DoubleBuffer<T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.next_mut()
    }
}

impl<T> Borrow<T> for DoubleBuffer<T> {
    #[inline]
    fn borrow(&self) -> &T {
        self.current()
    }
}

impl<T> BorrowMut<T> for DoubleBuffer<T> {
    #[inline]
    fn borrow_mut(&mut self) -> &mut T {
        self.next_mut()
    }
}

impl<T> AsRef<T> for DoubleBuffer<T> {
    #[inline]
    fn as_ref(&self) -> &T {
        self.current()
    }
}

impl<T> AsMut<T> for DoubleBuffer<T> {
    #[inline]
    fn as_mut(&mut self) -> &mut T {
        self.next_mut()
    }
}

impl<T: PartialEq> PartialEq<T> for DoubleBuffer<T> {
    #[inline]
    fn eq(&self, other: &T) -> bool {
        self.current().eq(other)
    }
}

impl<T: PartialEq> PartialEq for DoubleBuffer<T> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.current().eq(other.current())
    }
}

impl<T: Eq> Eq for DoubleBuffer<T> {}

impl<T: PartialOrd> PartialOrd<T> for DoubleBuffer<T> {
    #[inline]
    fn partial_cmp(&self, other: &T) -> Option<core::cmp::Ordering> {
        self.current().partial_cmp(other)
    }
}

impl<T: PartialOrd> PartialOrd for DoubleBuffer<T> {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        self.current().partial_cmp(other.current())
    }
}

impl<T: Ord> Ord for DoubleBuffer<T> {
    #[inline]
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.current().cmp(other.current())
    }
}

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

    #[test]
    fn test_access_and_modify_with_swap() {
        let mut buffer: DoubleBuffer<u32> = DoubleBuffer::new(1, 2);
        assert_eq!(buffer, 1);

        *buffer = 3;
        assert_eq!(buffer, 1);

        buffer.swap();
        assert_eq!(buffer, 3);
    }

    #[test]
    fn test_access_and_modify_with_swap_with_clone() {
        let mut buffer: DoubleBuffer<u32> = DoubleBuffer::new(1, 2);
        assert_eq!(buffer, 1);

        *buffer = 3;
        assert_eq!(buffer, 1);

        buffer.swap_with_clone();
        assert_eq!(buffer, 3);
    }

    #[test]
    fn test_access_and_modify_with_swap_with_default() {
        let mut buffer: DoubleBuffer<u32> = DoubleBuffer::new(1, 2);
        assert_eq!(buffer, 1);

        *buffer = 3;
        assert_eq!(buffer, 1);

        buffer.swap_with_default();
        assert_eq!(buffer, 3);
    }

    #[test]
    fn test_swap() {
        let mut buffer: DoubleBuffer<u32> = DoubleBuffer::new(1, 2);
        assert_eq!(*buffer.current(), 1);
        assert_eq!(*buffer.next(), 2);

        buffer.swap();
        assert_eq!(*buffer.current(), 2);
        assert_eq!(*buffer.next(), 1);
    }

    #[test]
    fn test_swap_with_clone() {
        let mut buffer: DoubleBuffer<u32> = DoubleBuffer::new(1, 2);
        assert_eq!(*buffer.current(), 1);
        assert_eq!(*buffer.next(), 2);

        buffer.swap_with_clone();
        assert_eq!(*buffer.current(), 2);
        assert_eq!(*buffer.next(), 2);
    }

    #[test]
    fn test_swap_with_default() {
        let mut buffer: DoubleBuffer<u32> = DoubleBuffer::new(1, 2);
        assert_eq!(*buffer.current(), 1);
        assert_eq!(*buffer.next(), 2);

        buffer.swap_with_default();
        assert_eq!(*buffer.current(), 2);
        assert_eq!(*buffer.next(), 0);
    }

    #[test]
    fn test_greater_and_less_than() {
        let mut buffer: DoubleBuffer<i32> = DoubleBuffer::default();
        *buffer = 1;
        assert!(buffer > -1);
        assert!(buffer < 1);

        buffer.swap();
        assert!(buffer > 0);
        assert!(buffer < 2);
    }

    #[test]
    fn test_modify_bytes_array() {
        let mut buffer: DoubleBuffer<[u8; 3]> = DoubleBuffer::default();
        buffer[1] = 2;
        assert_eq!(buffer[1], 0);
        assert_eq!(buffer, [0, 0, 0]);

        assert_eq!(*buffer.current(), [0, 0, 0]);
        assert_eq!(*buffer.next(), [0, 2, 0]);

        buffer.swap();
        assert_eq!(buffer[1], 2);
        assert_eq!(buffer, [0, 2, 0]);

        assert_eq!(*buffer.current(), [0, 2, 0]);
        assert_eq!(*buffer.next(), [0, 0, 0]);
    }

    #[test]
    fn test_for_iter_mut_bytes_array() {
        let mut buffer: DoubleBuffer<[u8; 3]> = DoubleBuffer::default();
        buffer[1] = 2;

        assert_eq!(*buffer.current(), [0, 0, 0]);
        assert_eq!(*buffer.next(), [0, 2, 0]);

        for byte in buffer.iter_mut() {
            *byte += 1;
        }

        assert_eq!(*buffer.current(), [0, 0, 0]);
        assert_eq!(*buffer.next(), [1, 3, 1]);
    }
}