orx_concurrent_recursive_iter/queue.rs
1use orx_concurrent_queue::{ConcurrentQueue, DefaultConPinnedVec};
2use orx_pinned_vec::ConcurrentPinnedVec;
3
4/// A queue of elements that will be returned by the [`ConcurrentRecursiveIter`].
5///
6/// Note that the concurrent recursive iterator is built on top of the [`ConcurrentQueue`].
7/// `Queue` is just a wrapper over the concurrent queue, exposing only two methods:
8///
9/// * `push` to add one task at a time,
10/// * `extend` to add multiple tasks with known length together with a single update.
11///
12/// Notice that `push` is very flexible and makes the pushed element available for iteration
13/// as fast as possible.
14///
15/// On the other hand, `extend` minimizes the overhead of concurrency.
16///
17/// [`ConcurrentRecursiveIter`]: crate::ConcurrentRecursiveIter
18/// [`ConcurrentQueue`]: orx_concurrent_queue::ConcurrentQueue
19pub struct Queue<'a, T, P = DefaultConPinnedVec<T>>
20where
21 T: Send,
22 P: ConcurrentPinnedVec<T>,
23{
24 queue: &'a ConcurrentQueue<T, P>,
25}
26
27impl<T, P> Queue<'_, T, P>
28where
29 T: Send,
30 P: ConcurrentPinnedVec<T>,
31{
32 /// Pushes the `element` to the iterator, making it available to all threads as fast as possible.
33 #[inline(always)]
34 pub fn push(&self, element: T) {
35 self.queue.push(element);
36 }
37
38 /// Pushes all `elements` to the iterator with a single update on the concurrent state.
39 ///
40 /// All of the elements will be available to all threads at the same time, once writing all of them
41 /// to the queue is completed.
42 #[inline(always)]
43 pub fn extend<I>(&self, elements: I)
44 where
45 I: IntoIterator<Item = T>,
46 I::IntoIter: ExactSizeIterator,
47 {
48 self.queue.extend(elements);
49 }
50}
51
52impl<'a, T, P> From<&'a ConcurrentQueue<T, P>> for Queue<'a, T, P>
53where
54 T: Send,
55 P: ConcurrentPinnedVec<T>,
56{
57 #[inline(always)]
58 fn from(queue: &'a ConcurrentQueue<T, P>) -> Self {
59 Self { queue }
60 }
61}