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
use crate::ringbuffer_trait::{RingBufferIntoIterator, RingBufferIterator, RingBufferMutIterator};
use crate::RingBuffer;
use core::iter::FromIterator;
use core::mem;
use core::mem::MaybeUninit;
use core::ops::{Index, IndexMut};

/// The `ConstGenericRingBuffer` struct is a `RingBuffer` implementation which does not require `alloc` but
/// uses const generics instead.
///
/// [`ConstGenericRingBuffer`] allocates the ringbuffer on the stack, and the size must be known at
/// compile time through const-generics.
///
/// # Example
/// ```
/// use ringbuffer::{ConstGenericRingBuffer, RingBuffer};
///
/// let mut buffer = ConstGenericRingBuffer::<_, 2>::new();
///
/// // First entry of the buffer is now 5.
/// buffer.push(5);
///
/// // The last item we pushed is 5
/// assert_eq!(buffer.back(), Some(&5));
///
/// // Second entry is now 42.
/// buffer.push(42);
///
/// assert_eq!(buffer.peek(), Some(&5));
/// assert!(buffer.is_full());
///
/// // Because capacity is reached the next push will be the first item of the buffer.
/// buffer.push(1);
/// assert_eq!(buffer.to_vec(), vec![42, 1]);
/// ```
#[derive(Debug)]
pub struct ConstGenericRingBuffer<T, const CAP: usize> {
    buf: [MaybeUninit<T>; CAP],
    readptr: usize,
    writeptr: usize,
}

impl<T, const CAP: usize> From<[T; CAP]> for ConstGenericRingBuffer<T, CAP> {
    fn from(value: [T; CAP]) -> Self {
        Self {
            // Safety:
            // T has the same layout as MaybeUninit<T>
            // [T; N] has the same layout as [MaybeUninit<T>; N]
            buf: unsafe { mem::transmute_copy(&value) },
            readptr: 0,
            writeptr: CAP,
        }
    }
}

impl<T: Clone, const CAP: usize> From<&[T; CAP]> for ConstGenericRingBuffer<T, CAP> {
    fn from(value: &[T; CAP]) -> Self {
        Self::from(value.clone())
    }
}

impl<T: Clone, const CAP: usize> From<&[T]> for ConstGenericRingBuffer<T, CAP> {
    fn from(value: &[T]) -> Self {
        value.iter().cloned().collect()
    }
}

impl<T: Clone, const CAP: usize> From<&mut [T; CAP]> for ConstGenericRingBuffer<T, CAP> {
    fn from(value: &mut [T; CAP]) -> Self {
        Self::from(value.clone())
    }
}

impl<T: Clone, const CAP: usize> From<&mut [T]> for ConstGenericRingBuffer<T, CAP> {
    fn from(value: &mut [T]) -> Self {
        value.iter().cloned().collect()
    }
}

#[cfg(feature = "alloc")]
impl<T, const CAP: usize> From<alloc::vec::Vec<T>> for ConstGenericRingBuffer<T, CAP> {
    fn from(value: alloc::vec::Vec<T>) -> Self {
        value.into_iter().collect()
    }
}

#[cfg(feature = "alloc")]
impl<T, const CAP: usize> From<alloc::collections::VecDeque<T>> for ConstGenericRingBuffer<T, CAP> {
    fn from(value: alloc::collections::VecDeque<T>) -> Self {
        value.into_iter().collect()
    }
}

#[cfg(feature = "alloc")]
impl<T, const CAP: usize> From<alloc::collections::LinkedList<T>>
    for ConstGenericRingBuffer<T, CAP>
{
    fn from(value: alloc::collections::LinkedList<T>) -> Self {
        value.into_iter().collect()
    }
}

#[cfg(feature = "alloc")]
impl<const CAP: usize> From<alloc::string::String> for ConstGenericRingBuffer<char, CAP> {
    fn from(value: alloc::string::String) -> Self {
        value.chars().collect()
    }
}

impl<const CAP: usize> From<&str> for ConstGenericRingBuffer<char, CAP> {
    fn from(value: &str) -> Self {
        value.chars().collect()
    }
}

