takoude 0.2.1

Lock-free SPSC ring buffer with per-slot sequence flags, tuned for low latency.
Documentation
use core::cell::UnsafeCell;
use core::mem::MaybeUninit;
use core::sync::atomic::AtomicU64;

/// One ring slot: publish flag and payload share a cache line, so a push
/// moves both to the consumer in a single line transfer.
///
/// `seq` is a producer-written publish flag. The producer stores
/// `seq = pos + 1` after writing the payload for position `pos`; the consumer
/// reads the slot when `seq == pos + 1`. The consumer never writes the slot
/// (it signals reuse via the ring's `progress` counter), so the hot line only
/// moves producer to consumer, never back. Flags from earlier laps differ by
/// a multiple of `N`, and the check is an equality, so `u64` wraparound is
/// harmless while `N` is a power of two (it divides 2^64 evenly).
///
/// A payload larger than 56 bytes spills onto further cache lines; the flag
/// still shares the first line with the payload head.
#[repr(C, align(64))]
pub(crate) struct Slot<T> {
    pub(crate) seq: AtomicU64,
    pub(crate) payload: UnsafeCell<MaybeUninit<T>>,
}

impl<T> Slot<T> {
    /// An empty slot whose flag cannot equal `pos + 1` for any position that
    /// maps to it until the producer writes it.
    pub(crate) fn empty(pos: u64) -> Self {
        Slot {
            seq: AtomicU64::new(pos),
            payload: UnsafeCell::new(MaybeUninit::uninit()),
        }
    }
}