hcl/structure/
iter.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//! Iterators over HCL structures.

use super::{Attribute, Block, Body, Structure};
use std::iter::FusedIterator;
use std::slice;
use std::vec;

macro_rules! impl_find_map_iterator {
    ($ty:ident$(<$lt:lifetime>)?, $item:ty, $map:expr) => {
        impl$(<$lt>)* Iterator for $ty$(<$lt>)* {
            type Item = $item;

            fn next(&mut self) -> Option<Self::Item> {
                self.iter.find_map($map)
            }
        }

        impl$(<$lt>)* DoubleEndedIterator for $ty$(<$lt>)* {
            fn next_back(&mut self) -> Option<Self::Item> {
                loop {
                    match self.iter.next_back() {
                        Some(val) => {
                            if let Some(val) = $map(val) {
                                return Some(val);
                            }
                        }
                        None => return None,
                    };
                }
            }
        }

        impl$(<$lt>)* FusedIterator for $ty$(<$lt>)* {}
    };
}

macro_rules! impl_exact_size_iterator {
    ($ty:ident$(<$lt:lifetime>)?, $item:ty) => {
        impl$(<$lt>)* Iterator for $ty$(<$lt>)* {
            type Item = $item;

            fn next(&mut self) -> Option<Self::Item> {
                self.iter.next()
            }

            fn size_hint(&self) -> (usize, Option<usize>) {
                self.iter.size_hint()
            }
        }

        impl$(<$lt>)* DoubleEndedIterator for $ty$(<$lt>)* {
            fn next_back(&mut self) -> Option<Self::Item> {
                self.iter.next_back()
            }
        }

        impl$(<$lt>)* ExactSizeIterator for $ty$(<$lt>)* {
            fn len(&self) -> usize {
                self.iter.len()
            }
        }

        impl$(<$lt>)* FusedIterator for $ty$(<$lt>)* {}
    };
}

impl<T> Extend<T> for Body
where
    T: Into<Structure>,
{
    fn extend<I>(&mut self, iterable: I)
    where
        I: IntoIterator<Item = T>,
    {
        self.0.extend(iterable.into_iter().map(Into::into));
    }
}

impl<T> FromIterator<T> for Body
where
    T: Into<Structure>,
{
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = T>,
    {
        let iter = iter.into_iter();
        let lower = iter.size_hint().0;
        let mut body = Body(Vec::with_capacity(lower));
        body.extend(iter);
        body
    }
}

impl IntoIterator for Body {
    type Item = Structure;

    type IntoIter = IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter::new(self)
    }
}

impl<'a> IntoIterator for &'a Body {
    type Item = &'a Structure;

    type IntoIter = Iter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a> IntoIterator for &'a mut Body {
    type Item = &'a mut Structure;

    type IntoIter = IterMut<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

/// An iterator over the structures within a `Body`.
///
/// This `struct` is created by the [`iter`][Body::iter] method on [`Body`]. See its documentation
/// for more.
#[derive(Debug, Clone)]
pub struct Iter<'a> {
    iter: slice::Iter<'a, Structure>,
}

impl<'a> Iter<'a> {
    pub(super) fn new(body: &'a Body) -> Iter<'a> {
        Iter {
            iter: body.0.iter(),
        }
    }
}

impl_exact_size_iterator!(Iter<'a>, &'a Structure);

/// A mutable iterator over the structures within a `Body`.
///
/// This `struct` is created by the [`iter_mut`][Body::iter_mut] method on [`Body`]. See its
/// documentation for more.
#[derive(Debug)]
pub struct IterMut<'a> {
    iter: slice::IterMut<'a, Structure>,
}

