#iterator #backtracking #history

backtracking_iterator

A simple implementation of a generic iterator with an item history, capable of backtracking and forgetting

10 unstable releases (3 breaking)

0.4.4 Apr 22, 2019
0.4.3 Mar 8, 2019
0.4.1 Dec 25, 2018
0.3.1 Dec 1, 2018
0.1.0 Oct 28, 2018

#1936 in Data structures

MIT license

44KB
595 lines

Backtracking Iterators

Documentation status

A wrapper around existing iterators to extend them with backtracking functionality by providing an in-memory history.

In order to create a backtracking iterator on top of an existing iterator, you first wrap it in a BacktrackingRecord. From there, you have two choices of BacktrackingIterator:

  • Copying, which produces memory clones of the iterator items
  • Referencing, which produces immutable borrows on iterator items

The behaviour comes from the BacktrackingIterator trait.

Example

use backtracking_iterator::{BacktrackingIterator, BacktrackingRecord};

let mut backtracking_record = BacktrackingRecord::new(my_iter);
let mut my_backtracking_iter = backtracking_record.copying();

// Now we can call `next()`, and the result will also be copied
let here = my_backtracking_iter.get_ref_point();
let fresh = my_backtracking_iter.next();

my_backtracking_iter.backtrack(here);
let remembered = my_backtracking_iter.next();

assert!(fresh == remembered);

No runtime deps