ph-eventing 0.2.0

Deterministic zero-allocation ring buffers for no-std embedded targets: bounded behaviour, no hidden cost, Loom-verified orderings
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
//! Bounded SPSC event buffer with backpressure — no heap, no alloc.
//!
//! [`EventBuf`] is a fixed-size, lock-free, single-producer single-consumer
//! ring buffer that **rejects** pushes when full instead of overwriting.
//! This gives the producer explicit backpressure so no events are silently
//! lost.
//!
//! # When to use
//! Use `EventBuf` when every event matters and the producer can afford to
//! handle a "buffer full" signal (retry, log, or apply its own policy).
//! If losing old events is acceptable, prefer [`crate::SeqRing`].
//! If you only need a single-owner ring, see [`crate::RingBuf`].
//!
//! # Memory ordering
//! This is a classic Lamport SPSC queue:
//! - The producer owns `head` (Relaxed load, Release store) and reads
//!   `tail` with Acquire to see consumer progress.
//! - The consumer owns `tail` (Relaxed load, Release store) and reads
//!   `head` with Acquire to see producer progress.
//! - A slot is written before `head` is advanced and read before `tail` is
//!   advanced, so the Release/Acquire pairs on the cursors act as the
//!   publication fence.
//! - The producer and consumer never touch the same slot: `push` writes at
//!   `head` only while `head - tail < N`, so there is no data race on the
//!   slots themselves, only on the cursors.
//! - [`EventBuf::len`] is the one observer that reads both cursors, so it
//!   brackets its `head` load between two `tail` samples to get a consistent
//!   pair.
//!
//! # Example
//! ```
//! use ph_eventing::EventBuf;
//!
//! let buf = EventBuf::<u32, 4>::new();
//! let producer = buf.try_producer().expect("producer");
//! let consumer = buf.try_consumer().expect("consumer");
//!
//! assert!(producer.push(1).is_ok());
//! assert!(producer.push(2).is_ok());
//! assert_eq!(consumer.peek(), Some(1));
//! assert_eq!(consumer.pop(), Some(1));
//! assert_eq!(consumer.pop(), Some(2));
//! assert_eq!(consumer.pop(), None); // empty
//! ```

use crate::sync::{AtomicBool, AtomicU32, Ordering, TrackedCell, fence};
use core::cell::Cell;
use core::marker::PhantomData;
use core::mem::MaybeUninit;

// Const on the host path so `EventBuf::new` can be const. Loom's cell is not
// const-constructible, so the Loom build keeps a non-const helper.
//
// Prefer `[const { … }; N]` over `array::from_fn`: the latter is not
// const-callable with these constructors on the MSRV toolchain.
#[cfg(not(loom))]
const fn slot_array<T, const N: usize>() -> [TrackedCell<MaybeUninit<T>>; N] {
    [const { TrackedCell::new(MaybeUninit::uninit()) }; N]
}

#[cfg(loom)]
fn slot_array<T, const N: usize>() -> [TrackedCell<MaybeUninit<T>>; N] {
    core::array::from_fn(|_| TrackedCell::new(MaybeUninit::uninit()))
}

/// How many times [`EventBuf::len`] retries its snapshot before falling back
/// to a clamped estimate. Bounded so `len` is always wait-free.
const RETRY_LIMIT: usize = 2;

/// Bounded SPSC event buffer with backpressure.
///
/// When the buffer is full, [`Producer::push`] returns `Err(val)` instead
/// of overwriting, giving the producer a chance to retry, drop, or log.
/// The consumer drains items with [`Consumer::pop`] or [`Consumer::drain`],
/// and can inspect the oldest item with [`Consumer::peek`] without consuming it.
///
/// # Panics
/// - `EventBuf::new()` fails to compile (const assertion) when `N == 0` on the
///   host path; under Loom it panics at runtime.
/// - `producer()` / `consumer()` panic if called while another handle of
///   the same kind is already active. Use [`EventBuf::try_producer`] /
///   [`EventBuf::try_consumer`] for a fallible alternative.
pub struct EventBuf<T: Copy, const N: usize> {
    head: AtomicU32,
    tail: AtomicU32,
    slots: [TrackedCell<MaybeUninit<T>>; N],
    producer_taken: AtomicBool,
    consumer_taken: AtomicBool,
}

