orx_concurrent_iter/
into_concurrent_iter.rs

1use crate::concurrent_iter::ConcurrentIter;
2
3/// Trait to convert a source (collection or generator) into a concurrent iterator; i.e., [`ConcurrentIter`],
4/// using its [`into_con_iter`] method.
5///
6/// It can be considered as the *concurrent counterpart* of the [`IntoIterator`] trait.
7///
8/// [`into_con_iter`]: crate::IntoConcurrentIter::into_con_iter
9///
10/// # Examples
11///
12/// ```
13/// use orx_concurrent_iter::*;
14///
15/// let vec = vec![1, 2];
16/// let con_iter = vec.into_con_iter();
17/// assert_eq!(con_iter.next(), Some(1));
18/// assert_eq!(con_iter.next(), Some(2));
19/// assert_eq!(con_iter.next(), None);
20///
21/// let range = 11..13;
22/// let con_iter = range.into_con_iter();
23/// assert_eq!(con_iter.next(), Some(11));
24/// assert_eq!(con_iter.next(), Some(12));
25/// assert_eq!(con_iter.next(), None);
26/// ```
27pub trait IntoConcurrentIter {
28    /// Type of the element that the concurrent iterator yields.
29    type Item;
30
31    /// Type of the concurrent iterator that this type can be converted into.
32    type IntoIter: ConcurrentIter<Item = Self::Item>;
33
34    /// Trait to convert a source (collection or generator) into a concurrent iterator; i.e., [`ConcurrentIter`],
35    /// using its [`into_con_iter`] method.
36    ///
37    /// It can be considered as the *concurrent counterpart* of the [`IntoIterator`] trait.
38    ///
39    /// [`into_con_iter`]: crate::IntoConcurrentIter::into_con_iter
40    ///
41    /// # Examples
42    ///
43    /// ```
44    /// use orx_concurrent_iter::*;
45    ///
46    /// let vec = vec![1, 2];
47    /// let con_iter = vec.into_con_iter();
48    /// assert_eq!(con_iter.next(), Some(1));
49    /// assert_eq!(con_iter.next(), Some(2));
50    /// assert_eq!(con_iter.next(), None);
51    ///
52    /// let range = 11..13;
53    /// let con_iter = range.into_con_iter();
54    /// assert_eq!(con_iter.next(), Some(11));
55    /// assert_eq!(con_iter.next(), Some(12));
56    /// assert_eq!(con_iter.next(), None);
57    /// ```
58    fn into_con_iter(self) -> Self::IntoIter;
59}
60
61impl<I: ConcurrentIter> IntoConcurrentIter for I {
62    type Item = I::Item;
63
64    type IntoIter = I;
65
66    fn into_con_iter(self) -> Self::IntoIter {
67        self
68    }
69}