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