module_util/evaluator/
imports.rs1use core::iter::FusedIterator;
4use core::slice;
5
6use alloc::vec::{self, Vec};
7
8#[derive(Debug, Default, Clone)]
18#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
19pub struct Imports<T>(Vec<T>);
20
21impl<T> From<Vec<T>> for Imports<T> {
22 fn from(x: Vec<T>) -> Self {
23 Self(x)
24 }
25}
26
27impl<T> From<Imports<T>> for Vec<T> {
28 fn from(x: Imports<T>) -> Self {
29 x.0
30 }
31}
32
33impl<T> FromIterator<T> for Imports<T> {
34 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
35 Self(Vec::from_iter(iter))
36 }
37}
38
39impl<T> Imports<T> {
40 #[must_use]
42 pub const fn empty() -> Self {
43 Self(Vec::new())
44 }
45
46 #[must_use]
48 pub fn len(&self) -> usize {
49 self.0.len()
50 }
51
52 #[must_use]
54 pub fn is_empty(&self) -> bool {
55 self.0.is_empty()
56 }
57
58 pub fn push(&mut self, import: T) {
79 self.0.push(import);
80 }
81
82 #[must_use]
103 pub fn iter(&self) -> Iter<'_, T> {
104 Iter(self.0.iter())
105 }
106}
107
108impl<'a, T> IntoIterator for &'a Imports<T> {
109 type Item = &'a T;
110 type IntoIter = Iter<'a, T>;
111
112 fn into_iter(self) -> Self::IntoIter {
113 self.iter()
114 }
115}
116
117impl<T> IntoIterator for Imports<T> {
118 type Item = T;
119 type IntoIter = IntoIter<T>;
120
121 fn into_iter(self) -> Self::IntoIter {
122 IntoIter(self.0.into_iter())
123 }
124}
125
126#[derive(Debug, Clone)]
132pub struct Iter<'a, T>(slice::Iter<'a, T>);
133
134impl<'a, T> Iterator for Iter<'a, T> {
135 type Item = &'a T;
136
137 fn next(&mut self) -> Option<Self::Item> {
138 self.0.next()
139 }
140
141 fn size_hint(&self) -> (usize, Option<usize>) {
142 self.0.size_hint()
143 }
144}
145
146impl<T> DoubleEndedIterator for Iter<'_, T> {
147 fn next_back(&mut self) -> Option<Self::Item> {
148 self.0.next_back()
149 }
150}
151
152impl<T> ExactSizeIterator for Iter<'_, T> {
153 fn len(&self) -> usize {
154 self.0.len()
155 }
156}
157
158impl<T> FusedIterator for Iter<'_, T> {}
159
160#[derive(Debug, Clone)]
164pub struct IntoIter<T>(vec::IntoIter<T>);
165
166impl<T> Iterator for IntoIter<T> {
167 type Item = T;
168
169 fn next(&mut self) -> Option<Self::Item> {
170 self.0.next()
171 }
172
173 fn size_hint(&self) -> (usize, Option<usize>) {
174 self.0.size_hint()
175 }
176}
177
178impl<T> DoubleEndedIterator for IntoIter<T> {
179 fn next_back(&mut self) -> Option<Self::Item> {
180 self.0.next()
181 }
182}
183
184impl<T> ExactSizeIterator for IntoIter<T> {
185 fn len(&self) -> usize {
186 self.0.len()
187 }
188}
189
190impl<T> FusedIterator for IntoIter<T> {}