#[cfg(feature = "alloc")]
impl<T, const CAP: usize> From<crate::GrowableAllocRingBuffer<T>>
    for ConstGenericRingBuffer<T, CAP>
{
    fn from(mut value: crate::GrowableAllocRingBuffer<T>) -> Self {
        value.drain().collect()
    }
}

#[cfg(feature = "alloc")]
impl<T, const CAP: usize> From<crate::AllocRingBuffer<T>> for ConstGenericRingBuffer<T, CAP> {
    fn from(mut value: crate::AllocRingBuffer<T>) -> Self {
        value.drain().collect()
    }
}

impl<T, const CAP: usize> Drop for ConstGenericRingBuffer<T, CAP> {
    fn drop(&mut self) {
        self.drain().for_each(drop);
    }
}

impl<T: Clone, const CAP: usize> Clone for ConstGenericRingBuffer<T, CAP> {
    fn clone(&self) -> Self {
        let mut new = ConstGenericRingBuffer::<T, CAP>::new();
        self.iter().cloned().for_each(|i| new.push(i));
        new
    }
}

// We need to manually implement PartialEq because MaybeUninit isn't PartialEq
impl<T: PartialEq, const CAP: usize> PartialEq for ConstGenericRingBuffer<T, CAP> {
    fn eq(&self, other: &Self) -> bool {
        if self.len() == other.len() {
            for (a, b) in self.iter().zip(other.iter()) {
                if a != b {
                    return false;
                }
            }
            true
        } else {
            false
        }
    }
}

impl<T: PartialEq, const CAP: usize> Eq for ConstGenericRingBuffer<T, CAP> {}

impl<T, const CAP: usize> ConstGenericRingBuffer<T, CAP> {
    const ERROR_CAPACITY_IS_NOT_ALLOWED_TO_BE_ZERO: () =
        assert!(CAP != 0, "Capacity is not allowed to be zero");

    /// Creates a const generic ringbuffer, size is passed as a const generic.
    ///
    /// Note that the size does not have to be a power of two, but that not using a power
    /// of two might be significantly (up to 3 times) slower.
    #[inline]
    #[must_use]
    pub const fn new<const N: usize>() -> Self
    where
        ConstGenericRingBuffer<T, CAP>: From<ConstGenericRingBuffer<T, N>>,
    {
        #[allow(clippy::let_unit_value)]
        let _ = Self::ERROR_CAPACITY_IS_NOT_ALLOWED_TO_BE_ZERO;

        // allow here since we are constructing an array of MaybeUninit<T>
        // which explicitly *is* defined behavior
        // https://rust-lang.github.io/rust-clippy/master/index.html#uninit_assumed_init
        #[allow(clippy::uninit_assumed_init)]
        Self {
            buf: unsafe { MaybeUninit::uninit().assume_init() },
            writeptr: 0,
            readptr: 0,
        }
    }
}

/// Get a reference from the buffer without checking it is initialized
/// Caller MUST be sure this index is initialized, or undefined behavior will happen
unsafe fn get_unchecked<'a, T, const N: usize>(
    rb: *const ConstGenericRingBuffer<T, N>,
    index: usize,
) -> &'a T {
    (*rb).buf[index]
        .as_ptr()
        .as_ref()
        .expect("const array ptr shouldn't be null!")
}

/// Get a mutable reference from the buffer without checking it is initialized
/// Caller MUST be sure this index is initialized, or undefined behavior will happen
unsafe fn get_unchecked_mut<T, const N: usize>(
    rb: *mut ConstGenericRingBuffer<T, N>,
    index: usize,
) -> *mut T {
    (*rb).buf[index]
        .as_mut_ptr()
        .as_mut()
        .expect("const array ptr shouldn't be null!")
}

impl<T, const CAP: usize> IntoIterator for ConstGenericRingBuffer<T, CAP> {
    type Item = T;
    type IntoIter = RingBufferIntoIterator<T, Self>;

    fn into_iter(self) -> Self::IntoIter {
        RingBufferIntoIterator::new(self)
    }
}