// SAFETY: EventBuf is Sync because the producer/consumer handles enforce
// SPSC usage, and the head/tail cursors are accessed via atomics with
// Release/Acquire ordering that guarantees slot visibility. T: Send ensures
// values can be transferred across threads safely.
unsafe impl<T: Copy + Send, const N: usize> Sync for EventBuf<T, N> {}

impl<T: Copy, const N: usize> EventBuf<T, N> {
    /// Create a new, empty event buffer.
    ///
    /// On the normal (non-Loom) build this is a `const fn`, so the buffer can
    /// be placed in a `static`:
    /// `static BUF: EventBuf<u32, 64> = EventBuf::new();`.
    /// Under `--cfg loom` it is deliberately non-const — Loom's atomics are
    /// not const-constructible.
    ///
    /// # Capacity `0` is a build failure
    /// The `N > 0` check is a *const* assertion, so a zero-capacity buffer
    /// cannot be constructed at all -- there is no runtime panic left to
    /// catch, and therefore no way to write the negative case as a `#[test]`.
    /// This `compile_fail` doctest is that coverage, and pinning the error code
    /// keeps it honest: without it the test would also pass on a typo.
    ///
    /// ```compile_fail,E0080
    /// let _ = ph_eventing::EventBuf::<u32, 0>::new();
    /// ```
    ///
    /// # Panics
    /// Does not panic on the host path. Under Loom, where `new` is non-const,
    /// `N == 0` is a runtime assertion instead.
    #[cfg(not(loom))]
    pub const fn new() -> Self {
        const {
            assert!(N > 0, "EventBuf capacity N must be > 0");
        }
        Self {
            head: AtomicU32::new(0),
            tail: AtomicU32::new(0),
            slots: slot_array::<T, N>(),
            producer_taken: AtomicBool::new(false),
            consumer_taken: AtomicBool::new(false),
        }
    }

    /// Create a new, empty event buffer (Loom build — non-const).
    ///
    /// # Panics
    /// Panics if `N == 0`.
    #[cfg(loom)]
    pub fn new() -> Self {
        assert!(N > 0, "EventBuf capacity N must be > 0");
        Self {
            head: AtomicU32::new(0),
            tail: AtomicU32::new(0),
            slots: slot_array::<T, N>(),
            producer_taken: AtomicBool::new(false),
            consumer_taken: AtomicBool::new(false),
        }
    }

    #[inline(always)]
    const fn slot_index(pos: u32) -> usize {
        (pos as usize) % N
    }

    /// Maximum number of items the buffer can hold.
    #[inline]
    pub const fn capacity(&self) -> usize {
        N
    }

    /// Approximate number of items currently buffered.
    ///
    /// Returns a consistent `(tail, head)` snapshot. The value may still be
    /// stale by the time the caller acts on it, but it will never spuriously
    /// exceed [`capacity`](Self::capacity).
    ///
    /// This never blocks: it makes a bounded number of attempts and then falls
    /// back to a clamped estimate, so a busy consumer cannot stall the caller.
    #[inline]
    pub fn len(&self) -> usize {
        // Seqlock-style read. Sampling `tail` on both sides of the `head` load
        // and requiring the samples to match means `head` was observed while
        // `tail` held still, so `head.wrapping_sub(tail)` cannot appear as a
        // huge unsigned value after a concurrent consumer advance.
        //
        // The two barriers pin the `head` load between the samples. They also
        // make equality a sound bound: if `h` observes a producer publication
        // that reused consumer-freed space, the following Acquire fence
        // synchronizes through that Relaxed load. The producer acquired the
        // newer `tail` before publishing `h`, so `t2` cannot then observe an
        // older tail. Thus `t1 == t2` implies `h - t1 <= N`.
        //
        // This depends on EventBuf's backpressure protocol: the producer reads
        // `tail` with Acquire before advancing `head`. It is not a generic
        // double-sampling property. See AGENTS.md for the full happens-before
        // chain.
        for _ in 0..RETRY_LIMIT {
            let t1 = self.tail.load(Ordering::Acquire);
            let h = self.head.load(Ordering::Relaxed);
            fence(Ordering::Acquire);
            let t2 = self.tail.load(Ordering::Relaxed);

            if t1 == t2 {
                return h.wrapping_sub(t1) as usize;
            }
        }

        // The consumer moved during every attempt. Read `tail` first so a
        // further advance can only make the count an over-estimate rather
        // than an underflow, then clamp to preserve the capacity bound.
        let t = self.tail.load(Ordering::Acquire);
        let h = self.head.load(Ordering::Relaxed);
        (h.wrapping_sub(t) as usize).min(N)
    }

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

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

