Trait BacktrackingIterator

Source
pub trait BacktrackingIterator: Iterator {
    type RefPoint;

    // Required methods
    fn get_ref_point(&self) -> Self::RefPoint;
    fn get_oldest_point(&self) -> Self::RefPoint;
    fn backtrack(&mut self, point: Self::RefPoint);

    // Provided methods
    fn start_again(&mut self) { ... }
    fn peek(&mut self) -> Option<Self::Item> { ... }
}
Expand description

An iterator capable of backtracking behaviour This generifies the copying and non-copying versions and their behaviour

Required Associated Types§

Source

type RefPoint

The type used to refer to positions in the history

Required Methods§

Source

fn get_ref_point(&self) -> Self::RefPoint

Yield a reference to the current point in the history This reference must be valid for as long as the current point remains in the history

Source

fn get_oldest_point(&self) -> Self::RefPoint

Yield a reference to the oldest point in the history

Source

fn backtrack(&mut self, point: Self::RefPoint)

Return to a given point in the history Doesn’t have to do anything if the point is invalid

extern crate backtracking_iterator;
use backtracking_iterator::{BacktrackingIterator, Walkback, Walkbackable};

let v = vec![1_u8, 2_u8, 3_u8];
let mut rec = backtracking_iterator::BacktrackingRecorder::new(v.into_iter());
let mut bt = rec.copying();
bt.next(); // 1_u8
bt.next(); // 2_u8
let wb_pos = {
  let mut wb = bt.walk_back();
  assert!(wb.next().unwrap() == 2_u8);
  wb.get_ref_point()
};
 
bt.backtrack(wb_pos);
assert!(bt.next().unwrap() == 2_u8);

Provided Methods§

Source

fn start_again(&mut self)

Start the iterator again from all the elements in the current history The iterator will repeat every element which was emitted since the last call to forget().

extern crate backtracking_iterator;
use backtracking_iterator::BacktrackingIterator;

let v = vec![1_u8, 2_u8];
let mut rec = backtracking_iterator::BacktrackingRecorder::new(v.into_iter());
let mut bt = rec.copying();
bt.next();
bt.start_again();
assert!(bt.next().unwrap() == 1_u8);
Source

fn peek(&mut self) -> Option<Self::Item>

Get the next element of the iterator, without advancing it. The iterator will yield the same element again in the next call to peek() or next().

extern crate backtracking_iterator;
use backtracking_iterator::BacktrackingIterator;

let v = vec![1_u8, 2_u8];
let mut rec = backtracking_iterator::BacktrackingRecorder::new(v.into_iter());
let mut bt = rec.copying();
assert!(bt.peek().unwrap() == 1_u8);
assert!(bt.peek().unwrap() == 1_u8);
assert!(bt.peek().unwrap() == 1_u8);
assert!(bt.next().unwrap() == 1_u8);

Implementors§