Skip to main content

nblf_queue/array/
queue.rs

1#[cfg(feature = "pool")]
2pub use pooled_static::*;
3
4use crate::{
5    MPMCQueue,
6    array::buffer::ArrayBuf,
7    core::{
8        AsPackedValue,
9        queue::QueueCore,
10        slots::{Auto, SlotType},
11    },
12};
13
14/// A `MPMCQueue` with capacity `N`.
15///
16/// This queue only accepts items that implememt `PtrLike`.
17///
18/// If you need to store larger types, consider using `PooledStaticQueue` instead.
19pub struct StaticQueue<T, const N: usize, S = Auto>
20where
21    T: AsPackedValue,
22    S: SlotType<T>,
23{
24    inner: QueueCore<ArrayBuf<N, S::Slot>>,
25}
26
27impl<T, const N: usize> StaticQueue<T, N, Auto>
28where
29    T: AsPackedValue,
30{
31    /// Constructs a new `StaticQueue` with slot type `Auto`.
32    /// `T` must fit into the chosen slot type
33    pub fn new() -> Self {
34        Self::with_slot::<Auto>()
35    }
36
37    /// Constructs a new `StaticQueue` with slot type `S`.
38    /// `T` must fit into the slot type `S`
39    pub fn with_slot<S>() -> StaticQueue<T, N, S>
40    where
41        S: SlotType<T>,
42    {
43        StaticQueue {
44            inner: QueueCore::new_in(ArrayBuf::new()),
45        }
46    }
47}
48
49impl<T, const N: usize, S> MPMCQueue for StaticQueue<T, N, S>
50where
51    T: AsPackedValue,
52    S: SlotType<T>,
53{
54    type Item = T;
55
56    fn push(&self, item: Self::Item) -> Result<(), Self::Item> {
57        self.inner.push(item)
58    }
59
60    fn pop(&self) -> Option<Self::Item> {
61        self.inner.pop()
62    }
63
64    fn len(&self) -> usize {
65        self.inner.len()
66    }
67
68    fn capacity(&self) -> usize {
69        self.inner.capacity()
70    }
71}
72
73impl<T, const N: usize> Default for StaticQueue<T, N, Auto>
74where
75    T: AsPackedValue,
76{
77    fn default() -> Self {
78        Self::new()
79    }
80}
81
82#[cfg(feature = "pool")]
83mod pooled_static {
84    use super::*;
85    use crate::pool::{DataStorage, IndexStorage, ItemHandle, Pooled};
86
87    /// A pooled `MPMCQueue` with capacity `N`.
88    ///
89    /// Unlike `StaticQueue`, this queue may store any item at the cost of higher runtime and higher memory.
90    ///
91    /// Only available on feature `pool`
92    #[allow(private_bounds)]
93    pub struct PooledStaticQueue<T, const N: usize, S = Auto>
94    where
95        S: SlotType<ItemHandle<T>>,
96    {
97        #[allow(clippy::type_complexity)]
98        inner: Pooled<
99            T,
100            StaticQueue<ItemHandle<T>, N, S>,
101            ArrayBuf<N, DataStorage<T>>,
102            StaticQueue<IndexStorage, N>,
103        >,
104    }
105
106    #[allow(private_bounds)]
107    impl<T, const N: usize> PooledStaticQueue<T, N, Auto> {
108        /// Constructs a new `PooledStaticQueue` with slot type `Auto`
109        pub fn new() -> Self {
110            Self::with_slot::<Auto>()
111        }
112
113        /// Constructs a new `PooledStaticQueue` with slot type `S`
114        pub fn with_slot<S>() -> PooledStaticQueue<T, N, S>
115        where
116            S: SlotType<ItemHandle<T>>,
117        {
118            PooledStaticQueue {
119                inner: Pooled::new_from(
120                    StaticQueue::with_slot(),
121                    ArrayBuf::new(),
122                    StaticQueue::with_slot(),
123                ),
124            }
125        }
126    }
127
128    impl<T, const N: usize, S> MPMCQueue for PooledStaticQueue<T, N, S>
129    where
130        S: SlotType<ItemHandle<T>>,
131    {
132        type Item = T;
133
134        fn push(&self, item: Self::Item) -> Result<(), Self::Item> {
135            self.inner.push(item)
136        }
137
138        fn pop(&self) -> Option<Self::Item> {
139            self.inner.pop()
140        }
141
142        fn len(&self) -> usize {
143            self.inner.len()
144        }
145
146        fn capacity(&self) -> usize {
147            self.inner.capacity()
148        }
149    }
150
151    impl<T, const N: usize> Default for PooledStaticQueue<T, N, Auto> {
152        fn default() -> Self {
153            Self::new()
154        }
155    }
156}