    /// Try to create the producer handle.
    ///
    /// Returns `None` if a producer is already active. Prefer this over
    /// [`producer`](Self::producer) when fallible bring-up is needed.
    #[inline]
    pub fn try_producer(&self) -> Option<Producer<'_, T, N>> {
        if self.producer_taken.swap(true, Ordering::AcqRel) {
            None
        } else {
            Some(Producer {
                buf: self,
                _not_sync: PhantomData,
            })
        }
    }

    /// Create the producer handle. Only one producer may be active.
    ///
    /// # Deprecated
    /// Prefer [`try_producer`](Self::try_producer). This crate targets firmware,
    /// where a panic is a reset and the panic machinery itself costs flash — a
    /// code-size probe shows no panic strings reach the binary when only the
    /// `try_*` constructors are used. The shorter, more discoverable name being
    /// the hazardous one is the inversion this deprecation exists to correct.
    ///
    /// Still sound, still tested, and convenient on a host where a panic is just
    /// a failed test. Scheduled for removal in 0.3.0.
    ///
    /// # Panics
    /// Panics if a producer handle is already active.
    #[deprecated(
        since = "0.2.0",
        note = "on an embedded target a panic is a reset, and the panic machinery costs flash; use try_producer() and handle None"
    )]
    #[inline]
    pub fn producer(&self) -> Producer<'_, T, N> {
        self.try_producer()
            .expect("EventBuf: only one Producer may be active at a time")
    }

    /// Try to create the consumer handle.
    ///
    /// Returns `None` if a consumer is already active. Prefer this over
    /// [`consumer`](Self::consumer) when fallible bring-up is needed.
    #[inline]
    pub fn try_consumer(&self) -> Option<Consumer<'_, T, N>> {
        if self.consumer_taken.swap(true, Ordering::AcqRel) {
            None
        } else {
            Some(Consumer {
                buf: self,
                _not_sync: PhantomData,
            })
        }
    }

    /// Create the consumer handle. Only one consumer may be active.
    ///
    /// # Deprecated
    /// Prefer [`try_consumer`](Self::try_consumer). This crate targets firmware,
    /// where a panic is a reset and the panic machinery itself costs flash — a
    /// code-size probe shows no panic strings reach the binary when only the
    /// `try_*` constructors are used. The shorter, more discoverable name being
    /// the hazardous one is the inversion this deprecation exists to correct.
    ///
    /// Still sound, still tested, and convenient on a host where a panic is just
    /// a failed test. Scheduled for removal in 0.3.0.
    ///
    /// # Panics
    /// Panics if a consumer handle is already active.
    #[deprecated(
        since = "0.2.0",
        note = "on an embedded target a panic is a reset, and the panic machinery costs flash; use try_consumer() and handle None"
    )]
    #[inline]
    pub fn consumer(&self) -> Consumer<'_, T, N> {
        self.try_consumer()
            .expect("EventBuf: only one Consumer may be active at a time")
    }
}

impl<T: Copy, const N: usize> Default for EventBuf<T, N> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Copy, const N: usize> core::fmt::Debug for EventBuf<T, N> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("EventBuf")
            .field("len", &self.len())
            .field("capacity", &N)
            .finish()
    }
}

/// Write handle for an [`EventBuf`].
///
/// Dropping the producer releases the slot so a new one can be created.
pub struct Producer<'a, T: Copy, const N: usize> {
    buf: &'a EventBuf<T, N>,
    _not_sync: PhantomData<Cell<()>>,
}

impl<T: Copy, const N: usize> Producer<'_, T, N> {
    /// Try to push a value into the buffer.
    ///
    /// Returns `Ok(())` on success, or `Err(val)` if the buffer is full
    /// (the value is returned to the caller so nothing is lost).
    #[inline]
    pub fn push(&self, val: T) -> Result<(), T> {
        let head = self.buf.head.load(Ordering::Relaxed);
        let tail = self.buf.tail.load(Ordering::Acquire);
        if head.wrapping_sub(tail) as usize >= N {
            return Err(val);
        }
        let idx = EventBuf::<T, N>::slot_index(head);
        // SAFETY: producer is the only writer to this slot; the consumer
        // will not read it until head is advanced (Release below).
        self.buf.slots[idx].with_mut(|slot| unsafe { (*slot).write(val) });
        self.buf.head.store(head.wrapping_add(1), Ordering::Release);
        Ok(())
    }
}

