Buffer is trait that wraps an Iterator. It extends upon this by requiring two extra methods
#![allow(unused)]fnmain() {
/// eagerly drops the first `n` elements in the bufferfnfast_forward(&mutself, n: usize);
/// finds the `i`th element in the iterator, storing any read elements into a buffer for later accessfnpeek_ahead(&mutself, i: usize) -> Option<T>
}
With these two method, the trait can implement a third method, cursor which returns a new Buffer type Cursor.
Cursor reads from a buffer only using peek_ahead.
It ensures that any data read through the buffer can be read again in future.
#![allow(unused)]fnmain() {
use nommy::{Buffer, IntoBuf};
letmut buffer = (0..).into_buf();
letmut cursor1 = buffer.cursor();
// cursors act exactly like an iteratorassert_eq!(cursor1.next(), Some(0));
assert_eq!(cursor1.next(), Some(1));
// cursors can be made from other cursorsletmut cursor2 = cursor1.cursor();
assert_eq!(cursor2.next(), Some(2));
assert_eq!(cursor2.next(), Some(3));
// child cursors do not move the parent's iterator positionassert_eq!(cursor1.next(), Some(2));
assert_eq!(buffer.next(), Some(0));
}
If you read from a cursor and decide that you won't need to re-read that contents again,
you can call fast_forward_parent. This takes how many elements ahead the Cursor has read,
and calls the parent buffer's fast_forward method with it.
#![allow(unused)]fnmain() {
use nommy::{Buffer, IntoBuf};
letmut input = "foobar".chars().into_buf();
letmut cursor = input.cursor();
assert_eq!(cursor.next(), Some('f'));
assert_eq!(cursor.next(), Some('o'));
assert_eq!(cursor.next(), Some('o'));
// Typically, the next three calls to `next` would repeat// the first three calls because cursors read non-destructively.// However, this method allows to drop the already-read contents
cursor.fast_forward_parent();
assert_eq!(input.next(), Some('b'));
assert_eq!(input.next(), Some('a'));
assert_eq!(input.next(), Some('r'));
}
The standard implementation of Buffer is Buf, and can be created from any type that implements IntoIterator.