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
//! [`Buffer`] definition and simple implementations.

use std::{
    fmt,
    iter::FusedIterator,
    marker::PhantomData,
    mem::ManuallyDrop,
    ops::{Deref, DerefMut, Range},
    ptr,
};

use crate::queue::Queue;

mod array;
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
mod vec;

pub use array::ArrayBuffer;
#[cfg(feature = "std")]
pub use vec::VecBuffer;

/// [`Queue`] buffer. It is used together with [`InsertIntoBuffer`].
///
/// # Safety
/// [`Buffer::clear`] *clears* the inserted range from the buffer
/// (see [`InsertIntoBuffer::insert_into`]), meaning new values can be inserted.
pub unsafe trait Buffer: Default {
    /// The slice type returned by [`slice`](Buffer::slice) method.
    type Slice<'a>
    where
        Self: 'a;
    /// Returns the buffer's capacity.
    fn capacity(&self) -> usize;
    /// Returns a slice of the buffer.
    ///
    /// # Safety
    /// Range **must** have been inserted (see [`InsertIntoBuffer::insert_into`]) before calling
    /// this method.
    unsafe fn slice(&mut self, range: Range<usize>) -> Self::Slice<'_>;
    /// Clears the buffer.
    ///
    /// # Safety
    /// Range **must** have been inserted (see [`InsertIntoBuffer::insert_into`]) before calling
    /// this method.
    ///
    /// Calling this method *clears* the inserted value, meaning new values can be inserted.
    unsafe fn clear(&mut self, range: Range<usize>);
}

/// [`Buffer`] value.
///
/// # Safety
/// Range `index..index+value.size()` is considered inserted into the buffer after calling
/// [`InsertIntoBuffer::insert_into`] (see [`Buffer::slice`]/[`Buffer::clear`])
pub unsafe trait InsertIntoBuffer<B: Buffer> {
    /// Returns the size taken by a value in the buffer.
    fn size(&self) -> usize;
    /// Inserts the value into the buffer at the given index.
    ///
    /// # Safety
    /// For every call to this method, the inserted range `index..index+self.size()` **must not**
    /// overlap with a previously inserted one.
    unsafe fn insert_into(self, buffer: &B, index: usize);
}

/// [`Buffer`] kind where value are inserted one by one.
///
/// # Safety
/// `index` is considered inserted into the buffer after calling [`CellBuffer::insert`] (see [`Buffer::slice`]/[`Buffer::clear`])
pub(crate) unsafe trait CellBuffer<T>: Buffer {
    /// Inserts a value into the buffer at the given index.
    ///
    /// # Safety
    /// For every call to this method, `index` **must not** have previously been inserted.
    unsafe fn insert(&self, index: usize, value: T);
}

/// Wrapper to implement [`InsertIntoBuffer`] on iterators.
pub struct ValueIter<I>(pub I);

/// Extension trait to instantiate [`ValueIter`].
pub trait IntoValueIter: Sized {
    /// Iterator type to be wrapped in [`ValueIter`].
    type Iter;
    /// Wrap iterator into [`ValueIter`].
    fn into_value_iter(self) -> ValueIter<Self::Iter>;
}

impl<I> IntoValueIter for I
where
    I: IntoIterator,
    I::IntoIter: ExactSizeIterator,
{
    type Iter = I::IntoIter;
    fn into_value_iter(self) -> ValueIter<Self::Iter> {
        ValueIter(self.into_iter())
    }
}

// SAFETY: `insert_into` does initialize the slice in the buffer
unsafe impl<B, I, T> InsertIntoBuffer<B> for ValueIter<I>
where
    B: CellBuffer<T>,
    I: Iterator<Item = T> + ExactSizeIterator,
{
    #[inline]
    fn size(&self) -> usize {
        self.0.len()
    }

    #[inline]
    unsafe fn insert_into(mut self, buffer: &B, index: usize) {
        // don't loop on iterator, because `ExactSizeIterator` is not a sufficient guarantee
        // for unsafe code
        for i in index..(index + self.0.len()) {
            // SAFETY: function contract encompass `CellBuffer::insert` one
            unsafe { buffer.insert(i, self.0.next().unwrap()) };
        }
    }
}

// SAFETY: `insert_into` does initialize the slice in the buffer
unsafe impl<B, T, const N: usize> InsertIntoBuffer<B> for [T; N]
where
    B: CellBuffer<T>,
{
    #[inline]
    fn size(&self) -> usize {
        N
    }

    #[inline]
    unsafe fn insert_into(self, buffer: &B, index: usize) {
        for (i, elt) in self.into_iter().enumerate() {
            // SAFETY: function contract encompass `CellBuffer::insert` one
            unsafe { buffer.insert(index + i, elt) };
        }
    }
}

/// Resizable [`Buffer`].
pub trait Resize: Buffer {
    /// Resizes the buffer.
    fn resize(&mut self, capacity: usize);
}

