use std::marker::PhantomData;
use std::sync::Arc;
use std::sync::atomic::AtomicUsize;
use crossbeam_utils::CachePadded;
use crate::mem::Allocator;
use crate::spmc::queue::{Queue, Slot};
pub(super) type ProducerState = CachePadded<ProducerStateInner>;
pub(super) struct ProducerStateInner {
pub(super) write_cursor: AtomicUsize,
}
pub struct Producer<T, const CAPACITY: usize, A>
where
T: bytemuck::AnyBitPattern,
A: Allocator,
{
inner: Arc<Queue<T, CAPACITY, A>>,
_not_sync: PhantomData<*const ()>,
}
impl<T, const CAPACITY: usize, A> std::fmt::Debug for Producer<T, CAPACITY, A>
where
T: bytemuck::AnyBitPattern,
A: Allocator,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Producer")
.field("capacity", &CAPACITY)
.finish_non_exhaustive()
}
}
unsafe impl<T, const CAPACITY: usize, A> Send for Producer<T, CAPACITY, A>
where
A: Allocator,
T: bytemuck::AnyBitPattern + Send,
A::Allocation<Slot<T>>: Send,
{
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mem::test_util::NeverAllocator;
static_assertions::assert_impl_all!(Producer<u32, 1, NeverAllocator>: Send);
static_assertions::assert_not_impl_any!(Producer<u32, 1, NeverAllocator>: Sync, Clone, Copy);
}
impl<T, const CAPACITY: usize, A> Producer<T, CAPACITY, A>
where
T: bytemuck::AnyBitPattern,
A: Allocator,
{
pub(super) fn new(queue: Arc<Queue<T, CAPACITY, A>>) -> Self {
Self {
inner: queue,
_not_sync: PhantomData,
}
}
#[inline]
pub fn publish(&self, value: T) {
self.inner.publish(value)
}
}