oximedia-core 0.1.3

Core types and traits for OxiMedia
Documentation
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
//! Lock-free ring buffer for media streaming.
//!
//! Provides:
//! - [`RingBuffer<T>`] — a single-threaded bounded FIFO ring buffer.
//! - [`MediaFrameQueue`] — paired video/audio ring buffers with PTS queues.
//! - [`SpscRingBuffer<T>`] — a truly lock-free single-producer / single-consumer
//!   ring buffer backed by `AtomicUsize` indices, safe for cross-thread use.

#![allow(dead_code)]

use std::collections::VecDeque;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;

// ---------------------------------------------------------------------------
// Statistics
// ---------------------------------------------------------------------------

/// Runtime statistics collected by a [`RingBuffer`].
#[derive(Debug, Clone, Copy, Default)]
pub struct RingBufferStats {
    /// Total capacity of the buffer (number of slots).
    pub capacity: usize,
    /// Number of items currently stored.
    pub len: usize,
    /// Cumulative number of successful pushes (not counting overwrites).
    pub push_count: u64,
    /// Cumulative number of pops.
    pub pop_count: u64,
    /// Number of times a push was rejected (buffer full, non-overwrite mode)
    /// or an item was silently evicted (overwrite mode).
    pub overflow_count: u64,
}

// ---------------------------------------------------------------------------
// RingBuffer<T>
// ---------------------------------------------------------------------------

/// A bounded first-in-first-out ring buffer.
///
/// Items are stored in a flat `Vec<Option<T>>` of length `capacity`.
/// `head` is the next write slot; `tail` is the next read slot.
///
/// # Invariants
///
/// - `head`, `tail` are always in `0..capacity`
/// - `len <= capacity`
pub struct RingBuffer<T> {
    data: Vec<Option<T>>,
    /// Write position (next slot to write into).
    head: usize,
    /// Read position (next slot to read from).
    tail: usize,
    capacity: usize,
    len: usize,
    push_count: u64,
    pop_count: u64,
    overflow_count: u64,
}

impl<T> RingBuffer<T> {
    /// Creates a new `RingBuffer` with the given capacity.
    ///
    /// # Panics
    ///
    /// Panics if `capacity` is zero.
    #[must_use]
    pub fn new(capacity: usize) -> Self {
        assert!(capacity > 0, "RingBuffer capacity must be non-zero");
        let mut data = Vec::with_capacity(capacity);
        for _ in 0..capacity {
            data.push(None);
        }
        Self {
            data,
            head: 0,
            tail: 0,
            capacity,
            len: 0,
            push_count: 0,
            pop_count: 0,
            overflow_count: 0,
        }
    }

    /// Returns the number of items currently in the buffer.
    #[must_use]
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns `true` if the buffer contains no items.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Returns `true` if the buffer is at capacity.
    #[must_use]
    pub fn is_full(&self) -> bool {
        self.len == self.capacity
    }

    /// Returns the maximum number of items the buffer can hold.
    #[must_use]
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Attempts to push `item` onto the back of the buffer.
    ///
    /// Returns `true` on success, `false` if the buffer is full.
    /// When `false` is returned `overflow_count` is incremented and the item
    /// is dropped.
    pub fn push(&mut self, item: T) -> bool {
        if self.is_full() {
            self.overflow_count += 1;
            return false;
        }
        self.data[self.head] = Some(item);
        self.head = (self.head + 1) % self.capacity;
        self.len += 1;
        self.push_count += 1;
        true
    }

    /// Pushes `item` onto the back of the buffer, evicting the oldest item if
    /// the buffer is full.
    ///
    /// When eviction occurs `overflow_count` is incremented.
    pub fn push_overwrite(&mut self, item: T) {
        if self.is_full() {
            // Evict the oldest item by advancing tail.
            self.tail = (self.tail + 1) % self.capacity;
            self.len -= 1;
            self.overflow_count += 1;
        }
        self.data[self.head] = Some(item);
        self.head = (self.head + 1) % self.capacity;
        self.len += 1;
        self.push_count += 1;
    }