impl<T: Copy, const N: usize> Drop for Producer<'_, T, N> {
    fn drop(&mut self) {
        self.buf.producer_taken.store(false, Ordering::Release);
    }
}

impl<T: Copy, const N: usize> core::fmt::Debug for Producer<'_, T, N> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("event_buf::Producer")
            .field("capacity", &N)
            .finish()
    }
}

/// Read handle for an [`EventBuf`].
///
/// Dropping the consumer releases the slot so a new one can be created.
pub struct Consumer<'a, T: Copy, const N: usize> {
    buf: &'a EventBuf<T, N>,
    _not_sync: PhantomData<Cell<()>>,
}

impl<T: Copy, const N: usize> Consumer<'_, T, N> {
    /// Pop the oldest item from the buffer.
    ///
    /// Returns `None` if the buffer is empty.
    #[inline]
    pub fn pop(&self) -> Option<T> {
        let tail = self.buf.tail.load(Ordering::Relaxed);
        let head = self.buf.head.load(Ordering::Acquire);
        if tail == head {
            return None;
        }
        let idx = EventBuf::<T, N>::slot_index(tail);
        // SAFETY: consumer is the only reader of this slot; the producer
        // will not overwrite it until tail is advanced (Release below).
        let val = self.buf.slots[idx].with(|slot| unsafe { (*slot).assume_init_read() });
        self.buf.tail.store(tail.wrapping_add(1), Ordering::Release);
        Some(val)
    }

    /// Copy the oldest item without removing it.
    ///
    /// Returns `None` if the buffer is empty. The consumer cursor is not
    /// advanced, so a following [`pop`](Self::pop) returns the same value.
    #[inline]
    pub fn peek(&self) -> Option<T> {
        let tail = self.buf.tail.load(Ordering::Relaxed);
        let head = self.buf.head.load(Ordering::Acquire);
        if tail == head {
            return None;
        }
        let idx = EventBuf::<T, N>::slot_index(tail);
        // SAFETY: same slot exclusivity as `pop` — the producer will not
        // overwrite this slot until `tail` advances. `T: Copy`, so reading
        // without advancing leaves a valid value for a later `pop`.
        Some(self.buf.slots[idx].with(|slot| unsafe { (*slot).assume_init_read() }))
    }

    /// Drain up to `max` items, passing each to `hook`.
    ///
    /// Returns the number of items consumed.
    #[inline]
    pub fn drain(&self, max: usize, mut hook: impl FnMut(T)) -> usize {
        let mut count = 0;
        while count < max {
            match self.pop() {
                Some(val) => {
                    hook(val);
                    count += 1;
                }
                None => break,
            }
        }
        count
    }
}

impl<T: Copy, const N: usize> Drop for Consumer<'_, T, N> {
    fn drop(&mut self) {
        self.buf.consumer_taken.store(false, Ordering::Release);
    }
}

impl<T: Copy, const N: usize> core::fmt::Debug for Consumer<'_, T, N> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("event_buf::Consumer")
            .field("capacity", &N)
            .finish()
    }
}

impl<T: Copy, const N: usize> crate::traits::Sink<T> for Producer<'_, T, N> {
    type Error = T;

    #[inline]
    fn try_push(&mut self, val: T) -> Result<(), T> {
        self.push(val)
    }
}

impl<T: Copy, const N: usize> crate::traits::Source<T> for Consumer<'_, T, N> {
    #[inline]
    fn try_pop(&mut self) -> Option<T> {
        self.pop()
    }
}

#[cfg(test)]
mod tests {
    // The deprecated `producer()` / `consumer()` remain public API until 0.3.0,
    // so these tests are their coverage -- including the two that assert the
    // panic message. Allowing the lint here rather than at the crate root keeps
    // the warning live for library code, which is where it should bite.
    #![allow(deprecated)]

    use super::*;

    #[test]
    fn new_buf_is_empty() {
        let buf = EventBuf::<u32, 4>::new();
        assert!(buf.is_empty());
        assert!(!buf.is_full());
        assert_eq!(buf.len(), 0);
        assert_eq!(buf.capacity(), 4);
    }

