use std::marker::PhantomData;
use heapless::spsc::{Consumer, Producer, Queue};
use crate::bench::harness::spsc::{SpscBench, SpscCons, SpscProd};
pub struct HeaplessSpsc<T, const C: usize, const N: usize>(PhantomData<fn() -> T>);
impl<T: Send + 'static, const C: usize, const N: usize> SpscBench<T, C> for HeaplessSpsc<T, C, N> {
type Prod = Producer<'static, T>;
type Cons = Consumer<'static, T>;
const NAME: &'static str = "heapless";
const WARNINGS: &'static [&'static str] =
&["heapless::Queue<T, N> holds N-1 items; adapter's raw N must equal logical C+1"];
fn new() -> (Self::Prod, Self::Cons) {
const {
assert!(
N == C + 1,
"HeaplessSpsc: raw slot count N must equal logical capacity C + 1"
);
}
let q: &'static mut Queue<T, N> = Box::leak(Box::new(Queue::new()));
q.split()
}
}
impl<T: Send> SpscProd<T> for Producer<'static, T> {
#[inline]
fn try_push(&mut self, v: T) -> Result<(), T> {
self.enqueue(v)
}
}
impl<T: Send> SpscCons<T> for Consumer<'static, T> {
#[inline]
fn try_pop(&mut self) -> Option<T> {
self.dequeue()
}
}