    /// Removes and returns the oldest item, or `None` if the buffer is empty.
    pub fn pop(&mut self) -> Option<T> {
        if self.is_empty() {
            return None;
        }
        let item = self.data[self.tail].take();
        self.tail = (self.tail + 1) % self.capacity;
        self.len -= 1;
        self.pop_count += 1;
        item
    }

    /// Returns a reference to the oldest item without removing it, or `None`
    /// if the buffer is empty.
    #[must_use]
    pub fn peek(&self) -> Option<&T> {
        if self.is_empty() {
            return None;
        }
        self.data[self.tail].as_ref()
    }

    /// Removes all items from the buffer.
    pub fn clear(&mut self) {
        for slot in &mut self.data {
            *slot = None;
        }
        self.head = 0;
        self.tail = 0;
        self.len = 0;
    }

    /// Returns a snapshot of runtime statistics.
    #[must_use]
    pub fn stats(&self) -> RingBufferStats {
        RingBufferStats {
            capacity: self.capacity,
            len: self.len,
            push_count: self.push_count,
            pop_count: self.pop_count,
            overflow_count: self.overflow_count,
        }
    }

    /// Returns an iterator over references to the buffered items in insertion
    /// order (oldest first).
    pub fn iter(&self) -> RingBufferIter<'_, T> {
        RingBufferIter {
            buffer: self,
            index: 0,
        }
    }
}

// ---------------------------------------------------------------------------
// Iterator
// ---------------------------------------------------------------------------

/// Iterator produced by [`RingBuffer::iter`].
pub struct RingBufferIter<'a, T> {
    buffer: &'a RingBuffer<T>,
    index: usize,
}

impl<'a, T> Iterator for RingBufferIter<'a, T> {
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index >= self.buffer.len {
            return None;
        }
        let slot = (self.buffer.tail + self.index) % self.buffer.capacity;
        self.index += 1;
        self.buffer.data[slot].as_ref()
    }
}

// ---------------------------------------------------------------------------
// MediaFrameQueue
// ---------------------------------------------------------------------------

/// A paired video/audio ring buffer with associated PTS queues.
///
/// Video frames are stored as raw byte slices (`Vec<u8>`) and audio frames
/// as PCM sample vectors (`Vec<f32>`).  Each ring buffer is accompanied by
/// a [`VecDeque`] of presentation timestamps so that callers can correlate
/// frames with their timing information.
pub struct MediaFrameQueue {
    video: RingBuffer<Vec<u8>>,
    audio: RingBuffer<Vec<f32>>,
    /// PTS values corresponding to video frames in insertion order.
    pub video_pts: VecDeque<i64>,
    /// PTS values corresponding to audio frames in insertion order.
    pub audio_pts: VecDeque<i64>,
}

impl MediaFrameQueue {
    /// Creates a new `MediaFrameQueue` with the specified per-stream
    /// capacities.
    ///
    /// # Panics
    ///
    /// Panics if either capacity is zero.
    #[must_use]
    pub fn new(video_cap: usize, audio_cap: usize) -> Self {
        Self {
            video: RingBuffer::new(video_cap),
            audio: RingBuffer::new(audio_cap),
            video_pts: VecDeque::with_capacity(video_cap),
            audio_pts: VecDeque::with_capacity(audio_cap),
        }
    }

    /// Pushes a video frame along with its PTS.
    ///
    /// Returns `true` on success, `false` if the video ring buffer is full.
    /// When `false` is returned neither the frame nor its PTS is queued.
    pub fn push_video(&mut self, frame: Vec<u8>, pts: i64) -> bool {
        if self.video.push(frame) {
            self.video_pts.push_back(pts);
            true
        } else {
            false
        }
    }