impl<'a, T, const CAP: usize> IntoIterator for &'a ConstGenericRingBuffer<T, CAP> {
    type Item = &'a T;
    type IntoIter = RingBufferIterator<'a, T, ConstGenericRingBuffer<T, CAP>>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, T, const CAP: usize> IntoIterator for &'a mut ConstGenericRingBuffer<T, CAP> {
    type Item = &'a mut T;
    type IntoIter = RingBufferMutIterator<'a, T, ConstGenericRingBuffer<T, CAP>>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

impl<T, const CAP: usize> Extend<T> for ConstGenericRingBuffer<T, CAP> {
    fn extend<A: IntoIterator<Item = T>>(&mut self, iter: A) {
        let iter = iter.into_iter();

        for i in iter {
            self.push(i);
        }
    }
}

unsafe impl<T, const CAP: usize> RingBuffer<T> for ConstGenericRingBuffer<T, CAP> {
    #[inline]
    unsafe fn ptr_capacity(_: *const Self) -> usize {
        CAP
    }

    #[inline]
    unsafe fn ptr_buffer_size(_: *const Self) -> usize {
        CAP
    }

    impl_ringbuffer!(readptr, writeptr);

    #[inline]
    fn push(&mut self, value: T) {
        if self.is_full() {
            let previous_value = mem::replace(
                &mut self.buf[crate::mask_modulo(CAP, self.readptr)],
                MaybeUninit::uninit(),
            );
            // make sure we drop whatever is being overwritten
            // SAFETY: the buffer is full, so this must be initialized
            //       : also, index has been masked
            // make sure we drop because it won't happen automatically
            unsafe {
                drop(previous_value.assume_init());
            }
            self.readptr += 1;
        }
        let index = crate::mask_modulo(CAP, self.writeptr);
        self.buf[index] = MaybeUninit::new(value);
        self.writeptr += 1;
    }

    fn dequeue(&mut self) -> Option<T> {
        if self.is_empty() {
            None
        } else {
            let index = crate::mask_modulo(CAP, self.readptr);
            let res = mem::replace(&mut self.buf[index], MaybeUninit::uninit());
            self.readptr += 1;

            // Safety: the fact that we got this maybeuninit from the buffer (with mask) means that
            // it's initialized. If it wasn't the is_empty call would have caught it. Values
            // are always initialized when inserted so this is safe.
            unsafe { Some(res.assume_init()) }
        }
    }

    impl_ringbuffer_ext!(
        get_unchecked,
        get_unchecked_mut,
        readptr,
        writeptr,
        crate::mask_modulo
    );

    #[inline]
    fn fill_with<F: FnMut() -> T>(&mut self, mut f: F) {
        self.clear();
        self.readptr = 0;
        self.writeptr = CAP;
        self.buf.fill_with(|| MaybeUninit::new(f()));
    }
}

impl<T, const CAP: usize> Default for ConstGenericRingBuffer<T, CAP> {
    /// Creates a buffer with a capacity specified through the Cap type parameter.
    /// # Panics
    /// Panics if `CAP` is 0
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl<RB, const CAP: usize> FromIterator<RB> for ConstGenericRingBuffer<RB, CAP> {
    fn from_iter<T: IntoIterator<Item = RB>>(iter: T) -> Self {
        let mut res = Self::default();
        for i in iter {
            res.push(i);
        }

        res
    }
}

impl<T, const CAP: usize> Index<usize> for ConstGenericRingBuffer<T, CAP> {
    type Output = T;

    fn index(&self, index: usize) -> &Self::Output {
        self.get(index).expect("index out of bounds")
    }
}

impl<T, const CAP: usize> IndexMut<usize> for ConstGenericRingBuffer<T, CAP> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        self.get_mut(index).expect("index out of bounds")
    }
}

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

