1
1
#![ cfg( feature = "use_alloc" ) ]
2
+ use Option :: { self as State , None as ProductEnded , Some as ProductInProgress } ;
2
3
3
4
use alloc:: vec:: Vec ;
4
5
@@ -13,10 +14,7 @@ use crate::size_hint;
13
14
/// See [`.multi_cartesian_product()`](crate::Itertools::multi_cartesian_product)
14
15
/// for more information.
15
16
#[ must_use = "iterator adaptors are lazy and do nothing unless consumed" ]
16
- pub struct MultiProduct < I > (
17
- /// `None` once the iterator has ended.
18
- Option < MultiProductInner < I > > ,
19
- )
17
+ pub struct MultiProduct < I > ( State < MultiProductInner < I > > )
20
18
where
21
19
I : Iterator + Clone ,
22
20
I :: Item : Clone ;
67
65
. collect ( ) ,
68
66
cur : None ,
69
67
} ;
70
- MultiProduct ( Some ( inner) )
68
+ MultiProduct ( ProductInProgress ( inner) )
71
69
}
72
70
73
71
#[ derive( Clone , Debug ) ]
@@ -119,16 +117,15 @@ where
119
117
* item = iter. iter . next ( ) . unwrap ( ) ;
120
118
}
121
119
}
122
- // The iterator ends.
123
- self . 0 = None ;
120
+ self . 0 = ProductEnded ;
124
121
None
125
122
}
126
123
// Only the first time.
127
124
None => {
128
125
let next: Option < Vec < _ > > = inner. iters . iter_mut ( ) . map ( |i| i. iter . next ( ) ) . collect ( ) ;
129
126
if next. is_none ( ) || inner. iters . is_empty ( ) {
130
127
// This cartesian product had at most one item to generate and now ends.
131
- self . 0 = None ;
128
+ self . 0 = ProductEnded ;
132
129
} else {
133
130
inner. cur = next. clone ( ) ;
134
131
}
@@ -139,8 +136,8 @@ where
139
136
140
137
fn count ( self ) -> usize {
141
138
match self . 0 {
142
- None => 0 , // The cartesian product has ended.
143
- Some ( MultiProductInner { iters, cur } ) => {
139
+ ProductEnded => 0 ,
140
+ ProductInProgress ( MultiProductInner { iters, cur } ) => {
144
141
if cur. is_none ( ) {
145
142
// The iterator is fresh so the count is the product of the length of each iterator:
146
143
// - If one of them is empty, stop counting.
@@ -171,8 +168,8 @@ where
171
168
172
169
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
173
170
match & self . 0 {
174
- None => ( 0 , Some ( 0 ) ) , // The cartesian product has ended.
175
- Some ( MultiProductInner { iters, cur } ) => {
171
+ ProductEnded => ( 0 , Some ( 0 ) ) ,
172
+ ProductInProgress ( MultiProductInner { iters, cur } ) => {
176
173
if cur. is_none ( ) {
177
174
iters
178
175
. iter ( )
0 commit comments