    /// Pushes an audio frame along with its PTS.
    ///
    /// Returns `true` on success, `false` if the audio ring buffer is full.
    pub fn push_audio(&mut self, samples: Vec<f32>, pts: i64) -> bool {
        if self.audio.push(samples) {
            self.audio_pts.push_back(pts);
            true
        } else {
            false
        }
    }

    /// Pops the oldest video frame and its PTS, or `None` if empty.
    pub fn pop_video(&mut self) -> Option<(Vec<u8>, i64)> {
        let frame = self.video.pop()?;
        let pts = self.video_pts.pop_front().unwrap_or(0);
        Some((frame, pts))
    }

    /// Pops the oldest audio frame and its PTS, or `None` if empty.
    pub fn pop_audio(&mut self) -> Option<(Vec<f32>, i64)> {
        let samples = self.audio.pop()?;
        let pts = self.audio_pts.pop_front().unwrap_or(0);
        Some((samples, pts))
    }

    /// Returns the A/V sync offset in milliseconds, defined as
    /// `video_front_pts - audio_front_pts`.
    ///
    /// Returns `None` if either stream has no queued frames.
    ///
    /// A positive value means the video is ahead of the audio; a negative
    /// value means the audio is ahead.
    #[must_use]
    pub fn sync_offset_ms(&self) -> Option<i64> {
        let v = *self.video_pts.front()?;
        let a = *self.audio_pts.front()?;
        Some(v - a)
    }

    /// Returns the number of video frames currently buffered.
    #[must_use]
    pub fn video_len(&self) -> usize {
        self.video.len()
    }

    /// Returns the number of audio frames currently buffered.
    #[must_use]
    pub fn audio_len(&self) -> usize {
        self.audio.len()
    }
}

// ---------------------------------------------------------------------------
// SpscRingBuffer<T>  —  lock-free single-producer / single-consumer buffer
// ---------------------------------------------------------------------------

/// Lock-free single-producer / single-consumer (SPSC) ring buffer.
///
/// This buffer uses atomic load/store with appropriate memory ordering so that
/// **one** producer thread may call [`push`](SpscRingBuffer::push) while
/// **one** consumer thread may call [`pop`](SpscRingBuffer::pop) concurrently,
/// with no locks and no data races.
///
/// The capacity is always rounded up to the next power of two so that index
/// masking can be used instead of modulo.
///
/// # Safety
///
/// This type is `Send` + `Sync` when `T: Send`.  The safety invariant is that
/// *at most one thread writes* and *at most one thread reads* at any time.
///
/// # Example
///
/// ```
/// use oximedia_core::ring_buffer::SpscRingBuffer;
/// use std::sync::Arc;
///
/// let buf: Arc<SpscRingBuffer<u32>> = Arc::new(SpscRingBuffer::new(8));
/// let producer = Arc::clone(&buf);
/// let consumer = Arc::clone(&buf);
///
/// let handle = std::thread::spawn(move || {
///     for i in 0..4_u32 {
///         while !producer.push(i) {} // busy-wait until slot available
///     }
/// });
///
/// handle.join().expect("thread ok");
/// for i in 0..4_u32 {
///     assert_eq!(consumer.pop(), Some(i));
/// }
/// ```
pub struct SpscRingBuffer<T> {
    /// Slots storing items wrapped in `Mutex<Option<T>>`.
    /// The SPSC discipline ensures only one thread writes and one reads at
    /// any slot at a time; the `Mutex` satisfies the `Sync` bound without
    /// requiring any `unsafe` code.
    slots: Box<[Mutex<Option<T>>]>,
    /// Next write index (owned exclusively by the producer).
    head: AtomicUsize,
    /// Next read index (owned exclusively by the consumer).
    tail: AtomicUsize,
    /// Capacity — always a power of two.
    capacity: usize,
    /// Bit-mask for fast modulo: `index & mask == index % capacity`.
    mask: usize,
}