/// [`Buffer`] whose values can be drained from.
///
/// # Safety
/// Calling [`Drain::remove`] remove the value inserted at index `index
/// (see [`InsertIntoBuffer::insert_into`])
pub unsafe trait Drain: Buffer {
    /// Value to be removed from the buffer
    type Value;
    /// Removes a value from the buffer at a given index and return it.
    ///
    /// # Safety
    /// A value **must** have been inserted at this index (see [`InsertIntoBuffer::insert_into`])
    /// before calling this method.
    unsafe fn remove(&mut self, index: usize) -> Self::Value;
}

/// [`Buffer`] slice returned by [`Queue::try_dequeue`] (see [`Buffer::Slice`]).
///
/// Buffer is released when the slice is dropped, so the other buffer will be dequeued next,
/// unless [`BufferSlice::requeue`]/[`BufferSlice::into_iter`] is called.
///
/// # Examples
/// ```
/// # use std::ops::Deref;
/// # use swap_buffer_queue::Queue;
/// # use swap_buffer_queue::buffer::VecBuffer;
/// let queue: Queue<VecBuffer<usize>> = Queue::with_capacity(42);
/// queue.try_enqueue([0]).unwrap();
/// queue.try_enqueue([1]).unwrap();
///
/// let slice = queue.try_dequeue().unwrap();
/// assert_eq!(slice.deref(), &[0, 1]);
/// assert_eq!(slice.into_iter().collect::<Vec<_>>(), vec![0, 1]);
/// ```
pub struct BufferSlice<'a, B, N>
where
    B: Buffer,
{
    queue: &'a Queue<B, N>,
    buffer_index: usize,
    range: Range<usize>,
    slice: B::Slice<'a>,
}

impl<'a, B, N> BufferSlice<'a, B, N>
where
    B: Buffer,
{
    #[inline]
    pub(crate) fn new(
        queue: &'a Queue<B, N>,
        buffer_index: usize,
        range: Range<usize>,
        slice: B::Slice<'a>,
    ) -> Self {
        Self {
            queue,
            buffer_index,
            range,
            slice,
        }
    }

    /// Reinsert the buffer at the beginning queue.
    ///
    /// It will thus de dequeued again next.
    ///
    /// # Examples
    /// ```
    /// # use std::ops::Deref;
    /// # use swap_buffer_queue::Queue;
    /// # use swap_buffer_queue::buffer::VecBuffer;
    /// let queue: Queue<VecBuffer<usize>> = Queue::with_capacity(42);
    /// queue.try_enqueue([0]).unwrap();
    /// queue.try_enqueue([1]).unwrap();
    ///
    /// let slice = queue.try_dequeue().unwrap();
    /// assert_eq!(slice.deref(), &[0, 1]);
    /// slice.requeue();
    /// let slice = queue.try_dequeue().unwrap();
    /// assert_eq!(slice.deref(), &[0, 1]);
    /// ```
    #[inline]
    pub fn requeue(self) {
        let slice = ManuallyDrop::new(self);
        slice.queue.requeue(slice.buffer_index, slice.range.clone());
    }
}

impl<'a, B, N> fmt::Debug for BufferSlice<'a, B, N>
where
    B: Buffer,
    B::Slice<'a>: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("BufferSlice").field(&self.slice).finish()
    }
}

impl<'a, B, N> Deref for BufferSlice<'a, B, N>
where
    B: Buffer,
{
    type Target = B::Slice<'a>;

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

impl<'a, B, N> DerefMut for BufferSlice<'a, B, N>
where
    B: Buffer,
{
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.slice
    }
}

impl<'a, B, N> Drop for BufferSlice<'a, B, N>
where
    B: Buffer,
{
    #[inline]
    fn drop(&mut self) {
        self.queue.release(self.buffer_index, self.range.clone());
    }
}

impl<'a, B, N> IntoIterator for BufferSlice<'a, B, N>
where
    B: Buffer + Drain,
{
    type Item = B::Value;
    type IntoIter = BufferIter<'a, B, N>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        let slice = ManuallyDrop::new(self);
        BufferIter {
            queue: slice.queue,
            buffer_index: slice.buffer_index,
            range: slice.range.clone(),
            _phantom: PhantomData,
        }
    }
}

/// [`Buffer`] iterator returned by [`BufferSlice::into_iter`] (see [`Drain`]).
///
/// Buffer is lazily drained, and requeued (see [`BufferSlice::requeue`]) if the iterator is dropped while non exhausted.
///
/// # Examples
/// ```
/// # use std::ops::Deref;
/// # use swap_buffer_queue::Queue;
/// # use swap_buffer_queue::buffer::VecBuffer;
/// let queue: Queue<VecBuffer<usize>> = Queue::with_capacity(42);
/// queue.try_enqueue([0]).unwrap();
/// queue.try_enqueue([1]).unwrap();
///
/// let mut iter = queue.try_dequeue().unwrap().into_iter();
/// assert_eq!(iter.next(), Some(0));
/// drop(iter);
/// let mut iter = queue.try_dequeue().unwrap().into_iter();
/// assert_eq!(iter.next(), Some(1));
/// assert_eq!(iter.next(), None);
/// ```
pub struct OwnedBufferIter<Q, B, N>
where
    Q: AsRef<Queue<B, N>>,
    B: Buffer,
{
    queue: Q,
    buffer_index: usize,
    range: Range<usize>,
    _phantom: PhantomData<Queue<B, N>>,
}

