orx_concurrent_iter/concurrent_collection.rs
1use crate::{concurrent_iterable::ConcurrentIterable, into_concurrent_iter::IntoConcurrentIter};
2
3/// A type implementing [`ConcurrentCollection`] is a collection owning the elements such that
4///
5/// * if the elements are of type `T`,
6/// * then, non-consuming [`con_iter`] method can be called **multiple times** to create concurrent
7/// iterators; i.e., [`ConcurrentIter`], yielding references to the elements `&T`.
8///
9/// This trait can be considered as the *concurrent counterpart* of the [`Collection`] trait.
10///
11/// [`con_iter`]: crate::ConcurrentCollection::con_iter
12/// [`Collection`]: orx_iterable::Collection
13/// [`ConcurrentIter`]: crate::ConcurrentIter
14///
15/// # Examples
16///
17/// ```
18/// use orx_concurrent_iter::*;
19///
20/// let data = vec![1, 2];
21///
22/// let con_iter = data.con_iter();
23/// assert_eq!(con_iter.next(), Some(&1));
24/// assert_eq!(con_iter.next(), Some(&2));
25/// assert_eq!(con_iter.next(), None);
26/// ```
27pub trait ConcurrentCollection {
28 /// Type of the element that the concurrent iterator yields.
29 type Item;
30
31 /// Type of the ConcurrentIterable that reference of this type implements.
32 type Iterable<'i>: ConcurrentIterable<Item = &'i Self::Item>
33 where
34 Self: 'i;
35
36 /// Returns the ConcurrentIterable that a reference of this type can create.
37 fn as_concurrent_iterable(&self) -> Self::Iterable<'_>;
38
39 /// A type implementing [`ConcurrentCollection`] is a collection owning the elements such that
40 ///
41 /// * if the elements are of type `T`,
42 /// * then, non-consuming [`con_iter`] method can be called **multiple times** to create concurrent
43 /// iterators; i.e., [`ConcurrentIter`], yielding references to the elements `&T`.
44 ///
45 /// This trait can be considered as the *concurrent counterpart* of the [`Collection`] trait.
46 ///
47 /// [`con_iter`]: crate::ConcurrentCollection::con_iter
48 /// [`Collection`]: orx_iterable::Collection
49 /// [`ConcurrentIter`]: crate::ConcurrentIter
50 ///
51 /// # Examples
52 ///
53 /// ```
54 /// use orx_concurrent_iter::*;
55 ///
56 /// let data = vec![1, 2];
57 ///
58 /// let con_iter = data.con_iter();
59 /// assert_eq!(con_iter.next(), Some(&1));
60 /// assert_eq!(con_iter.next(), Some(&2));
61 /// assert_eq!(con_iter.next(), None);
62 /// ```
63 fn con_iter(&self) -> <Self::Iterable<'_> as ConcurrentIterable>::Iter {
64 self.as_concurrent_iterable().con_iter()
65 }
66}
67
68impl<X> ConcurrentCollection for X
69where
70 X: IntoConcurrentIter,
71 for<'a> &'a X: IntoConcurrentIter<Item = &'a <X as IntoConcurrentIter>::Item>,
72{
73 type Item = <X as IntoConcurrentIter>::Item;
74
75 type Iterable<'i>
76 = &'i X
77 where
78 Self: 'i;
79
80 fn as_concurrent_iterable(&self) -> Self::Iterable<'_> {
81 self
82 }
83}