// `Mutex<Option<T>>: Send + Sync` when `T: Send`, so `SpscRingBuffer<T>` is
// `Send + Sync` without any `unsafe impl` blocks.

impl<T> SpscRingBuffer<T> {
    /// Creates a new `SpscRingBuffer` with at least `min_capacity` slots.
    ///
    /// The actual capacity is the smallest power of two >= `min_capacity` and
    /// >= 2.
    ///
    /// # Panics
    ///
    /// Panics if the rounded-up capacity overflows `usize`.
    #[must_use]
    pub fn new(min_capacity: usize) -> Self {
        let capacity = min_capacity.max(2).next_power_of_two();
        let mask = capacity - 1;
        let slots: Box<[Mutex<Option<T>>]> = (0..capacity)
            .map(|_| Mutex::new(None))
            .collect::<Vec<_>>()
            .into_boxed_slice();
        Self {
            slots,
            head: AtomicUsize::new(0),
            tail: AtomicUsize::new(0),
            capacity,
            mask,
        }
    }

    /// Returns the buffer capacity (always a power of two).
    #[must_use]
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Returns the number of items currently in the buffer.
    ///
    /// Because `head` and `tail` are owned by separate threads, this value is
    /// approximate when called from either thread; it is exact when called from
    /// a thread that has exclusive access to both.
    #[must_use]
    pub fn len(&self) -> usize {
        let head = self.head.load(Ordering::Acquire);
        let tail = self.tail.load(Ordering::Acquire);
        head.wrapping_sub(tail)
    }

    /// Returns `true` if the buffer contains no items.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.head.load(Ordering::Acquire) == self.tail.load(Ordering::Acquire)
    }

    /// Returns `true` if the buffer is full.
    #[must_use]
    pub fn is_full(&self) -> bool {
        self.len() == self.capacity
    }

    /// Attempts to push `item` into the buffer.
    ///
    /// Returns `true` on success, `false` if the buffer is full (item is
    /// dropped).
    ///
    /// **Must only be called from the producer thread.**
    pub fn push(&self, item: T) -> bool {
        let head = self.head.load(Ordering::Relaxed);
        let tail = self.tail.load(Ordering::Acquire);
        if head.wrapping_sub(tail) == self.capacity {
            return false; // full
        }
        let slot = &self.slots[head & self.mask];
        // Only the producer thread accesses head's slot; lock is uncontended.
        if let Ok(mut guard) = slot.lock() {
            *guard = Some(item);
        }
        self.head.store(head.wrapping_add(1), Ordering::Release);
        true
    }

    /// Attempts to pop the oldest item.
    ///
    /// Returns `Some(item)` if the buffer is non-empty, or `None` if empty.
    ///
    /// **Must only be called from the consumer thread.**
    pub fn pop(&self) -> Option<T> {
        let tail = self.tail.load(Ordering::Relaxed);
        let head = self.head.load(Ordering::Acquire);
        if head == tail {
            return None; // empty
        }
        let slot = &self.slots[tail & self.mask];
        // Only the consumer thread accesses tail's slot; lock is uncontended.
        let item = slot.lock().ok().and_then(|mut g| g.take());
        self.tail.store(tail.wrapping_add(1), Ordering::Release);
        item
    }
}

