#![no_std]
extern crate alloc;
#[cfg(not(loom))]
use core::sync::atomic::{AtomicU64, Ordering};
#[cfg(loom)]
use loom::sync::atomic::{AtomicU64, Ordering};
use alloc::vec::Vec;
use core::cell::UnsafeCell;
use core::fmt;
use core::mem::MaybeUninit;
pub mod oracle;
#[derive(Clone)]
pub struct SampleRing<T, const N: usize> {
head: usize,
len: usize,
slots: [Option<T>; N],
}
impl<T, const N: usize> SampleRing<T, N> {
#[must_use]
pub fn new() -> Self {
Self {
head: 0,
len: 0,
slots: core::array::from_fn(|_| None),
}
}
pub fn push(&mut self, value: T) -> Option<T> {
if N == 0 {
return Some(value);
}
if self.len == N {
let evicted = match self.slots.get_mut(self.head) {
Some(slot) => slot.replace(value),
None => return Some(value),
};
self.head = (self.head + 1) % N;
return evicted;
}
let slot = (self.head + self.len) % N;
match self.slots.get_mut(slot) {
Some(cell) => {
*cell = Some(value);
self.len += 1;
None
}
None => Some(value),
}
}
#[must_use]
pub fn iter(&self) -> Iter<'_, T, N> {
Iter { ring: self, pos: 0 }
}
#[must_use]
pub const fn len(&self) -> usize {
self.len
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.len == 0
}
#[must_use]
pub fn last(&self) -> Option<&T> {
if self.len == 0 {
return None;
}
let slot = (self.head + self.len - 1) % N;
self.slots.get(slot).and_then(Option::as_ref)
}
}
impl<T, const N: usize> Default for SampleRing<T, N> {
fn default() -> Self {
Self::new()
}
}
impl<T: fmt::Debug, const N: usize> fmt::Debug for SampleRing<T, N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}
impl<'a, T, const N: usize> IntoIterator for &'a SampleRing<T, N> {
type Item = &'a T;
type IntoIter = Iter<'a, T, N>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
#[derive(Clone)]
pub struct Iter<'a, T, const N: usize> {
ring: &'a SampleRing<T, N>,
pos: usize,
}
impl<'a, T, const N: usize> Iterator for Iter<'a, T, N> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
if self.pos >= self.ring.len {
return None;
}
let slot = (self.ring.head + self.pos) % N;
self.pos += 1;
self.ring.slots.get(slot).and_then(Option::as_ref)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.ring.len - self.pos;
(remaining, Some(remaining))
}
}
impl<T, const N: usize> ExactSizeIterator for Iter<'_, T, N> {}
pub struct SpscRing<T> {
buf: UnsafeCell<Vec<MaybeUninit<T>>>,
capacity: u64,
head: AtomicU64,
tail: AtomicU64,
}
#[allow(unsafe_code)] unsafe impl<T: Send> Send for SpscRing<T> {}
#[allow(unsafe_code)] unsafe impl<T: Send> Sync for SpscRing<T> {}
#[allow(unsafe_code)]
impl<T> SpscRing<T> {
#[must_use]
pub fn new(capacity: usize) -> Option<Self> {
if capacity == 0 {
return None;
}
let buf: Vec<MaybeUninit<T>> = (0..capacity).map(|_| MaybeUninit::uninit()).collect();
Some(Self {
capacity: capacity as u64, buf: UnsafeCell::new(buf),
head: AtomicU64::new(0),
tail: AtomicU64::new(0),
})
}
pub fn push(&self, value: T) -> Result<(), T> {
let tail = self.tail.load(Ordering::Relaxed); let head = self.head.load(Ordering::Acquire);
if tail.wrapping_sub(head) >= self.capacity {
return Err(value);
}
#[allow(clippy::cast_possible_truncation)] let slot = (tail % self.capacity) as usize;
unsafe {
let buf = self.buf.get();
(*buf).as_mut_ptr().add(slot).write(MaybeUninit::new(value));
}
self.tail.store(tail.wrapping_add(1), Ordering::Release);
Ok(())
}
pub fn pop(&self) -> Option<T> {
let head = self.head.load(Ordering::Relaxed); let tail = self.tail.load(Ordering::Acquire);
if head == tail {
return None;
}
#[allow(clippy::cast_possible_truncation)] let slot = (head % self.capacity) as usize;
let value = unsafe {
let buf = self.buf.get();
(*(*buf).as_ptr().add(slot)).assume_init_read()
};
self.head.store(head.wrapping_add(1), Ordering::Release);
Some(value)
}
#[must_use]
#[allow(clippy::cast_possible_truncation)] pub fn len(&self) -> usize {
let head = self.head.load(Ordering::Acquire);
let tail = self.tail.load(Ordering::Acquire);
tail.wrapping_sub(head) as usize
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl<T> fmt::Debug for SpscRing<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SpscRing")
.field("len", &self.len())
.finish()
}
}
impl<T> Drop for SpscRing<T> {
fn drop(&mut self) {
while self.pop().is_some() {}
}
}