    #[test]
    fn push_and_pop_fifo() {
        let buf = EventBuf::<u32, 4>::new();
        let p = buf.producer();
        let c = buf.consumer();

        assert!(p.push(10).is_ok());
        assert!(p.push(20).is_ok());
        assert!(p.push(30).is_ok());

        assert_eq!(c.pop(), Some(10));
        assert_eq!(c.pop(), Some(20));
        assert_eq!(c.pop(), Some(30));
        assert_eq!(c.pop(), None);
    }

    #[test]
    fn push_rejects_when_full() {
        let buf = EventBuf::<u32, 2>::new();
        let p = buf.producer();
        let c = buf.consumer();

        assert!(p.push(1).is_ok());
        assert!(p.push(2).is_ok());
        assert_eq!(p.push(3), Err(3)); // full — value returned

        // drain one, then push succeeds
        assert_eq!(c.pop(), Some(1));
        assert!(p.push(3).is_ok());
    }

    #[test]
    fn drain_returns_count() {
        let buf = EventBuf::<u32, 8>::new();
        let p = buf.producer();
        let c = buf.consumer();

        for i in 0..5 {
            p.push(i).unwrap();
        }

        let mut out = std::vec::Vec::new();
        let n = c.drain(3, |v| out.push(v));
        assert_eq!(n, 3);
        assert_eq!(out, [0, 1, 2]);

        // remaining
        let n = c.drain(100, |v| out.push(v));
        assert_eq!(n, 2);
        assert_eq!(out, [0, 1, 2, 3, 4]);
    }

    #[test]
    fn drain_on_empty_returns_zero() {
        let buf = EventBuf::<u32, 4>::new();
        let _p = buf.producer();
        let c = buf.consumer();

        let n = c.drain(10, |_| panic!("should not be called"));
        assert_eq!(n, 0);
    }

    #[test]
    fn producer_consumer_can_be_recreated() {
        let buf = EventBuf::<u32, 4>::new();
        {
            let p = buf.producer();
            p.push(1).unwrap();
        }
        // producer dropped — can create a new one
        let p = buf.producer();
        p.push(2).unwrap();

        {
            let c = buf.consumer();
            assert_eq!(c.pop(), Some(1));
        }
        // consumer dropped — can create a new one
        let c = buf.consumer();
        assert_eq!(c.pop(), Some(2));
        assert_eq!(c.pop(), None);
    }

    #[test]
    #[should_panic(expected = "only one Producer")]
    fn double_producer_panics() {
        let buf = EventBuf::<u32, 4>::new();
        let _p1 = buf.producer();
        let _p2 = buf.producer();
    }

    #[test]
    #[should_panic(expected = "only one Consumer")]
    fn double_consumer_panics() {
        let buf = EventBuf::<u32, 4>::new();
        let _c1 = buf.consumer();
        let _c2 = buf.consumer();
    }

    #[test]
    fn wraps_around_correctly() {
        let buf = EventBuf::<u32, 3>::new();
        let p = buf.producer();
        let c = buf.consumer();

        // fill, drain, fill again — exercises the wrap
        for round in 0u32..4 {
            let base = round * 3;
            for i in 0..3 {
                assert!(p.push(base + i).is_ok());
            }
            assert_eq!(p.push(99), Err(99)); // full
            for i in 0..3 {
                assert_eq!(c.pop(), Some(base + i));
            }
            assert_eq!(c.pop(), None); // empty
        }
    }

    #[test]
    fn default_is_new() {
        let buf: EventBuf<u8, 4> = EventBuf::default();
        assert!(buf.is_empty());
    }

    #[test]
    fn len_and_full_track_state() {
        let buf = EventBuf::<u32, 3>::new();
        let p = buf.producer();
        let c = buf.consumer();

        assert_eq!(buf.len(), 0);
        assert!(buf.is_empty());

        p.push(1).unwrap();
        assert_eq!(buf.len(), 1);

        p.push(2).unwrap();
        p.push(3).unwrap();
        assert_eq!(buf.len(), 3);
        assert!(buf.is_full());

        c.pop();
        assert_eq!(buf.len(), 2);
        assert!(!buf.is_full());
    }