impl<T: std::fmt::Debug> std::fmt::Debug for SpscRingBuffer<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SpscRingBuffer")
            .field("capacity", &self.capacity)
            .field("len", &self.len())
            .finish()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // --- RingBuffer tests ---

    #[test]
    fn test_new_empty() {
        let rb: RingBuffer<i32> = RingBuffer::new(4);
        assert!(rb.is_empty());
        assert!(!rb.is_full());
        assert_eq!(rb.len(), 0);
        assert_eq!(rb.capacity(), 4);
    }

    #[test]
    fn test_push_pop_ordering() {
        let mut rb = RingBuffer::new(4);
        assert!(rb.push(1));
        assert!(rb.push(2));
        assert!(rb.push(3));
        assert_eq!(rb.pop(), Some(1));
        assert_eq!(rb.pop(), Some(2));
        assert_eq!(rb.pop(), Some(3));
        assert_eq!(rb.pop(), None);
    }

    #[test]
    fn test_push_when_full_returns_false() {
        let mut rb = RingBuffer::new(2);
        assert!(rb.push(10));
        assert!(rb.push(20));
        assert!(rb.is_full());
        assert!(!rb.push(30));
        assert_eq!(rb.stats().overflow_count, 1);
    }

    #[test]
    fn test_push_overwrite_evicts_oldest() {
        let mut rb = RingBuffer::new(3);
        rb.push_overwrite(1);
        rb.push_overwrite(2);
        rb.push_overwrite(3);
        // Buffer is now full; the next push should evict 1.
        rb.push_overwrite(4);
        assert_eq!(rb.len(), 3);
        assert_eq!(rb.pop(), Some(2));
        assert_eq!(rb.pop(), Some(3));
        assert_eq!(rb.pop(), Some(4));
    }

    #[test]
    fn test_peek_does_not_consume() {
        let mut rb = RingBuffer::new(4);
        rb.push(42);
        assert_eq!(rb.peek(), Some(&42));
        assert_eq!(rb.len(), 1);
        assert_eq!(rb.pop(), Some(42));
    }

    #[test]
    fn test_clear() {
        let mut rb = RingBuffer::new(4);
        rb.push(1);
        rb.push(2);
        rb.clear();
        assert!(rb.is_empty());
        assert_eq!(rb.pop(), None);
    }

    #[test]
    fn test_iter_order() {
        let mut rb = RingBuffer::new(5);
        for i in 0..4_i32 {
            rb.push(i);
        }
        let collected: Vec<i32> = rb.iter().copied().collect();
        assert_eq!(collected, vec![0, 1, 2, 3]);
    }

    #[test]
    fn test_wrap_around() {
        let mut rb = RingBuffer::new(3);
        rb.push(1);
        rb.push(2);
        rb.pop(); // consume 1; tail advances
        rb.push(3);
        rb.push(4);
        assert_eq!(rb.len(), 3);
        assert_eq!(rb.pop(), Some(2));
        assert_eq!(rb.pop(), Some(3));
        assert_eq!(rb.pop(), Some(4));
    }

    #[test]
    fn test_stats_tracking() {
        let mut rb = RingBuffer::new(2);
        rb.push(1);
        rb.push(2);
        rb.push(3); // overflow
        rb.pop();
        let s = rb.stats();
        assert_eq!(s.push_count, 2);
        assert_eq!(s.pop_count, 1);
        assert_eq!(s.overflow_count, 1);
        assert_eq!(s.len, 1);
    }

    // --- MediaFrameQueue tests ---

    #[test]
    fn test_media_frame_queue_push_pop_video() {
        let mut q = MediaFrameQueue::new(4, 4);
        assert!(q.push_video(vec![1, 2, 3], 1000));
        let (frame, pts) = q.pop_video().expect("should have video");
        assert_eq!(frame, vec![1, 2, 3]);
        assert_eq!(pts, 1000);
    }

    #[test]
    fn test_media_frame_queue_push_pop_audio() {
        let mut q = MediaFrameQueue::new(4, 4);
        assert!(q.push_audio(vec![0.5_f32, -0.5], 2000));
        let (samples, pts) = q.pop_audio().expect("should have audio");
        assert_eq!(pts, 2000);
        assert!((samples[0] - 0.5).abs() < 1e-6);
    }

    #[test]
    fn test_sync_offset_ms() {
        let mut q = MediaFrameQueue::new(4, 4);
        q.push_video(vec![], 1050);
        q.push_audio(vec![], 1000);
        assert_eq!(q.sync_offset_ms(), Some(50));
    }

    #[test]
    fn test_sync_offset_ms_empty() {
        let q = MediaFrameQueue::new(4, 4);
        assert!(q.sync_offset_ms().is_none());
    }

    // --- SpscRingBuffer tests ---

    #[test]
    fn test_spsc_new_capacity_power_of_two() {
        let buf: SpscRingBuffer<u32> = SpscRingBuffer::new(5);
        assert_eq!(buf.capacity(), 8); // next power of two >= 5
        let buf2: SpscRingBuffer<u32> = SpscRingBuffer::new(8);
        assert_eq!(buf2.capacity(), 8);
        let buf3: SpscRingBuffer<u32> = SpscRingBuffer::new(1);
        assert_eq!(buf3.capacity(), 2); // minimum 2
    }

    #[test]
    fn test_spsc_push_pop_fifo() {
        let buf: SpscRingBuffer<i32> = SpscRingBuffer::new(4);
        assert!(buf.push(10));
        assert!(buf.push(20));
        assert!(buf.push(30));
        assert_eq!(buf.pop(), Some(10));
        assert_eq!(buf.pop(), Some(20));
        assert_eq!(buf.pop(), Some(30));
        assert_eq!(buf.pop(), None);
    }

    #[test]
    fn test_spsc_is_empty_is_full() {
        let buf: SpscRingBuffer<u8> = SpscRingBuffer::new(2);
        assert!(buf.is_empty());
        assert!(!buf.is_full());
        buf.push(1);
        buf.push(2);
        assert!(buf.is_full());
        assert!(!buf.is_empty());
    }

    #[test]
    fn test_spsc_push_when_full_returns_false() {
        let buf: SpscRingBuffer<u8> = SpscRingBuffer::new(2);
        assert!(buf.push(1));
        assert!(buf.push(2));
        assert!(!buf.push(3)); // full
        assert_eq!(buf.len(), 2);
    }

    #[test]
    fn test_spsc_wrap_around() {
        let buf: SpscRingBuffer<u32> = SpscRingBuffer::new(4);
        // Fill
        for i in 0..4 {
            assert!(buf.push(i));
        }
        // Drain 2
        assert_eq!(buf.pop(), Some(0));
        assert_eq!(buf.pop(), Some(1));
        // Push 2 more (wraps around in the ring)
        assert!(buf.push(4));
        assert!(buf.push(5));
        // Drain all
        assert_eq!(buf.pop(), Some(2));
        assert_eq!(buf.pop(), Some(3));
        assert_eq!(buf.pop(), Some(4));
        assert_eq!(buf.pop(), Some(5));
        assert_eq!(buf.pop(), None);
    }

    #[test]
    fn test_spsc_len() {
        let buf: SpscRingBuffer<u32> = SpscRingBuffer::new(8);
        assert_eq!(buf.len(), 0);
        buf.push(1);
        buf.push(2);
        assert_eq!(buf.len(), 2);
        buf.pop();
        assert_eq!(buf.len(), 1);
    }

    #[test]
    fn test_spsc_threaded_producer_consumer() {
        use std::sync::Arc;
        const N: u32 = 1000;
        let buf: Arc<SpscRingBuffer<u32>> = Arc::new(SpscRingBuffer::new(64));
        let producer = Arc::clone(&buf);
        let handle = std::thread::spawn(move || {
            for i in 0..N {
                while !producer.push(i) {
                    std::thread::yield_now();
                }
            }
        });
        let mut received = Vec::with_capacity(N as usize);
        while received.len() < N as usize {
            if let Some(v) = buf.pop() {
                received.push(v);
            } else {
                std::thread::yield_now();
            }
        }
        handle.join().expect("producer thread should not panic");
        let expected: Vec<u32> = (0..N).collect();
        assert_eq!(received, expected);
    }
}