Skip to content

Commit 02483ae

Browse files
MultiProduct state: comment with code
1 parent c13391d commit 02483ae

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

src/adaptors/multi_product.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![cfg(feature = "use_alloc")]
2+
use Option::{self as State, None as ProductEnded, Some as ProductInProgress};
23

34
use alloc::vec::Vec;
45

@@ -13,10 +14,7 @@ use crate::size_hint;
1314
/// See [`.multi_cartesian_product()`](crate::Itertools::multi_cartesian_product)
1415
/// for more information.
1516
#[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>>)
2018
where
2119
I: Iterator + Clone,
2220
I::Item: Clone;
@@ -67,7 +65,7 @@ where
6765
.collect(),
6866
cur: None,
6967
};
70-
MultiProduct(Some(inner))
68+
MultiProduct(ProductInProgress(inner))
7169
}
7270

7371
#[derive(Clone, Debug)]
@@ -119,16 +117,15 @@ where
119117
*item = iter.iter.next().unwrap();
120118
}
121119
}
122-
// The iterator ends.
123-
self.0 = None;
120+
self.0 = ProductEnded;
124121
None
125122
}
126123
// Only the first time.
127124
None => {
128125
let next: Option<Vec<_>> = inner.iters.iter_mut().map(|i| i.iter.next()).collect();
129126
if next.is_none() || inner.iters.is_empty() {
130127
// This cartesian product had at most one item to generate and now ends.
131-
self.0 = None;
128+
self.0 = ProductEnded;
132129
} else {
133130
inner.cur = next.clone();
134131
}
@@ -139,8 +136,8 @@ where
139136

140137
fn count(self) -> usize {
141138
match self.0 {
142-
None => 0, // The cartesian product has ended.
143-
Some(MultiProductInner { iters, cur }) => {
139+
ProductEnded => 0,
140+
ProductInProgress(MultiProductInner { iters, cur }) => {
144141
if cur.is_none() {
145142
// The iterator is fresh so the count is the product of the length of each iterator:
146143
// - If one of them is empty, stop counting.
@@ -171,8 +168,8 @@ where
171168

172169
fn size_hint(&self) -> (usize, Option<usize>) {
173170
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 }) => {
176173
if cur.is_none() {
177174
iters
178175
.iter()

0 commit comments

Comments
 (0)