rune_alloc/iter/
try_cloned.rs

1use crate::clone::TryClone;
2use crate::error::Error;
3
4/// An iterator that clones the elements of an underlying iterator.
5///
6/// This `struct` is created by the [`try_cloned`] method on [`IteratorExt`].
7/// See its documentation for more.
8///
9/// [`try_cloned`]: crate::iter::IteratorExt::try_cloned
10/// [`IteratorExt`]: crate::iter::IteratorExt
11pub struct TryCloned<I> {
12    it: I,
13}
14
15impl<I> TryCloned<I> {
16    pub(in crate::iter) fn new(it: I) -> Self {
17        Self { it }
18    }
19}
20
21impl<'a, I, T: 'a> Iterator for TryCloned<I>
22where
23    I: Iterator<Item = &'a T>,
24    T: TryClone,
25{
26    type Item = Result<T, Error>;
27
28    #[inline]
29    fn next(&mut self) -> Option<Self::Item> {
30        Some(self.it.next()?.try_clone())
31    }
32
33    fn size_hint(&self) -> (usize, Option<usize>) {
34        self.it.size_hint()
35    }
36}