Skip to content

Commit f8d6fc1

Browse files
committed
Open-code Fuse's Option matches
1 parent f6fe99c commit f8d6fc1

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

src/libcore/iter/adapters/fuse.rs

+42-35
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ impl<I> Fuse<I> {
2828
#[stable(feature = "fused", since = "1.26.0")]
2929
impl<I> FusedIterator for Fuse<I> where I: Iterator {}
3030

31+
/// Fuse the iterator if the expression is `None`.
32+
macro_rules! fuse {
33+
($self:ident . iter . $($call:tt)+) => {
34+
match $self.iter {
35+
Some(ref mut iter) => match iter.$($call)+ {
36+
None => {
37+
$self.iter = None;
38+
None
39+
}
40+
item => item,
41+
},
42+
None => None,
43+
}
44+
};
45+
}
46+
3147
#[stable(feature = "rust1", since = "1.0.0")]
3248
impl<I> Iterator for Fuse<I>
3349
where
@@ -37,35 +53,36 @@ where
3753

3854
#[inline]
3955
default fn next(&mut self) -> Option<<I as Iterator>::Item> {
40-
let next = self.iter.as_mut()?.next();
41-
if next.is_none() {
42-
self.iter = None;
43-
}
44-
next
56+
fuse!(self.iter.next())
4557
}
4658

4759
#[inline]
4860
default fn nth(&mut self, n: usize) -> Option<I::Item> {
49-
let nth = self.iter.as_mut()?.nth(n);
50-
if nth.is_none() {
51-
self.iter = None;
52-
}
53-
nth
61+
fuse!(self.iter.nth(n))
5462
}
5563

5664
#[inline]
5765
default fn last(self) -> Option<I::Item> {
58-
self.iter?.last()
66+
match self.iter {
67+
Some(iter) => iter.last(),
68+
None => None,
69+
}
5970
}
6071

6172
#[inline]
6273
default fn count(self) -> usize {
63-
self.iter.map_or(0, I::count)
74+
match self.iter {
75+
Some(iter) => iter.count(),
76+
None => 0,
77+
}
6478
}
6579

6680
#[inline]
6781
default fn size_hint(&self) -> (usize, Option<usize>) {
68-
self.iter.as_ref().map_or((0, Some(0)), I::size_hint)
82+
match self.iter {
83+
Some(ref iter) => iter.size_hint(),
84+
None => (0, Some(0)),
85+
}
6986
}
7087

7188
#[inline]
@@ -98,11 +115,7 @@ where
98115
where
99116
P: FnMut(&Self::Item) -> bool,
100117
{
101-
let found = self.iter.as_mut()?.find(predicate);
102-
if found.is_none() {
103-
self.iter = None;
104-
}
105-
found
118+
fuse!(self.iter.find(predicate))
106119
}
107120
}
108121

@@ -113,20 +126,12 @@ where
113126
{
114127
#[inline]
115128
default fn next_back(&mut self) -> Option<<I as Iterator>::Item> {
116-
let next = self.iter.as_mut()?.next_back();
117-
if next.is_none() {
118-
self.iter = None;
119-
}
120-
next
129+
fuse!(self.iter.next_back())
121130
}
122131

123132
#[inline]
124133
default fn nth_back(&mut self, n: usize) -> Option<<I as Iterator>::Item> {
125-
let nth = self.iter.as_mut()?.nth_back(n);
126-
if nth.is_none() {
127-
self.iter = None;
128-
}
129-
nth
134+
fuse!(self.iter.nth_back(n))
130135
}
131136

132137
#[inline]
@@ -159,11 +164,7 @@ where
159164
where
160165
P: FnMut(&Self::Item) -> bool,
161166
{
162-
let found = self.iter.as_mut()?.rfind(predicate);
163-
if found.is_none() {
164-
self.iter = None;
165-
}
166-
found
167+
fuse!(self.iter.rfind(predicate))
167168
}
168169
}
169170

@@ -173,11 +174,17 @@ where
173174
I: ExactSizeIterator,
174175
{
175176
default fn len(&self) -> usize {
176-
self.iter.as_ref().map_or(0, I::len)
177+
match self.iter {
178+
Some(ref iter) => iter.len(),
179+
None => 0,
180+
}
177181
}
178182

179183
default fn is_empty(&self) -> bool {
180-
self.iter.as_ref().map_or(true, I::is_empty)
184+
match self.iter {
185+
Some(ref iter) => iter.is_empty(),
186+
None => true,
187+
}
181188
}
182189
}
183190

0 commit comments

Comments
 (0)