error2/
iterator_ext.rs

1use crate::{Attach, Location};
2
3pub trait IteratorExt: Iterator + Sized
4where
5    Self::Item: Attach,
6{
7    #[track_caller]
8    #[inline]
9    fn attach(self) -> AttachIter<Self> {
10        AttachIter {
11            inner: self,
12            location: Location::caller(),
13        }
14    }
15
16    #[inline]
17    fn attach_location(self, location: Location) -> AttachIter<Self> {
18        AttachIter {
19            inner: self,
20            location,
21        }
22    }
23}
24
25impl<T: Iterator> IteratorExt for T where T::Item: Attach {}
26
27#[derive(Debug, Clone, Copy)]
28#[must_use = "iterators are lazy and do nothing unless consumed"]
29pub struct AttachIter<I> {
30    inner: I,
31    location: Location,
32}
33
34impl<I> Iterator for AttachIter<I>
35where
36    I: Iterator,
37    I::Item: Attach,
38{
39    type Item = I::Item;
40
41    #[inline]
42    fn next(&mut self) -> Option<Self::Item> {
43        match self.inner.next() {
44            Some(item) => Some(item.attach_location(self.location)),
45            None => None,
46        }
47    }
48}