Skip to main content

lender/fallible_adapters/
cloned.rs

1use fallible_iterator::{DoubleEndedFallibleIterator, FallibleIterator};
2
3use crate::{Cloned, DoubleEndedFallibleLender, FallibleLender, FallibleLending};
4
5impl<T, L> FallibleIterator for Cloned<L>
6where
7    L: FallibleLender,
8    T: Clone,
9    L: for<'all> FallibleLending<'all, Lend = &'all T>,
10{
11    type Item = T;
12    type Error = L::Error;
13
14    #[inline]
15    fn next(&mut self) -> Result<Option<Self::Item>, Self::Error> {
16        self.lender.next().map(Option::<&T>::cloned)
17    }
18
19    #[inline(always)]
20    fn size_hint(&self) -> (usize, Option<usize>) {
21        self.lender.size_hint()
22    }
23
24    #[inline]
25    fn fold<B, F>(self, init: B, mut f: F) -> Result<B, Self::Error>
26    where
27        Self: Sized,
28        F: FnMut(B, Self::Item) -> Result<B, Self::Error>,
29    {
30        self.lender.fold(init, |acc, x| f(acc, x.clone()))
31    }
32
33    #[inline(always)]
34    fn count(self) -> Result<usize, Self::Error>
35    where
36        Self: Sized,
37    {
38        self.lender.count()
39    }
40
41    #[inline]
42    fn nth(&mut self, n: usize) -> Result<Option<Self::Item>, Self::Error> {
43        self.lender.nth(n).map(Option::<&T>::cloned)
44    }
45}
46
47impl<T, L> DoubleEndedFallibleIterator for Cloned<L>
48where
49    L: DoubleEndedFallibleLender,
50    T: Clone,
51    L: for<'all> FallibleLending<'all, Lend = &'all T>,
52{
53    #[inline]
54    fn next_back(&mut self) -> Result<Option<Self::Item>, Self::Error> {
55        self.lender.next_back().map(Option::<&T>::cloned)
56    }
57
58    #[inline]
59    fn rfold<B, F>(self, init: B, mut f: F) -> Result<B, Self::Error>
60    where
61        Self: Sized,
62        F: FnMut(B, Self::Item) -> Result<B, Self::Error>,
63    {
64        self.lender.rfold(init, |acc, x| f(acc, x.clone()))
65    }
66}