use std::marker::PhantomData;
use crossbeam_utils::CachePadded;
use crate::mem::Allocator;
use crate::shim::cell::Cell;
use crate::shim::sync::Arc;
use crate::shim::sync::atomic::AtomicUsize;
use crate::spsc::queue::Queue;
pub(super) type ProducerState = CachePadded<ProducerStateInner>;
#[derive(Default)]
pub(super) struct ProducerStateInner {
pub tail: AtomicUsize,
pub cached_head: Cell<usize>,
}
pub struct Producer<T, const CAPACITY: usize, A>
where
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
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
T: Send,
A: Allocator,
A::Allocation<T>: Send,
{
}
impl<T, const CAPACITY: usize, A> Producer<T, CAPACITY, A>
where
A: Allocator,
{
pub(super) fn new(queue: Arc<Queue<T, CAPACITY, A>>) -> Self {
Self {
inner: queue,
_not_sync: PhantomData,
}
}
#[inline]
#[must_use = "if the queue is full, the returned item must be handled (e.g. retried) or it is silently dropped"]
pub fn push(&self, item: T) -> Option<T> {
self.inner.push(item)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mem::test_util::NeverAllocator;
static_assertions::assert_not_impl_any!(Producer<u32, 0, NeverAllocator>: Sync);
}