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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
/// Extension trait for `Iterator<Item = Result<O, E>>` to selectively transform Oks and Errors.
pub trait FlattenX<O, E>: Sized {
fn flatten_ok<U, O2>(self) -> FlattenOk<Self, U>
where
U: IntoIterator<Item = O2>;
fn flatten_err<U, E2>(self) -> FlattenErr<Self, U>
where
U: IntoIterator<Item = E2>;
}
impl<I, O, E> FlattenX<O, E> for I
where
I: Iterator<Item = Result<O, E>> + Sized,
{
fn flatten_ok<U, O2>(self) -> FlattenOk<Self, U>
where
U: IntoIterator<Item = O2>,
{
FlattenOk {
frontiter: None,
iter: self,
}
}
fn flatten_err<U, E2>(self) -> FlattenErr<Self, U>
where
U: IntoIterator<Item = E2>,
{
FlattenErr {
frontiter: None,
iter: self,
}
}
}
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct FlattenOk<I, U>
where
U: IntoIterator,
{
frontiter: Option<<U as IntoIterator>::IntoIter>,
iter: I,
}
impl<I, E, O2, U> Iterator for FlattenOk<I, U>
where
I: Iterator<Item = Result<U, E>>,
U: IntoIterator<Item = O2>,
{
type Item = Result<O2, E>;
fn next(&mut self) -> Option<Self::Item> {
loop {
if let Some(ref mut inner) = self.frontiter {
if let elt @ Some(_) = inner.next() {
return elt.map(Ok);
}
}
match self.iter.next() {
None => return None,
Some(Ok(x)) => {
self.frontiter = Some(x.into_iter());
}
Some(Err(e)) => return Some(Err(e)),
}
}
}
#[inline]
// TODO: Oh dear, this hint could be much better
// https://doc.rust-lang.org/src/core/iter/mod.rs.html#2694
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct FlattenErr<I, U: IntoIterator> {
frontiter: Option<<U as IntoIterator>::IntoIter>,
iter: I,
}
impl<I, O, E2, U> Iterator for FlattenErr<I, U>
where
I: Iterator<Item = Result<O, U>>,
U: IntoIterator<Item = E2>,
{
type Item = Result<O, E2>;
fn next(&mut self) -> Option<Self::Item> {
loop {
if let Some(ref mut inner) = self.frontiter {
if let elt @ Some(_) = inner.next() {
return elt.map(Err);
}
}
match self.iter.next() {
None => return None,
Some(Err(e)) => {
self.frontiter = Some(e.into_iter());
}
Some(Ok(o)) => return Some(Ok(o)),
}
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
#[test]
fn test_flatten_ok() {
use map_x::MapX;
let mapped: Vec<_> = vec![Ok(1), Ok(2), Err(2), Err(0), Ok(2)]
.into_iter()
.map_ok(|i| (0..i))
.map_err(|i| 0..(i * 2))
.flatten_ok()
.flatten_err()
.collect();
assert_eq!(
mapped,
[
Ok(0),
Ok(0),
Ok(1),
Err(0),
Err(1),
Err(2),
Err(3),
Ok(0),
Ok(1)
]
);
}
|