    #[test]
    fn test_not_power_of_two() {
        let mut rb = ConstGenericRingBuffer::<usize, 10>::new();
        const NUM_VALS: usize = 1000;

        // recycle the ringbuffer a bunch of time to see if noneof the logic
        // messes up
        for _ in 0..100 {
            for i in 0..NUM_VALS {
                rb.enqueue(i);
            }
            assert!(rb.is_full());

            for i in 0..10 {
                assert_eq!(Some(i + NUM_VALS - rb.capacity()), rb.dequeue())
            }

            assert!(rb.is_empty())
        }
    }

    #[test]
    #[should_panic]
    fn test_index_zero_length() {
        let b = ConstGenericRingBuffer::<i32, 2>::new();
        let _ = b[2];
    }

    #[test]
    fn test_extend() {
        let mut buf = ConstGenericRingBuffer::<u8, 4>::new();
        (0..4).for_each(|_| buf.push(0));

        let new_data = [0, 1, 2];
        buf.extend(new_data);

        let expected = [0, 0, 1, 2];

        for i in 0..4 {
            let actual = buf[i];
            let expected = expected[i];
            assert_eq!(actual, expected);
        }
    }

    #[test]
    fn test_extend_with_overflow() {
        let mut buf = ConstGenericRingBuffer::<u8, 8>::new();
        (0..8).for_each(|_| buf.push(0));

        let new_data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        buf.extend(new_data);

        let expected = [2, 3, 4, 5, 6, 7, 8, 9];

        for i in 0..8 {
            let actual = buf[i];
            let expected = expected[i];
            assert_eq!(actual, expected);
        }
    }

    #[cfg(test)]
    mod tests {
        use crate::{AllocRingBuffer, ConstGenericRingBuffer, GrowableAllocRingBuffer, RingBuffer};
        use alloc::collections::{LinkedList, VecDeque};
        use alloc::string::ToString;
        use alloc::vec;

        #[test]
        fn from() {
            assert_eq!(
                ConstGenericRingBuffer::<i32, 3>::from([1, 2, 3]).to_vec(),
                vec![1, 2, 3]
            );

            let v: &[i32; 3] = &[1, 2, 3];
            assert_eq!(
                ConstGenericRingBuffer::<i32, 3>::from(v).to_vec(),
                vec![1, 2, 3]
            );

            let v: &[i32] = &[1, 2, 3];
            assert_eq!(
                ConstGenericRingBuffer::<i32, 3>::from(v).to_vec(),
                vec![1, 2, 3]
            );

            let v: &mut [i32; 3] = &mut [1, 2, 3];
            assert_eq!(
                ConstGenericRingBuffer::<i32, 3>::from(v).to_vec(),
                vec![1, 2, 3]
            );

            let v: &mut [i32] = &mut [1, 2, 3];
            assert_eq!(
                ConstGenericRingBuffer::<i32, 3>::from(v).to_vec(),
                vec![1, 2, 3]
            );

            assert_eq!(
                ConstGenericRingBuffer::<i32, 3>::from(vec![1, 2, 3]).to_vec(),
                vec![1, 2, 3]
            );
            assert_eq!(
                ConstGenericRingBuffer::<i32, 3>::from(
                    vec![1, 2, 3].into_iter().collect::<VecDeque<_>>()
                )
                .to_vec(),
                vec![1, 2, 3]
            );
            assert_eq!(
                ConstGenericRingBuffer::<i32, 3>::from(
                    vec![1, 2, 3].into_iter().collect::<LinkedList<_>>()
                )
                .to_vec(),
                vec![1, 2, 3]
            );
            assert_eq!(
                ConstGenericRingBuffer::<_, 3>::from("abc".to_string()).to_vec(),
                vec!['a', 'b', 'c']
            );
            assert_eq!(
                ConstGenericRingBuffer::<_, 3>::from("abc").to_vec(),
                vec!['a', 'b', 'c']
            );
            assert_eq!(
                ConstGenericRingBuffer::<_, 3>::from(GrowableAllocRingBuffer::from(vec![1, 2, 3]))
                    .to_vec(),
                vec![1, 2, 3]
            );
            assert_eq!(
                ConstGenericRingBuffer::<_, 3>::from(AllocRingBuffer::from(vec![1, 2, 3])).to_vec(),
                vec![1, 2, 3]
            );
        }
    }
}