Crate transactional_iterator

Source
Expand description

A Transaction can be used to make changes to an iterator and only apply the changes if the transaction is committed. When the Transaction is rolled back, the changes are discarded. This is useful for implementing backtracking searches, parsers, undo functionality, unlimited peeking and more.

The original iterator must implement ‘Clone’ to be useable with the transactional iterator.

§Policies

Transactions can be created with 3 different policies:

  • Panic:
    Will panic on drop if not committed or rolled back.
  • Rollback:
    Will rollback changes on drop or panic.
  • AutoCommit:
    Will commit changes on drop or panic.

§Example

use transactional_iterator::{Transaction, Panic};

let mut iter = vec![1, 2, 3].into_iter();
let mut transaction = Transaction::new(&mut iter, Panic);

// iterate within the transaction
assert_eq!(transaction.next(), Some(1));
assert_eq!(transaction.next(), Some(2));

// Commit the transaction
transaction.commit();

// The changes are now applied
assert_eq!(iter.next(), Some(3));

Structs§

AutoCommit
The policy that tags a transaction to auto commit changes on drop or panic.
Panic
The policy that tags a transaction to panic on drop.
Rollback
The policy that tags a transaction to roll back changes on drop or panic.
Transaction
A Transaction refers to the original iterator and clones a working/backup copy of that. This allows to commit or rollback changes made to the iterator.

Traits§

IsSuccess
A trait to check if a (return) value signifies success. This trait is implemented for Option, Result, bool and (). This can be implemented in user code.
Policy
A trait for the three policies. This allows user code to be generic over the policy.