1
1
use super :: { for_both, Either , Left , Right } ;
2
2
use core:: iter;
3
3
4
- macro_rules! map_either {
5
- ( $value: expr, $pattern: pat => $result: expr) => {
6
- match $value {
7
- Left ( $pattern) => Left ( $result) ,
8
- Right ( $pattern) => Right ( $result) ,
9
- }
10
- } ;
11
- }
12
-
13
4
macro_rules! wrap_either {
14
5
( $value: expr => $( $tail: tt ) * ) => {
15
6
match $value {
@@ -19,164 +10,6 @@ macro_rules! wrap_either {
19
10
} ;
20
11
}
21
12
22
- impl < L , R > Either < L , R > {
23
- /// Convert the inner value to an iterator.
24
- ///
25
- /// This requires the `Left` and `Right` iterators to have the same item type.
26
- /// See [`factor_into_iter`][Either::factor_into_iter] to iterate different types.
27
- ///
28
- /// ```
29
- /// use either::*;
30
- ///
31
- /// let left: Either<_, Vec<u32>> = Left(vec![1, 2, 3, 4, 5]);
32
- /// let mut right: Either<Vec<u32>, _> = Right(vec![]);
33
- /// right.extend(left.into_iter());
34
- /// assert_eq!(right, Right(vec![1, 2, 3, 4, 5]));
35
- /// ```
36
- #[ allow( clippy:: should_implement_trait) ]
37
- pub fn into_iter ( self ) -> Either < L :: IntoIter , R :: IntoIter >
38
- where
39
- L : IntoIterator ,
40
- R : IntoIterator < Item = L :: Item > ,
41
- {
42
- map_either ! ( self , inner => inner. into_iter( ) )
43
- }
44
-
45
- /// Borrow the inner value as an iterator.
46
- ///
47
- /// This requires the `Left` and `Right` iterators to have the same item type.
48
- /// See [`factor_iter`][Either::factor_iter] to iterate different types.
49
- ///
50
- /// ```
51
- /// use either::*;
52
- ///
53
- /// let left: Either<_, &[u32]> = Left(vec![2, 3]);
54
- /// let mut right: Either<Vec<u32>, _> = Right(&[4, 5][..]);
55
- /// let mut all = vec![1];
56
- /// all.extend(left.iter());
57
- /// all.extend(right.iter());
58
- /// assert_eq!(all, vec![1, 2, 3, 4, 5]);
59
- /// ```
60
- pub fn iter ( & self ) -> Either < <& L as IntoIterator >:: IntoIter , <& R as IntoIterator >:: IntoIter >
61
- where
62
- for < ' a > & ' a L : IntoIterator ,
63
- for < ' a > & ' a R : IntoIterator < Item = <& ' a L as IntoIterator >:: Item > ,
64
- {
65
- map_either ! ( self , inner => inner. into_iter( ) )
66
- }
67
-
68
- /// Mutably borrow the inner value as an iterator.
69
- ///
70
- /// This requires the `Left` and `Right` iterators to have the same item type.
71
- /// See [`factor_iter_mut`][Either::factor_iter_mut] to iterate different types.
72
- ///
73
- /// ```
74
- /// use either::*;
75
- ///
76
- /// let mut left: Either<_, &mut [u32]> = Left(vec![2, 3]);
77
- /// for l in left.iter_mut() {
78
- /// *l *= *l
79
- /// }
80
- /// assert_eq!(left, Left(vec![4, 9]));
81
- ///
82
- /// let mut inner = [4, 5];
83
- /// let mut right: Either<Vec<u32>, _> = Right(&mut inner[..]);
84
- /// for r in right.iter_mut() {
85
- /// *r *= *r
86
- /// }
87
- /// assert_eq!(inner, [16, 25]);
88
- /// ```
89
- pub fn iter_mut (
90
- & mut self ,
91
- ) -> Either < <& mut L as IntoIterator >:: IntoIter , <& mut R as IntoIterator >:: IntoIter >
92
- where
93
- for < ' a > & ' a mut L : IntoIterator ,
94
- for < ' a > & ' a mut R : IntoIterator < Item = <& ' a mut L as IntoIterator >:: Item > ,
95
- {
96
- map_either ! ( self , inner => inner. into_iter( ) )
97
- }
98
-
99
- /// Converts an `Either` of `Iterator`s to be an `Iterator` of `Either`s
100
- ///
101
- /// Unlike [`into_iter`][Either::into_iter], this does not require the
102
- /// `Left` and `Right` iterators to have the same item type.
103
- ///
104
- /// ```
105
- /// use either::*;
106
- /// let left: Either<_, Vec<u8>> = Left(&["hello"]);
107
- /// assert_eq!(left.factor_into_iter().next(), Some(Left(&"hello")));
108
-
109
- /// let right: Either<&[&str], _> = Right(vec![0, 1]);
110
- /// assert_eq!(right.factor_into_iter().collect::<Vec<_>>(), vec![Right(0), Right(1)]);
111
- ///
112
- /// ```
113
- // TODO(MSRV): doc(alias) was stabilized in Rust 1.48
114
- // #[doc(alias = "transpose")]
115
- pub fn factor_into_iter ( self ) -> IterEither < L :: IntoIter , R :: IntoIter >
116
- where
117
- L : IntoIterator ,
118
- R : IntoIterator ,
119
- {
120
- IterEither {
121
- inner : map_either ! ( self , inner => inner. into_iter( ) ) ,
122
- }
123
- }
124
-
125
- /// Borrows an `Either` of `Iterator`s to be an `Iterator` of `Either`s
126
- ///
127
- /// Unlike [`iter`][Either::iter], this does not require the
128
- /// `Left` and `Right` iterators to have the same item type.
129
- ///
130
- /// ```
131
- /// use either::*;
132
- /// let left: Either<_, Vec<u8>> = Left(["hello"]);
133
- /// assert_eq!(left.factor_iter().next(), Some(Left(&"hello")));
134
-
135
- /// let right: Either<[&str; 2], _> = Right(vec![0, 1]);
136
- /// assert_eq!(right.factor_iter().collect::<Vec<_>>(), vec![Right(&0), Right(&1)]);
137
- ///
138
- /// ```
139
- pub fn factor_iter (
140
- & self ,
141
- ) -> IterEither < <& L as IntoIterator >:: IntoIter , <& R as IntoIterator >:: IntoIter >
142
- where
143
- for < ' a > & ' a L : IntoIterator ,
144
- for < ' a > & ' a R : IntoIterator ,
145
- {
146
- IterEither {
147
- inner : map_either ! ( self , inner => inner. into_iter( ) ) ,
148
- }
149
- }
150
-
151
- /// Mutably borrows an `Either` of `Iterator`s to be an `Iterator` of `Either`s
152
- ///
153
- /// Unlike [`iter_mut`][Either::iter_mut], this does not require the
154
- /// `Left` and `Right` iterators to have the same item type.
155
- ///
156
- /// ```
157
- /// use either::*;
158
- /// let mut left: Either<_, Vec<u8>> = Left(["hello"]);
159
- /// left.factor_iter_mut().for_each(|x| *x.unwrap_left() = "goodbye");
160
- /// assert_eq!(left, Left(["goodbye"]));
161
-
162
- /// let mut right: Either<[&str; 2], _> = Right(vec![0, 1, 2]);
163
- /// right.factor_iter_mut().for_each(|x| if let Right(r) = x { *r = -*r; });
164
- /// assert_eq!(right, Right(vec![0, -1, -2]));
165
- ///
166
- /// ```
167
- pub fn factor_iter_mut (
168
- & mut self ,
169
- ) -> IterEither < <& mut L as IntoIterator >:: IntoIter , <& mut R as IntoIterator >:: IntoIter >
170
- where
171
- for < ' a > & ' a mut L : IntoIterator ,
172
- for < ' a > & ' a mut R : IntoIterator ,
173
- {
174
- IterEither {
175
- inner : map_either ! ( self , inner => inner. into_iter( ) ) ,
176
- }
177
- }
178
- }
179
-
180
13
/// Iterator that maps left or right iterators to corresponding `Either`-wrapped items.
181
14
///
182
15
/// This struct is created by the [`Either::factor_into_iter`],
@@ -187,6 +20,12 @@ pub struct IterEither<L, R> {
187
20
inner : Either < L , R > ,
188
21
}
189
22
23
+ impl < L , R > IterEither < L , R > {
24
+ pub ( crate ) fn new ( inner : Either < L , R > ) -> Self {
25
+ IterEither { inner }
26
+ }
27
+ }
28
+
190
29
impl < L , R , A > Extend < A > for Either < L , R >
191
30
where
192
31
L : Extend < A > ,
0 commit comments