Skip to content

Commit 1e1871f

Browse files
committed
auto merge of #11479 : khodzha/rust/peekable_empty, r=brson
to fix #11218
2 parents 7d75bbf + 4993c7c commit 1e1871f

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/libstd/iter.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -1362,6 +1362,12 @@ impl<'a, A, T: Iterator<A>> Peekable<A, T> {
13621362
None => None,
13631363
}
13641364
}
1365+
1366+
/// Check whether peekable iterator is empty or not.
1367+
#[inline]
1368+
pub fn is_empty(&mut self) -> bool {
1369+
self.peek().is_some()
1370+
}
13651371
}
13661372

13671373
/// An iterator which rejects elements while `predicate` is true
@@ -2923,4 +2929,12 @@ mod tests {
29232929
ys.mut_iter().reverse_();
29242930
assert_eq!(ys, [5, 4, 3, 2, 1]);
29252931
}
2932+
2933+
fn test_peekable_is_empty() {
2934+
let a = [1];
2935+
let mut it = a.iter().peekable();
2936+
assert!( !it.is_empty() );
2937+
it.next();
2938+
assert!( it.is_empty() );
2939+
}
29262940
}

0 commit comments

Comments
 (0)