Skip to main content

lender/fallible_adapters/
rev.rs

1use core::num::NonZeroUsize;
2
3use crate::{
4    DoubleEndedFallibleLender, ExactSizeFallibleLender, FallibleLend, FallibleLender,
5    FallibleLending, FusedFallibleLender, Rev, try_trait_v2::Try,
6};
7
8impl<L: FallibleLender> Rev<L> {
9    #[inline(always)]
10    pub(crate) fn new_fallible(lender: L) -> Rev<L> {
11        let _ = L::__check_covariance(crate::CovariantProof::new());
12        Rev { lender }
13    }
14}
15
16impl<'lend, L> FallibleLending<'lend> for Rev<L>
17where
18    L: FallibleLender,
19{
20    type Lend = FallibleLend<'lend, L>;
21}
22
23impl<L> FallibleLender for Rev<L>
24where
25    L: DoubleEndedFallibleLender,
26{
27    type Error = L::Error;
28    // SAFETY: the lend is that of L
29    crate::unsafe_assume_covariance_fallible!();
30
31    #[inline(always)]
32    fn next(&mut self) -> Result<Option<FallibleLend<'_, Self>>, Self::Error> {
33        self.lender.next_back()
34    }
35
36    #[inline(always)]
37    fn size_hint(&self) -> (usize, Option<usize>) {
38        self.lender.size_hint()
39    }
40
41    #[inline(always)]
42    fn advance_by(&mut self, n: usize) -> Result<Result<(), NonZeroUsize>, Self::Error> {
43        self.lender.advance_back_by(n)
44    }
45
46    #[inline(always)]
47    fn nth(&mut self, n: usize) -> Result<Option<FallibleLend<'_, Self>>, Self::Error> {
48        self.lender.nth_back(n)
49    }
50
51    #[inline(always)]
52    fn try_fold<B, F, R>(&mut self, init: B, f: F) -> Result<R, Self::Error>
53    where
54        Self: Sized,
55        F: FnMut(B, FallibleLend<'_, Self>) -> Result<R, Self::Error>,
56        R: Try<Output = B>,
57    {
58        self.lender.try_rfold(init, f)
59    }
60
61    #[inline(always)]
62    fn fold<B, F>(self, init: B, f: F) -> Result<B, Self::Error>
63    where
64        Self: Sized,
65        F: FnMut(B, FallibleLend<'_, Self>) -> Result<B, Self::Error>,
66    {
67        self.lender.rfold(init, f)
68    }
69
70    #[inline(always)]
71    fn find<P>(&mut self, predicate: P) -> Result<Option<FallibleLend<'_, Self>>, Self::Error>
72    where
73        Self: Sized,
74        P: FnMut(&FallibleLend<'_, Self>) -> Result<bool, Self::Error>,
75    {
76        self.lender.rfind(predicate)
77    }
78}
79
80impl<L> DoubleEndedFallibleLender for Rev<L>
81where
82    L: DoubleEndedFallibleLender,
83{
84    #[inline(always)]
85    fn next_back(&mut self) -> Result<Option<FallibleLend<'_, Self>>, Self::Error> {
86        self.lender.next()
87    }
88
89    #[inline(always)]
90    fn advance_back_by(&mut self, n: usize) -> Result<Result<(), NonZeroUsize>, Self::Error> {
91        self.lender.advance_by(n)
92    }
93
94    #[inline(always)]
95    fn nth_back(&mut self, n: usize) -> Result<Option<FallibleLend<'_, Self>>, Self::Error> {
96        self.lender.nth(n)
97    }
98
99    #[inline(always)]
100    fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> Result<R, Self::Error>
101    where
102        Self: Sized,
103        F: FnMut(B, FallibleLend<'_, Self>) -> Result<R, Self::Error>,
104        R: Try<Output = B>,
105    {
106        self.lender.try_fold(init, f)
107    }
108
109    #[inline(always)]
110    fn rfold<B, F>(self, init: B, f: F) -> Result<B, Self::Error>
111    where
112        Self: Sized,
113        F: FnMut(B, FallibleLend<'_, Self>) -> Result<B, Self::Error>,
114    {
115        self.lender.fold(init, f)
116    }
117
118    #[inline(always)]
119    fn rfind<P>(&mut self, predicate: P) -> Result<Option<FallibleLend<'_, Self>>, Self::Error>
120    where
121        Self: Sized,
122        P: FnMut(&FallibleLend<'_, Self>) -> Result<bool, Self::Error>,
123    {
124        self.lender.find(predicate)
125    }
126}
127
128impl<L> ExactSizeFallibleLender for Rev<L>
129where
130    L: ExactSizeFallibleLender + DoubleEndedFallibleLender,
131{
132    #[inline(always)]
133    fn len(&self) -> usize {
134        self.lender.len()
135    }
136
137    #[inline(always)]
138    fn is_empty(&self) -> bool {
139        self.lender.is_empty()
140    }
141}
142
143impl<L> FusedFallibleLender for Rev<L> where L: DoubleEndedFallibleLender + FusedFallibleLender {}