Skip to content

Commit

Permalink
added empty() to Peekable
Browse files Browse the repository at this point in the history
  • Loading branch information
khodzha committed Jan 11, 2014
1 parent f0541d5 commit 901dc2c
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/libstd/iter.rs
@@ -1,4 +1,4 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand Down Expand Up @@ -1362,6 +1362,12 @@ impl<'a, A, T: Iterator<A>> Peekable<A, T> {
None => None,
}
}

/// Check whether peekable iterator is empty or not.
#[inline]
pub fn empty(&mut self) -> bool {
self.peek().is_some()
}
}

/// An iterator which rejects elements while `predicate` is true
Expand Down Expand Up @@ -2923,4 +2929,12 @@ mod tests {
ys.mut_iter().reverse_();
assert_eq!(ys, [5, 4, 3, 2, 1]);
}

fn test_peekable_empty() {
let a = [1];
let mut it = a.iter().peekable();
assert!( !it.empty() );
it.next();
assert!( it.empty() );
}
}

0 comments on commit 901dc2c

Please sign in to comment.