async_selector/selector/
iter.rs1use crate::{
4 list::{Cursor, List},
5 selector::{Borrowed, BorrowedMut, Removed},
6};
7
8pub struct IntoIter<T>(pub(super) List<T>);
10
11impl<T> Iterator for IntoIter<T> {
12 type Item = Removed<T>;
13
14 fn next(&mut self) -> Option<Self::Item> {
15 self.0.cursor_mut().remove_front().map(Removed)
16 }
17
18 fn size_hint(&self) -> (usize, Option<usize>) {
19 let len = self.0.len();
20 (len, Some(len))
21 }
22}
23
24impl<T> ExactSizeIterator for IntoIter<T> {
25 fn len(&self) -> usize {
26 self.0.len()
27 }
28}
29
30impl<T> DoubleEndedIterator for IntoIter<T> {
31 fn next_back(&mut self) -> Option<Self::Item> {
32 self.0.cursor_mut().remove_back().map(Removed)
33 }
34}
35
36pub struct Iter<'a, T>(pub(super) Cursor<T, &'a List<T>>);
38
39impl<'a, T> Iterator for Iter<'a, T> {
40 type Item = Borrowed<'a, T>;
41
42 fn next(&mut self) -> Option<Self::Item> {
43 self.0.pop_front().map(Borrowed)
44 }
45
46 fn size_hint(&self) -> (usize, Option<usize>) {
47 let len = self.len();
48 (len, Some(len))
49 }
50}
51
52impl<T> ExactSizeIterator for Iter<'_, T> {
53 fn len(&self) -> usize {
54 self.0.len()
55 }
56}
57
58impl<T> DoubleEndedIterator for Iter<'_, T> {
59 fn next_back(&mut self) -> Option<Self::Item> {
60 self.0.pop_back().map(Borrowed)
61 }
62}
63
64pub struct IterMut<'a, T>(pub(super) Cursor<T, &'a mut List<T>>);
66
67impl<'a, T> Iterator for IterMut<'a, T> {
68 type Item = BorrowedMut<'a, T>;
69
70 fn next(&mut self) -> Option<Self::Item> {
71 self.0.pop_front().map(BorrowedMut)
72 }
73
74 fn size_hint(&self) -> (usize, Option<usize>) {
75 let len = self.len();
76 (len, Some(len))
77 }
78}
79
80impl<T> ExactSizeIterator for IterMut<'_, T> {
81 fn len(&self) -> usize {
82 self.0.len()
83 }
84}
85
86impl<T> DoubleEndedIterator for IterMut<'_, T> {
87 fn next_back(&mut self) -> Option<Self::Item> {
88 self.0.pop_back().map(BorrowedMut)
89 }
90}
91
92pub struct ExtractIf<'a, T, F> {
94 pub(super) cursor: Cursor<T, &'a mut List<T>>,
95 pub(super) pred: F,
96}
97
98impl<'a, T, F> Iterator for ExtractIf<'a, T, F>
99where
100 F: for<'b> FnMut(BorrowedMut<'b, T>) -> bool,
101{
102 type Item = Removed<T>;
103
104 fn next(&mut self) -> Option<Self::Item> {
105 loop {
106 let next = self.cursor.peek_front()?;
107 if (self.pred)(BorrowedMut(next)) {
108 break self.cursor.remove_front().map(Removed);
109 } else {
110 self.cursor.pop_front();
111 }
112 }
113 }
114
115 fn size_hint(&self) -> (usize, Option<usize>) {
116 (0, None)
117 }
118}
119
120impl<'a, T, F> DoubleEndedIterator for ExtractIf<'a, T, F>
121where
122 F: for<'b> FnMut(BorrowedMut<'b, T>) -> bool,
123{
124 fn next_back(&mut self) -> Option<Self::Item> {
125 loop {
126 let back = self.cursor.peek_back()?;
127 if (self.pred)(BorrowedMut(back)) {
128 break self.cursor.remove_back().map(Removed);
129 } else {
130 self.cursor.pop_back();
131 }
132 }
133 }
134}