impl<'a> IterMut<'a> {
    pub(super) fn new(body: &'a mut Body) -> IterMut<'a> {
        IterMut {
            iter: body.0.iter_mut(),
        }
    }
}

impl_exact_size_iterator!(IterMut<'a>, &'a mut Structure);

/// An owning iterator over the structures within a `Body`.
///
/// This `struct` is created by the [`into_iter`] method on [`Body`] (provided by the
/// [`IntoIterator`] trait). See its documentation for more.
///
/// [`into_iter`]: IntoIterator::into_iter
/// [`IntoIterator`]: core::iter::IntoIterator
#[derive(Debug, Clone)]
pub struct IntoIter {
    iter: vec::IntoIter<Structure>,
}

impl IntoIter {
    pub(super) fn new(body: Body) -> IntoIter {
        IntoIter {
            iter: body.0.into_iter(),
        }
    }
}

impl_exact_size_iterator!(IntoIter, Structure);

/// An iterator over the attributes within a `Body`.
///
/// This `struct` is created by the [`attributes`][Body::attributes] method on [`Body`]. See its
/// documentation for more.
#[derive(Debug, Clone)]
pub struct Attributes<'a> {
    iter: Iter<'a>,
}

impl<'a> Attributes<'a> {
    pub(super) fn new(body: &'a Body) -> Attributes<'a> {
        Attributes { iter: body.iter() }
    }
}

impl_find_map_iterator!(Attributes<'a>, &'a Attribute, Structure::as_attribute);

/// A mutable iterator over the attributes within a `Body`.
///
/// This `struct` is created by the [`attributes_mut`][Body::attributes_mut] method on [`Body`].
/// See its documentation for more.
#[derive(Debug)]
pub struct AttributesMut<'a> {
    iter: IterMut<'a>,
}

impl<'a> AttributesMut<'a> {
    pub(super) fn new(body: &'a mut Body) -> AttributesMut<'a> {
        AttributesMut {
            iter: body.iter_mut(),
        }
    }
}

impl_find_map_iterator!(
    AttributesMut<'a>,
    &'a mut Attribute,
    Structure::as_attribute_mut
);

/// An owning iterator over the attributes within a `Body`.
///
/// This `struct` is created by the [`into_attributes`][Body::into_attributes] method on [`Body`].
/// See its documentation for more.
#[derive(Debug, Clone)]
pub struct IntoAttributes {
    iter: IntoIter,
}

impl IntoAttributes {
    pub(super) fn new(body: Body) -> IntoAttributes {
        IntoAttributes {
            iter: body.into_iter(),
        }
    }
}

impl_find_map_iterator!(IntoAttributes, Attribute, Structure::into_attribute);

/// An iterator over the blocks within a `Body`.
///
/// This `struct` is created by the [`blocks`][Body::blocks] method on [`Body`]. See its
/// documentation for more.
#[derive(Debug, Clone)]
pub struct Blocks<'a> {
    iter: Iter<'a>,
}

impl<'a> Blocks<'a> {
    pub(super) fn new(body: &'a Body) -> Blocks<'a> {
        Blocks { iter: body.iter() }
    }
}

impl_find_map_iterator!(Blocks<'a>, &'a Block, Structure::as_block);

/// A mutable iterator over the blocks within a `Body`.
///
/// This `struct` is created by the [`blocks_mut`][Body::blocks_mut] method on [`Body`]. See its
/// documentation for more.
#[derive(Debug)]
pub struct BlocksMut<'a> {
    iter: IterMut<'a>,
}

impl<'a> BlocksMut<'a> {
    pub(super) fn new(body: &'a mut Body) -> BlocksMut<'a> {
        BlocksMut {
            iter: body.iter_mut(),
        }
    }
}

impl_find_map_iterator!(BlocksMut<'a>, &'a mut Block, Structure::as_block_mut);

/// An owning iterator over the blocks within a `Body`.
///
/// This `struct` is created by the [`into_blocks`][Body::into_blocks] method on [`Body`]. See its
/// documentation for more.
#[derive(Debug, Clone)]
pub struct IntoBlocks {
    iter: IntoIter,
}

impl IntoBlocks {
    pub(super) fn new(body: Body) -> IntoBlocks {
        IntoBlocks {
            iter: body.into_iter(),
        }
    }
}

impl_find_map_iterator!(IntoBlocks, Block, Structure::into_block);