1use alloc::borrow::ToOwned;
2use core::iter::FusedIterator;
3
4use crate::{DoubleEndedLender, ExactSizeLender, FusedLender, Lend, Lender};
5
6#[derive(Clone, Debug)]
11#[must_use = "iterators are lazy and do nothing unless consumed"]
12pub struct Owned<L> {
13 pub(crate) lender: L,
14}
15
16impl<L> Owned<L> {
17 #[inline(always)]
18 pub(crate) fn new(lender: L) -> Self {
19 Self { lender }
20 }
21
22 #[inline(always)]
24 pub fn into_inner(self) -> L {
25 self.lender
26 }
27}
28
29impl<T, L> Iterator for Owned<L>
30where
31 L: Lender,
32 for<'all> Lend<'all, L>: ToOwned<Owned = T>,
33{
34 type Item = T;
35 #[inline]
36 fn next(&mut self) -> Option<Self::Item> {
37 self.lender.next().map(|ref x| x.to_owned())
38 }
39
40 #[inline(always)]
41 fn size_hint(&self) -> (usize, Option<usize>) {
42 self.lender.size_hint()
43 }
44
45 #[inline]
46 fn fold<B, F>(self, init: B, mut f: F) -> B
47 where
48 F: FnMut(B, Self::Item) -> B,
49 {
50 self.lender.fold(init, |acc, ref x| f(acc, x.to_owned()))
51 }
52
53 #[inline(always)]
54 fn count(self) -> usize {
55 self.lender.count()
56 }
57
58 #[inline]
59 fn nth(&mut self, n: usize) -> Option<Self::Item> {
60 self.lender.nth(n).map(|ref x| x.to_owned())
61 }
62}
63
64impl<T, L> DoubleEndedIterator for Owned<L>
65where
66 L: DoubleEndedLender,
67 for<'all> Lend<'all, L>: ToOwned<Owned = T>,
68{
69 #[inline]
70 fn next_back(&mut self) -> Option<Self::Item> {
71 self.lender.next_back().map(|ref x| x.to_owned())
72 }
73
74 #[inline]
75 fn rfold<B, F>(self, init: B, mut f: F) -> B
76 where
77 F: FnMut(B, Self::Item) -> B,
78 {
79 self.lender.rfold(init, |acc, ref x| f(acc, x.to_owned()))
80 }
81}
82
83impl<T, L> ExactSizeIterator for Owned<L>
84where
85 L: ExactSizeLender,
86 for<'all> Lend<'all, L>: ToOwned<Owned = T>,
87{
88 #[inline(always)]
89 fn len(&self) -> usize {
90 self.lender.len()
91 }
92}
93
94impl<T, L> FusedIterator for Owned<L>
95where
96 L: FusedLender,
97 for<'all> Lend<'all, L>: ToOwned<Owned = T>,
98{
99}
100
101impl<L> Default for Owned<L>
102where
103 L: Default,
104{
105 #[inline(always)]
106 fn default() -> Self {
107 Self::new(L::default())
108 }
109}