arch_pkg_db/text/single/
iter.rs

1use super::TextCollection;
2use crate::Text;
3use core::{iter::FusedIterator, slice};
4use std::vec;
5
6/// [Iterator] over immutable references to all items inside a [`TextCollection`].
7#[derive(Debug, Clone)]
8pub struct TextIter<'a> {
9    internal: slice::Iter<'a, Text>,
10}
11
12impl<'a> Iterator for TextIter<'a> {
13    type Item = &'a Text;
14
15    fn next(&mut self) -> Option<Self::Item> {
16        self.internal.next()
17    }
18
19    fn size_hint(&self) -> (usize, Option<usize>) {
20        self.internal.size_hint()
21    }
22
23    fn count(self) -> usize {
24        self.internal.count()
25    }
26}
27
28impl DoubleEndedIterator for TextIter<'_> {
29    fn next_back(&mut self) -> Option<Self::Item> {
30        self.internal.next_back()
31    }
32}
33
34impl ExactSizeIterator for TextIter<'_> {
35    fn len(&self) -> usize {
36        self.internal.len()
37    }
38}
39
40impl FusedIterator for TextIter<'_> {}
41
42impl TextCollection {
43    /// Iterate over immutable references to the items inside.
44    pub fn iter(&self) -> TextIter<'_> {
45        TextIter {
46            internal: self.internal.iter(),
47        }
48    }
49}
50
51impl<'a> IntoIterator for &'a TextCollection {
52    type Item = &'a Text;
53    type IntoIter = TextIter<'a>;
54    fn into_iter(self) -> Self::IntoIter {
55        self.iter()
56    }
57}
58
59/// [Iterator] over mutable references to all items inside a [`TextCollection`].
60#[derive(Debug)]
61pub struct TextIterMut<'a> {
62    internal: slice::IterMut<'a, Text>,
63}
64
65impl<'a> Iterator for TextIterMut<'a> {
66    type Item = &'a mut Text;
67
68    fn next(&mut self) -> Option<Self::Item> {
69        self.internal.next()
70    }
71
72    fn size_hint(&self) -> (usize, Option<usize>) {
73        self.internal.size_hint()
74    }
75
76    fn count(self) -> usize {
77        self.internal.count()
78    }
79}
80
81impl DoubleEndedIterator for TextIterMut<'_> {
82    fn next_back(&mut self) -> Option<Self::Item> {
83        self.internal.next_back()
84    }
85}
86
87impl ExactSizeIterator for TextIterMut<'_> {
88    fn len(&self) -> usize {
89        self.internal.len()
90    }
91}
92
93impl FusedIterator for TextIterMut<'_> {}
94
95impl TextCollection {
96    /// Iterate over mutable references to the items inside.
97    pub fn iter_mut(&mut self) -> TextIterMut<'_> {
98        TextIterMut {
99            internal: self.internal.iter_mut(),
100        }
101    }
102}
103
104impl<'a> IntoIterator for &'a mut TextCollection {
105    type Item = &'a mut Text;
106    type IntoIter = TextIterMut<'a>;
107    fn into_iter(self) -> Self::IntoIter {
108        self.iter_mut()
109    }
110}
111
112/// [Iterator] over owned items inside a [`TextCollection`].
113#[derive(Debug, Clone)]
114pub struct TextIntoIter {
115    internal: vec::IntoIter<Text>,
116}
117
118impl Iterator for TextIntoIter {
119    type Item = Text;
120
121    fn next(&mut self) -> Option<Self::Item> {
122        self.internal.next()
123    }
124
125    fn size_hint(&self) -> (usize, Option<usize>) {
126        self.internal.size_hint()
127    }
128
129    fn count(self) -> usize {
130        self.internal.count()
131    }
132}
133
134impl DoubleEndedIterator for TextIntoIter {
135    fn next_back(&mut self) -> Option<Self::Item> {
136        self.internal.next_back()
137    }
138}
139
140impl ExactSizeIterator for TextIntoIter {
141    fn len(&self) -> usize {
142        self.internal.len()
143    }
144}
145
146impl FusedIterator for TextIntoIter {}
147
148impl IntoIterator for TextCollection {
149    type Item = Text;
150    type IntoIter = TextIntoIter;
151    fn into_iter(self) -> Self::IntoIter {
152        TextIntoIter {
153            internal: self.internal.into_iter(),
154        }
155    }
156}