#iterator-adapter #save #restore

restorable

An iterator adapter for saving and restoring iterator state

2 unstable releases

0.2.0 Oct 5, 2019
0.1.0 Oct 3, 2019

#1039 in Data structures

MIT/Apache

7KB
104 lines

Restorable: an iterator adapter for saving and restoring iterator state

This is ideal for writing lexers and parsers where backtracking may be required.

Usage

  1. Adapter
use restorable::RestorableIter;

#[test]
fn test_restorable_new() {
    let mut iterator = RestorableIter::new(0..4);
    assert_eq!(iterator.next(), Some(0));
    assert_eq!(iterator.next(), Some(1));
    iterator.save();
    assert_eq!(iterator.next(), Some(2));
    assert_eq!(iterator.next(), Some(3));
    iterator.restore();
    assert_eq!(iterator.next(), Some(2));
    assert_eq!(iterator.next(), Some(3));
    assert_eq!(iterator.next(), None);
}
  1. Trait
use restorable::Restorable;

#[test]
fn test_iter_restorable() {
    let mut iterator = (0..4).iter_restorable();
    assert_eq!(iterator.next(), Some(0));
    assert_eq!(iterator.next(), Some(1));
    iterator.save();
    assert_eq!(iterator.next(), Some(2));
    assert_eq!(iterator.next(), Some(3));
    iterator.restore();
    assert_eq!(iterator.next(), Some(2));
    assert_eq!(iterator.next(), Some(3));
    assert_eq!(iterator.next(), None);
}

Installation

Add restorable under [dependecies] with the appropriate version number in your cargo.toml.

[dependencies]
restorable = "^MAJOR.MINOR"

Changelog

0.2.0 - Added clear to stop saving and clear the buffer.

No runtime deps