low_latency_data_structures/spsc/
consumer.rs1use std::marker::PhantomData;
2
3use crossbeam_utils::CachePadded;
4
5use crate::mem::Allocator;
6use crate::shim::cell::Cell;
7use crate::shim::sync::Arc;
8use crate::shim::sync::atomic::AtomicUsize;
9use crate::spsc::queue::Queue;
10
11pub(super) type ConsumerState = CachePadded<ConsumerStateInner>;
12
13#[derive(Default)]
14pub(super) struct ConsumerStateInner {
15 pub head: AtomicUsize,
16 pub cached_tail: Cell<usize>,
17}
18
19pub struct Consumer<T, const CAPACITY: usize, A>
26where
27 A: Allocator,
28{
29 inner: Arc<Queue<T, CAPACITY, A>>,
30 _not_sync: PhantomData<*const ()>,
31}
32
33impl<T, const CAPACITY: usize, A> std::fmt::Debug for Consumer<T, CAPACITY, A>
34where
35 A: Allocator,
36{
37 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 f.debug_struct("Consumer")
39 .field("capacity", &CAPACITY)
40 .finish_non_exhaustive()
41 }
42}
43
44unsafe impl<T, const CAPACITY: usize, A> Send for Consumer<T, CAPACITY, A>
47where
48 T: Send,
49 A: Allocator,
50 A::Allocation<T>: Send,
51{
52}
53
54impl<T, const CAPACITY: usize, A> Consumer<T, CAPACITY, A>
55where
56 A: Allocator,
57{
58 pub(super) fn new(queue: Arc<Queue<T, CAPACITY, A>>) -> Self {
59 Self {
60 inner: queue,
61 _not_sync: PhantomData,
62 }
63 }
64
65 #[inline]
84 #[must_use = "ignoring the popped item silently drops it"]
85 pub fn pop(&self) -> Option<T> {
86 self.inner.pop()
87 }
88}
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93 use crate::mem::test_util::NeverAllocator;
94
95 static_assertions::assert_not_impl_any!(Consumer<u32, 0, NeverAllocator>: Sync);
98}