1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::{iter::FusedIterator, marker::PhantomData};
pub trait From<T> {
fn from(source: T) -> Self;
}
pub trait Into<T> {
fn into(self) -> T;
}
impl<T, U> Into<T> for U
where
T: From<U>,
{
fn into(self) -> T {
T::from(self)
}
}
#[derive(Clone)]
pub struct MapInto<I, T> {
inner: I,
_phantom: PhantomData<T>,
}
impl<I, T> MapInto<I, T> {
pub fn new(inner: I) -> Self {
Self {
inner,
_phantom: PhantomData,
}
}
}
impl<I, T> Iterator for MapInto<I, T>
where
I: Iterator,
T: From<I::Item>,
{
type Item = T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(T::from)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
#[inline]
fn count(self) -> usize {
self.inner.count()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.inner.nth(n).map(T::from)
}
#[inline]
fn last(self) -> Option<Self::Item> {
self.inner.last().map(T::from)
}
}
impl<I, T> DoubleEndedIterator for MapInto<I, T>
where
I: DoubleEndedIterator,
T: From<I::Item>,
{
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.inner.next_back().map(T::from)
}
#[inline]
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
self.inner.nth_back(n).map(T::from)
}
}
impl<I, T> ExactSizeIterator for MapInto<I, T>
where
I: ExactSizeIterator,
T: From<I::Item>,
{
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}
impl<I, T> FusedIterator for MapInto<I, T>
where
I: FusedIterator,
T: From<I::Item>,
{
}