Skip to main content

nblf_queue/owned/
queue.rs

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