1use fallible_iterator::{DoubleEndedFallibleIterator, FallibleIterator, IntoFallibleIterator};
2
3use crate::{
4 CovariantFallibleLending, DoubleEndedFallibleLender, FallibleLend, FallibleLender,
5 FallibleLending, IntoFallibleLender, prelude::*,
6};
7
8#[inline]
27pub fn from_iter<I: FallibleIterator>(iter: I) -> FromIter<I> {
28 FromIter { iter }
29}
30
31#[derive(Clone, Debug)]
37#[repr(transparent)]
38#[must_use = "lenders are lazy and do nothing unless consumed"]
39pub struct FromIter<I> {
40 iter: I,
41}
42
43impl<I: FallibleIterator> FallibleLending<'_> for FromIter<I> {
44 type Lend = I::Item;
45}
46
47impl<I: FallibleIterator> FallibleLender for FromIter<I> {
48 type Error = I::Error;
49 crate::check_covariance_fallible!();
50 #[inline(always)]
51 fn next(&mut self) -> Result<Option<FallibleLend<'_, Self>>, Self::Error> {
52 self.iter.next()
53 }
54
55 #[inline(always)]
56 fn size_hint(&self) -> (usize, Option<usize>) {
57 self.iter.size_hint()
58 }
59
60 #[inline(always)]
61 fn nth(&mut self, n: usize) -> Result<Option<FallibleLend<'_, Self>>, Self::Error> {
62 self.iter.nth(n)
63 }
64
65 #[inline(always)]
66 fn count(self) -> Result<usize, Self::Error>
67 where
68 Self: Sized,
69 {
70 self.iter.count()
71 }
72
73 #[inline(always)]
74 fn fold<B, F>(self, init: B, f: F) -> Result<B, Self::Error>
75 where
76 Self: Sized,
77 F: FnMut(B, FallibleLend<'_, Self>) -> Result<B, Self::Error>,
78 {
79 self.iter.fold(init, f)
80 }
81}
82
83impl<I: DoubleEndedFallibleIterator> DoubleEndedFallibleLender for FromIter<I> {
84 #[inline(always)]
85 fn next_back(&mut self) -> Result<Option<FallibleLend<'_, Self>>, Self::Error> {
86 self.iter.next_back()
87 }
88
89 #[inline(always)]
90 fn rfold<B, F>(self, init: B, f: F) -> Result<B, Self::Error>
91 where
92 Self: Sized,
93 F: FnMut(B, FallibleLend<'_, Self>) -> Result<B, Self::Error>,
94 {
95 self.iter.rfold(init, f)
96 }
97}
98
99impl<I: FallibleIterator> From<I> for FromIter<I> {
105 #[inline(always)]
106 fn from(iter: I) -> Self {
107 from_iter(iter)
108 }
109}
110
111#[inline]
137pub fn from_into_iter<I: IntoFallibleIterator>(into_iter: I) -> FromIntoIter<I> {
138 FromIntoIter { into_iter }
139}
140
141#[repr(transparent)]
151#[derive(Clone, Debug)]
152pub struct FromIntoIter<I> {
153 into_iter: I,
154}
155
156impl<I: IntoFallibleIterator> IntoFallibleLender for FromIntoIter<I> {
157 type Error = I::Error;
158
159 type FallibleLender = FromIter<I::IntoFallibleIter>;
160
161 #[inline(always)]
162 fn into_fallible_lender(self) -> <Self as IntoFallibleLender>::FallibleLender {
163 self.into_iter.into_fallible_iter().into_fallible_lender()
164 }
165}
166
167impl<I: IntoFallibleIterator> From<I> for FromIntoIter<I> {
168 #[inline(always)]
169 fn from(into_iter: I) -> Self {
170 from_into_iter(into_iter)
171 }
172}
173
174#[inline]
203pub fn lend_iter<'a, L, I>(iter: I) -> LendIter<'a, L, I>
204where
205 L: ?Sized + CovariantFallibleLending + 'a,
206 I: FallibleIterator<Item = FallibleLend<'a, L>>,
207{
208 let _ = L::__check_covariance(crate::CovariantProof::new());
209 LendIter {
210 iter,
211 _marker: core::marker::PhantomData,
212 }
213}
214
215#[derive(Clone, Debug)]
224#[must_use = "lenders are lazy and do nothing unless consumed"]
225pub struct LendIter<'a, L: ?Sized, I> {
226 iter: I,
227 _marker: core::marker::PhantomData<&'a L>,
228}
229
230impl<'a, 'lend, L, I> FallibleLending<'lend> for LendIter<'a, L, I>
231where
232 L: ?Sized + CovariantFallibleLending + 'a,
233 I: FallibleIterator<Item = FallibleLend<'a, L>>,
234{
235 type Lend = FallibleLend<'lend, L>;
236}
237
238impl<'a, L, I> FallibleLender for LendIter<'a, L, I>
239where
240 L: ?Sized + CovariantFallibleLending + 'a,
241 I: FallibleIterator<Item = FallibleLend<'a, L>>,
242{
243 type Error = I::Error;
244 crate::unsafe_assume_covariance_fallible!();
246
247 #[inline]
248 fn next(&mut self) -> Result<Option<FallibleLend<'_, Self>>, Self::Error> {
249 let next = self.iter.next()?;
250 Ok(
251 unsafe {
253 core::mem::transmute::<Option<FallibleLend<'a, L>>, Option<FallibleLend<'_, L>>>(
254 next,
255 )
256 },
257 )
258 }
259
260 #[inline(always)]
261 fn size_hint(&self) -> (usize, Option<usize>) {
262 self.iter.size_hint()
263 }
264
265 #[inline]
266 fn nth(&mut self, n: usize) -> Result<Option<FallibleLend<'_, Self>>, Self::Error> {
267 let nth = self.iter.nth(n)?;
268 Ok(
269 unsafe {
271 core::mem::transmute::<Option<FallibleLend<'a, L>>, Option<FallibleLend<'_, L>>>(
272 nth,
273 )
274 },
275 )
276 }
277
278 #[inline(always)]
279 fn count(self) -> Result<usize, Self::Error>
280 where
281 Self: Sized,
282 {
283 self.iter.count()
284 }
285
286 #[inline]
287 fn fold<B, F>(self, init: B, mut f: F) -> Result<B, Self::Error>
288 where
289 Self: Sized,
290 F: FnMut(B, FallibleLend<'_, Self>) -> Result<B, Self::Error>,
291 {
292 self.iter.fold(init, |acc, x| {
293 f(acc, unsafe {
295 core::mem::transmute::<FallibleLend<'a, L>, FallibleLend<'_, L>>(x)
296 })
297 })
298 }
299}
300
301impl<'a, L, I> DoubleEndedFallibleLender for LendIter<'a, L, I>
302where
303 L: ?Sized + CovariantFallibleLending + 'a,
304 I: DoubleEndedFallibleIterator<Item = FallibleLend<'a, L>>,
305{
306 #[inline]
307 fn next_back(&mut self) -> Result<Option<FallibleLend<'_, Self>>, Self::Error> {
308 let next = self.iter.next_back()?;
309 Ok(
310 unsafe {
312 core::mem::transmute::<Option<FallibleLend<'a, L>>, Option<FallibleLend<'_, L>>>(
313 next,
314 )
315 },
316 )
317 }
318
319 #[inline]
320 fn rfold<B, F>(self, init: B, mut f: F) -> Result<B, Self::Error>
321 where
322 Self: Sized,
323 F: FnMut(B, FallibleLend<'_, Self>) -> Result<B, Self::Error>,
324 {
325 self.iter.rfold(init, |acc, x| {
326 f(acc, unsafe {
328 core::mem::transmute::<FallibleLend<'a, L>, FallibleLend<'_, L>>(x)
329 })
330 })
331 }
332}
333
334