/// Alias of [`OwnedBufferIter`] with a queue reference.
pub type BufferIter<'a, B, N> = OwnedBufferIter<&'a Queue<B, N>, B, N>;

impl<'a, B, N> BufferIter<'a, B, N>
where
    B: Buffer,
{
    /// Convert back a buffer iterator into a buffer slice.
    ///
    /// # Examples
    /// ```
    /// # use std::ops::Deref;
    /// # use std::sync::Arc;
    /// # use swap_buffer_queue::Queue;
    /// # use swap_buffer_queue::buffer::VecBuffer;
    /// let queue: Arc<Queue<VecBuffer<usize>>> = Arc::new(Queue::with_capacity(42));
    /// queue.try_enqueue([0]).unwrap();
    /// queue.try_enqueue([1]).unwrap();
    ///
    /// let iter = queue.try_dequeue().unwrap().into_iter();
    /// let slice = iter.into_slice();
    /// assert_eq!(slice.deref(), &[0, 1]);
    /// ```
    #[inline]
    pub fn into_slice(self) -> BufferSlice<'a, B, N> {
        let iter = ManuallyDrop::new(self);
        BufferSlice {
            queue: iter.queue,
            buffer_index: iter.buffer_index,
            range: iter.range.clone(),
            slice: iter.queue.get_slice(iter.buffer_index, iter.range.clone()),
        }
    }
}

impl<Q, B, N> OwnedBufferIter<Q, B, N>
where
    Q: AsRef<Queue<B, N>>,
    B: Buffer,
{
    /// Returns a "owned" version of the buffer iterator using a "owned" version of the queue.
    ///
    /// # Examples
    /// ```
    /// # use std::ops::Deref;
    /// # use std::sync::Arc;
    /// # use swap_buffer_queue::Queue;
    /// # use swap_buffer_queue::buffer::VecBuffer;
    /// let queue: Arc<Queue<VecBuffer<usize>>> = Arc::new(Queue::with_capacity(42));
    /// queue.try_enqueue([0]).unwrap();
    /// queue.try_enqueue([1]).unwrap();
    ///
    /// let mut iter = queue
    ///     .try_dequeue()
    ///     .unwrap()
    ///     .into_iter()
    ///     .with_owned(queue.clone());
    /// drop(queue); // iter is "owned", queue can be dropped
    /// assert_eq!(iter.next(), Some(0));
    /// assert_eq!(iter.next(), Some(1));
    /// assert_eq!(iter.next(), None);
    /// ```
    #[inline]
    pub fn with_owned<O>(self, queue: O) -> OwnedBufferIter<O, B, N>
    where
        O: AsRef<Queue<B, N>>,
    {
        let iter = ManuallyDrop::new(self);
        assert!(
            ptr::eq(iter.queue.as_ref(), queue.as_ref()),
            "new owner must reference the queue referenced by the iterator"
        );
        OwnedBufferIter {
            queue,
            buffer_index: iter.buffer_index,
            range: iter.range.clone(),
            _phantom: PhantomData,
        }
    }
}

impl<Q, B, N> fmt::Debug for OwnedBufferIter<Q, B, N>
where
    Q: AsRef<Queue<B, N>>,
    B: Buffer,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("BufferIter").field(&self.range).finish()
    }
}

impl<Q, B, N> Drop for OwnedBufferIter<Q, B, N>
where
    Q: AsRef<Queue<B, N>>,
    B: Buffer,
{
    #[inline]
    fn drop(&mut self) {
        self.queue
            .as_ref()
            .requeue(self.buffer_index, self.range.clone());
    }
}

impl<Q, B, N> Iterator for OwnedBufferIter<Q, B, N>
where
    Q: AsRef<Queue<B, N>>,
    B: Buffer + Drain,
{
    type Item = B::Value;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.range.is_empty() {
            return None;
        }
        let value = self
            .queue
            .as_ref()
            .remove(self.buffer_index, self.range.start);
        self.range.start += 1;
        Some(value)
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.range.size_hint()
    }
}

impl<Q, B, N> ExactSizeIterator for OwnedBufferIter<Q, B, N>
where
    Q: AsRef<Queue<B, N>>,
    B: Buffer + Drain,
{
}

impl<Q, B, N> FusedIterator for OwnedBufferIter<Q, B, N>
where
    Q: AsRef<Queue<B, N>>,
    B: Buffer + Drain,
{
}