A performant iterator providing double peek functionality.
This crate provides a Peekable
iterator adapter that allows looking ahead by up to two elements without advancing the
iterator. It maintains performance parity with core::iter::Peekable
for single peek operations, with only a minimal 90 picosecond overhead for double peek operations.
Key Features
- Double Peek: Look ahead by up to two elements while maintaining iterator position.
- Performance: Single peek operations are as fast or faster than
core::iter::Peekable. - Greater Control: Enhanced functionality for conditional iteration and element consumption.
Basic Usage
use Peekable;
let mut iter = new;
assert_eq!; // Look at next element
assert_eq!; // Look two elements ahead
assert_eq!; // Iterator position unchanged
Enhanced Peek Control
The Peek type returned by
peek provides
additional control over iteration:
# use Peekable;
let mut iter = new;
let mut peek = iter.peek;
// Examine current and next elements
assert_eq!;
assert_eq!;
// Conditionally consume elements (moving the iterator forward)
if peek == Some
Conditional Draining
The drain_if method
allows consuming multiple elements based on lookahead:
# use Peekable;
let mut iter = new;
let peek = iter.peek;
peek.drain_if
.map_or_else;
Performance Considerations
- Single peek operations (
peek()) are optimized to match or exceed the performance ofcore::iter::Peekable. - Double peek operations (
peek_2()) incur only a 90 picosecond overhead compared to single peek operations. - State transitions and element storage are designed to minimize memory operations.
Safety
This crate is marked with #![forbid(unsafe_code)] and employs design by contract
principles, making the property tests far more meaningful.