    #[test]
    fn len_stays_within_capacity_while_consumer_drains() {
        let buf = EventBuf::<u32, 8>::new();
        let done = AtomicBool::new(false);
        let pushes = crate::test_support::iterations(200_000);

        std::thread::scope(|scope| {
            scope.spawn(|| {
                let p = buf.producer();
                for i in 0..pushes {
                    let _ = p.push(i);
                }
                done.store(true, Ordering::Release);
            });

            scope.spawn(|| {
                let c = buf.consumer();
                while !done.load(Ordering::Acquire) {
                    c.pop();
                }
            });

            // `len` races both handles; it may be stale, but it must never
            // report more than the buffer can hold.
            while !done.load(Ordering::Acquire) {
                let observed = buf.len();
                assert!(
                    observed <= buf.capacity(),
                    "len() reported {observed} for a capacity-{} buffer",
                    buf.capacity()
                );
            }
        });
    }

    #[test]
    fn concurrent_spsc_preserves_fifo_and_loses_nothing() {
        let buf = EventBuf::<u32, 4>::new();
        let total = crate::test_support::iterations(50_000);

        let received = std::thread::scope(|scope| {
            scope.spawn(|| {
                let p = buf.producer();
                // Backpressure means push can fail; retry so the stream is
                // complete and any gap in the consumer's view is a real bug.
                for i in 0..total {
                    let mut val = i;
                    while let Err(rejected) = p.push(val) {
                        val = rejected;
                        std::thread::yield_now();
                    }
                }
            });

            let consumer = scope.spawn(|| {
                let c = buf.consumer();
                let mut seen = 0u32;
                while seen < total {
                    match c.pop() {
                        // Strict FIFO: the nth item popped must be n.
                        Some(val) => {
                            assert_eq!(val, seen, "out-of-order pop at index {seen}");
                            seen += 1;
                        }
                        None => std::thread::yield_now(),
                    }
                }
                seen
            });

            consumer.join().unwrap()
        });

        assert_eq!(received, total);
        assert_eq!(buf.len(), 0);
    }

    #[test]
    fn handles_are_send() {
        fn assert_send<T: Send>() {}
        assert_send::<super::Producer<'_, u32, 4>>();
        assert_send::<super::Consumer<'_, u32, 4>>();
    }

    #[test]
    fn try_producer_and_try_consumer() {
        let buf = EventBuf::<u32, 4>::new();
        let p = buf.try_producer().expect("first producer");
        assert!(buf.try_producer().is_none());
        let c = buf.try_consumer().expect("first consumer");
        assert!(buf.try_consumer().is_none());
        p.push(1).unwrap();
        assert_eq!(c.pop(), Some(1));
        drop(p);
        drop(c);
        assert!(buf.try_producer().is_some());
        assert!(buf.try_consumer().is_some());
    }

    #[test]
    fn peek_copies_without_advancing() {
        let buf = EventBuf::<u32, 4>::new();
        let p = buf.producer();
        let c = buf.consumer();

        assert_eq!(c.peek(), None);
        p.push(10).unwrap();
        p.push(20).unwrap();
        assert_eq!(c.peek(), Some(10));
        assert_eq!(c.peek(), Some(10));
        assert_eq!(buf.len(), 2);
        assert_eq!(c.pop(), Some(10));
        assert_eq!(c.peek(), Some(20));
        assert_eq!(c.pop(), Some(20));
        assert_eq!(c.peek(), None);
    }

    // Loom's `new` is deliberately non-const, so a `static` init only exists
    // on the host path.
    #[cfg(not(loom))]
    #[test]
    fn const_new_works_in_const_context() {
        static BUF: EventBuf<u32, 4> = EventBuf::new();
        assert!(BUF.is_empty());
        assert_eq!(BUF.capacity(), 4);
    }

    // The point of the const `new` is not that a `static` compiles -- it is
    // that handles borrowed from one are `'static` and `Send`, which is what
    // lets the producer move into an ISR while the consumer stays in a task
    // loop. A test that only builds the `static` would still pass if the
    // lifetime were tied to a local, so pin the signature explicitly.
    #[cfg(not(loom))]
    #[test]
    fn static_buf_yields_static_sendable_handles() {
        static BUF: EventBuf<u32, 4> = EventBuf::new();

        fn producer_for_isr() -> super::Producer<'static, u32, 4> {
            BUF.producer()
        }
        fn consumer_for_task() -> super::Consumer<'static, u32, 4> {
            BUF.consumer()
        }
        fn assert_send<T: Send>(_: &T) {}

        let p = producer_for_isr();
        let c = consumer_for_task();
        assert_send(&p);
        assert_send(&c);

        p.push(7).unwrap();
        assert_eq!(c.pop(), Some(7));
    }
}